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

18,524

Solution 1

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

Solution 2

This code works fine for me with text, tables, graphs and images in word document.
Need to have Word installed in server.
I'm using Word 2013 and its ok.

Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application();

                    object oMissing = System.Reflection.Missing.Value;
                    Object oFalse = false;
                    Object filename = (Object)path;

                    Microsoft.Office.Interop.Word.Document doc = wordApplication.Documents.Open(ref filename, ref oMissing,
                        ref oFalse, 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);

                    doc.Activate();

                    var pathfilename = Path.Combine(Server.MapPath("~/UploadDocuments/PDF"), fileName).Replace(".docx", ".pdf"); ;
                    Object filename2 = (Object)pathfilename;

                    doc.SaveAs(ref filename2, WdSaveFormat.wdFormatPDF,
                        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);


                    // close word doc and word app.
                    object saveChanges = WdSaveOptions.wdDoNotSaveChanges;

                    ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);

                    ((_Application)wordApplication).Quit(ref oMissing, ref oMissing, ref oMissing);

                    wordApplication = null;
                    doc = null;
Share:
18,524
Naga Sandeep
Author by

Naga Sandeep

.Net Programmer Angular.js novice

Updated on June 05, 2022

Comments

  • Naga Sandeep
    Naga Sandeep almost 2 years

    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.