如何在转换为PDF时保持word文档的字体属性

时间:2015-05-13 03:10:02

标签: c# .net pdf ms-word office-interop

我将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 个答案:

答案 0 :(得分:2)

我最近有类似的问题。我的字体不会被更改,但使用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);

希望这可以解决您的问题。