Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在转换为PDF时维护word文档的字体属性_C#_.net_Pdf_Ms Word_Office Interop - Fatal编程技术网

C# 如何在转换为PDF时维护word文档的字体属性

C# 如何在转换为PDF时维护word文档的字体属性,c#,.net,pdf,ms-word,office-interop,C#,.net,Pdf,Ms Word,Office Interop,我在将Word文档转换为PDF时遇到问题。在我的word文档中,字体如下(Times New Roman): 但当转换为PDF时,它变成: 我使用了以下代码: Word._Application oWord = new Word.Application(); // Make this instance of word invisible (Can still see it in the taskmgr). oWord.Visible = fa

我在将Word文档转换为PDF时遇到问题。在我的word文档中,字体如下(Times New Roman):

但当转换为PDF时,它变成:

我使用了以下代码:

        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.Interop SaveAs方法时,其他Word格式元素将丢失/更改。这是我用来解决这个问题的方法。我下面的示例使用应用程序和文档,而不是您的_应用程序和_文档。我不熟悉这两种方法的区别,但我认为这两种方法都可以

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阅读器,并用您的代码替换了我的代码。现在,它完全修好了非常感谢。没问题。我发现使用Word和PDF有时会有点棘手。