ThEpRoGrAmMiNgNoOb
ThEpRoGrAmMiNgNoOb

Reputation: 1294

How to maintain font properties of word documents when converted to PDF

I am having trouble with converting Word Document to PDF. In my word document, the font is this (Times New Roman):

enter image description here

but when converted to PDF, it became:

enter image description here

I used the following code:

        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);

What will I do to convert word document to PDF maintaining its font properties?

Upvotes: 3

Views: 1613

Answers (1)

SimTrooper
SimTrooper

Reputation: 92

I recently had a similar issue. My font wouldn't get changed, but other Word formatting elements would be lost/changed when using the Word.Interop SaveAs method. Here is the method I used that fixed the issue for me. My below example uses Application and Document, instead of your _Application and _Document. I'm not familiar with the differences in these, but think it should work with either method.

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);

Hope this fixes your issue.

Upvotes: 3

Related Questions