Search code examples
c#pdfms-wordoffice-interop

Save as PDF using c# and Interop not saving embedded pdf in a word document


Background: I am trying to generate a word document using c# and Interop. I am successful in inserting tables, headers, footers, diagrams..most of the things are working fine. As a next step, I am looking to convert this into pdf.

Process adapted: Since I am using word 2011 and word 2007 (after installing saveaspdf plugin), I am saving the document in pdf using "save as" and then file type as pdf.

Problems Raised: I have successfully embedded a pdf into my document using following code.

        Object oIconLabel = new FileInfo(file_path).Name;
        Object oFileDesignInfo = file_path;
        Object oClassType = "AcroExch.Document.7";
        Object oTrue = true;
        Object oFalse = false;
        Object oMissing = System.Reflection.Missing.Value;
        Object index = 0;
        Range r = d.Bookmarks.get_Item(ref eod).Range;

        r.InlineShapes.AddOLEObject(
            ref oClassType, ref oFileDesignInfo, ref oFalse, ref oTrue, ref oMissing,
            ref index, ref oIconLabel, ref oMissing);

Here d is the Document object. Now When I tried to save this word document as pdf, using

 d.SaveAs(ref filename, ref filetype, 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);

pdf is getting generated. But the document which i embedded is not getting embedded into the generated pdf. rather only a icon is getting displayed.

How can I embed the document into pdf.? How do we usually handle this kind of situations. Kindly let me know if question is not clear.


Solution

  • it is the wrong api thot I chose. it should not be SaveAs. it is ExportAsFixedFormat.

    documentation is present here

    sample usage can be

    d.ExportAsFixedFormat(filename.ToString(), WdExportFormat.wdExportFormatPDF,false,WdExportOptimizeFor.wdExportOptimizeForOnScreen,
                        WdExportRange.wdExportAllDocument,1,1,WdExportItem.wdExportDocumentContent,true,true,
                        WdExportCreateBookmarks.wdExportCreateHeadingBookmarks,true,true,false,ref oMissing);