如何在将Word文档转换为PDF时保持字体属性不变

3

我在将Word文档转换为PDF时遇到了问题。我的Word文档中的字体是这样的(Times New Roman):

enter image description here

但是当转换为PDF时,变成了:

enter image description here

我使用了以下代码:

        Word._Application oWord = new Word.Application();

        // Make this instance of word invisible (Can still see it in the taskmgr).
        oWord.Visible = false;

        // Interop requires objects.
        object oMissing = System.Reflection.Missing.Value;
        object isVisible = true;
        object readOnly = false;
        object oInput = Application.StartupPath+"\file.docx";
        object oOutput = Application.StartupPath+"\file.docx".Replace(".docx", ".pdf");
        object oFormat = Word.WdSaveFormat.wdFormatPDF;

        // Load a document into our instance of word.exe
        Word._Document oDoc = oWord.Documents.Open(ref oInput, ref oMissing, ref readOnly, 
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
            ref oMissing, ref oMissing, ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

        // Make this document the active document.
        oDoc.Activate();

        // Save this document in Word 2003 format.
        oDoc.SaveAs(ref oOutput, ref oFormat, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

        // Always close Word.exe.
        oWord.Quit(ref oMissing, ref oMissing, ref oMissing);

我该如何将Word文档转换为PDF并保持其字体属性?

1个回答

3

我最近也遇到了类似的问题。使用Word.Interop的SaveAs方法时,我的字体无法更改,但其他Word格式元素会丢失/更改。这是我用来解决问题的方法。我的下面示例使用Application和Document,而不是您的_Application和_Document。我不熟悉它们之间的区别,但认为应该适用于任何一种方法。

bool _OpenAfterExport = false;
bool _KeepIRM = true;
int _From = 1;
int _To = 1; //I thought this was odd, setting From and To to 1, but it exported all pages of the document
bool _IncludeDocProps = true;

Word.Document oDoc = oWord.Documents.Open(inputFile);
oDoc.ExportAsFixedFormat(outputFile,
                         Word.WdExportFormat.wdExportFormatPDF,
                         OpenAfterExport,
                         Word.WdExportOptimizeFor.wdExportOptimizeForPrint,
                         Word.WdExportRange.wdExportAllDocument,
                         _From,
                         _To,
                         Word.WdExportItem.wdExportDocumentContent,
                         _IncludeDocProps,
                         _KeepIRM,
                         Word.WdExportCreateBookmarks.wdExportCreateHeadingBookmarks)
oDoc.Close();
oWord.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);

希望这能解决您的问题。

我重新安装了所有的PDF阅读器,并用你的代码替换了我的代码。现在,它完美地修复了。^_^非常感谢。. . - ThEpRoGrAmMiNgNoOb
没问题。我发现有时候使用Word和PDF可能会有些棘手。 - SimTrooper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接