save as pdf from c#

    General discussion

  • According to

    one can save from 2010 word document to PDF


    This information also applies to Microsoft Word Starter 2010.

    Check out Convert a document to PDF for Word 2013 steps.

      • Click the File tab.
      • Click Save As.
        To see the Save As dialog box in Word 2013, you have to choose a location and folder.
      • In the File Name box, enter a name for the file, if you haven't already.
      • In the Save as type list, click PDF (*.pdf).
      • If you want the file to open in the selected format after saving, select the Open file after publishing check box.
      • If the document requires high print quality, click Standard (publishing online and printing).
      • If the file size is more important than print quality, click Minimum size (publishing online).
    1. Click Options to set the page to be printed, to choose whether markup should be printed, and to select output options. Click OK when finished.
    2. Click Save.

    I verifyied the above steps manually with my word 2010.

    When I tried from c# .net 3.5 using office.interop.Word version 14, like this

    Clipboard.SetData(System.Windows.Forms.DataFormats.Html, html);
    			object missing = Type.Missing;
    			Microsoft.Office.Interop.Word.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
    			Microsoft.Office.Interop.Word.Document document = wordApp.Documents.Add(ref missing, ref missing, ref missing, true);
    			wordApp.Visible = true;
    			document.ActiveWindow.Selection.Paste(); //so far so good
    			document.SaveAs2(saveAsFilePathName, System.Windows.Forms.DataFormats.PDF);  // no DataFormats.PDF

    I spent fai amount of time serachign for answer wiithout luck.
    finally stumble acoross wdFormatPDF after google searching with "word interop 14  saveas fileformat"


    // the last line of code
    document.SaveAs2(saveAsFilePathName, System.Windows.Forms.DataFormats.PDF); 
    // should be:
    Microsoft.Office.Interop.Word.WdSaveFormat wdSaveFmt = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
    			object oWdSaveFmt = wdSaveFmt, oSaveAsSpec = saveAsFilePathName;
    			document.SaveAs2(ref oSaveAsSpec, ref oWdSaveFmt);

    I was tempted to delete this thread bu then at least or myself, I will be able the the answer quicker in the future adn maybe comeone can contribut a better way of handling savas PDF

    • Changed type fs - ab Tuesday, July 23, 2013 4:43 AM
    • Edited by fs - ab Tuesday, July 23, 2013 5:29 AM
    Tuesday, July 23, 2013 4:26 AM

All replies

  • I never use SaveAs2. I always use SaveAs. It can also convert it to PDF format.
    Wednesday, July 24, 2013 9:08 AM
  • Howie, please provide sample code using SaveAs. thx.

    I had trouble accessing saveAS instead of saveAS2 for the word 2010 PIA

    • Edited by fs - ab Saturday, July 27, 2013 7:35 AM typo
    Thursday, July 25, 2013 7:01 PM