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

标签 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 并保持其字体属性?

最佳答案

我最近遇到了类似的问题。我的字体不会更改,但其他 Word 格式元素在使用 Word.Interop SaveAs 方法时会丢失/更改。这是我用来为我解决问题的方法。我下面的示例使用应用程序和文档,而不是您的 _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);

希望这能解决您的问题。

关于c# - 如何在转换为PDF时保持word文档的字体属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30204996/

相关文章:

c# - HttpClient爬取导致内存泄漏

C# MongoDB 驱动程序忽略超时选项

java - 如何在 iTextPDF 中对齐图形?

iphone - 安全/加密的 PDF : iOS 4. 3 显示空白页

c# - System.String[*] 代表什么?

c# - Azure 表高级查询

c# - 如何将 span 元素放入 ActionLink MVC3 中?

c# - Directory.Move 不适用于 UNC 路径

c# - Paypal TLS 安全更改

iphone - 使用 UIWebView 显示某些 PDF 会错误地呈现某些文本