首页 新闻 会员 周边

C# 如何能让Word 转 PDF格式不变

0
悬赏园豆:20 [待解决问题]

我的文件在进行Word 转 PDF格式的时候,进行了自动的换行,导致我的PDF文件与word文件有差别,文件在第二条的最后进行了回车,如何解决,让文件转换成PDF格式,样式与word一模一样,已排除(文档的问题,第三方版本库的问题)?
原文件(绿色)与转成PDA文件(白色),文件在第二条的最后进行了回车,如下图!
我的目前转PDF的方法:
public static string WordToPDF(string filePath, bool IsResetResolution = true,int Resolution = 200)
{
string filePath_pdf = filePath.Replace(filePath.Split('.')[filePath.Split('.').Count() - 1], "pdf");
Spire.Doc.Document document = new Spire.Doc.Document();
document.LoadFromFile(filePath);

        // 得到word的总页码
        int pages = document.PageCount;
        if (pages > 25)
        {
            document.Close();
            document.Dispose();
            Microsoft.Office.Interop.Word.Application app = null;
            try
            {
                app = new Microsoft.Office.Interop.Word.Application();
                #region 保存为PDF
                object nullobj = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word.Document doc = app.Documents.OpenNoRepairDialog(filePath, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj,
                ref nullobj, ref nullobj, ref nullobj, ref nullobj);

                object filePath_pdf_obj = filePath_pdf;
                //设置保存的格式   
                object filefarmat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
                Object missing = System.Reflection.Missing.Value;
                doc.SaveAs(ref filePath_pdf_obj, ref filefarmat, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

                doc.Close();
                app.Quit();
             
                #endregion
            }
            catch {
            }
        }
        else
        {
            document.SaveToFile(filePath_pdf, FileFormat.PDF);
            document.Dispose();
        }

        return filePath_pdf;
    }

已试过方法

简单,的主页 简单, | 初学一级 | 园豆:37
提问于:2024-05-28 15:36
< >
分享
所有回答(2)
0

为了确保Word文档在转换为PDF时格式保持不变,可以尝试以下几个方法和注意事项:

使用Spire.Doc设置兼容选项:
Spire.Doc可以通过设置兼容选项来改善格式问题。确保使用最新版本的Spire.Doc库。

设置页面布局和字体:
在转换过程中设置文档的页面布局和字体,确保与原始Word文档一致。

禁用自动换行和调整文档内容:
尽量避免使用自动换行功能,并在转换前手动调整文档内容。

下面是一个修改后的示例代码,加入了一些额外的配置来改善格式问题:

csharp
Copy code
public static string WordToPDF(string filePath, bool IsResetResolution = true, int Resolution = 200)
{
string filePath_pdf = filePath.Replace(filePath.Split('.').Last(), "pdf");
Spire.Doc.Document document = new Spire.Doc.Document();
document.LoadFromFile(filePath);

// 设置文档兼容性选项
document.CompatibilityOptions.DoNotExpandShiftReturn = true;
document.CompatibilityOptions.UseWord2003LineSpacing = true;

// 设置页面布局和字体
foreach (Spire.Doc.Section section in document.Sections)
{
    section.PageSetup.Orientation = Spire.Doc.Documents.PageOrientation.Portrait;
    section.PageSetup.PageSize = Spire.Doc.Documents.PageSize.A4;
    foreach (Spire.Doc.Documents.Paragraph paragraph in section.Paragraphs)
    {
        paragraph.Format.KeepFollow = true;
        paragraph.Format.KeepLines = true;
    }
}

// 得到word的总页码
int pages = document.PageCount;
if (pages > 25)
{
    document.Close();
    document.Dispose();
    Microsoft.Office.Interop.Word.Application app = null;
    try
    {
        app = new Microsoft.Office.Interop.Word.Application();
        #region 保存为PDF
        object nullobj = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word.Document doc = app.Documents.OpenNoRepairDialog(filePath, ref nullobj, ref nullobj,
        ref nullobj, ref nullobj, ref nullobj,
        ref nullobj, ref nullobj, ref nullobj,
        ref nullobj, ref nullobj, ref nullobj,
        ref nullobj, ref nullobj, ref nullobj, ref nullobj);

        object filePath_pdf_obj = filePath_pdf;
        //设置保存的格式   
        object filefarmat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
        Object missing = System.Reflection.Missing.Value;
        doc.SaveAs(ref filePath_pdf_obj, ref filefarmat, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

        doc.Close();
        app.Quit();
     
        #endregion
    }
    catch
    {
    }
}
else
{
    document.SaveToFile(filePath_pdf, Spire.Doc.FileFormat.PDF);
    document.Dispose();
}

return filePath_pdf;

}
额外建议:
检查原始Word文档的格式:
确保原始Word文档中没有隐藏的格式或不可见字符,这些可能会影响转换后的PDF格式。

更新软件库:
确保使用最新版本的Spire.Doc和Microsoft Office Interop库,以获得最新的功能和修复。

使用其他转换工具:
如果问题依然存在,可以尝试使用其他高质量的Word到PDF转换工具,例如Adobe Acrobat或其他第三方库(如 iTextSharp)。

这些步骤和建议应该有助于解决Word转换为PDF时的格式不一致问题。

Technologyforgood | 园豆:6790 (大侠五级) | 2024-05-28 20:05
0

优先尝试调参数,多试试。还不行的话,可以考虑换库, Aspose 系质量更好。

 
幻天芒 | 园豆:37205 (高人七级) | 2024-06-06 21:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册