Click here to Skip to main content
15,936,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, is there a way to print a word file to pdf without using any third party component?

Thanks in advance.
Alder
Posted
Comments
Richard MacCutchan 21-Aug-12 6:54am    
Yes, but it means writing a lot of code. you would need to read the word document and all its metadata and transform that into PDF. But since there are plenty of third-party products already available, why put yourself through all that pain?

C#
public static bool CreatePDF(string WordFilePath)
        {
            bool returnValue = true;
            try
            {
                ApplicationClass wordApplication = new ApplicationClass();
                Document wordDocument = null;

                object paramSourceDocPath = WordFilePath;
                object paramMissing = Type.Missing;

                string paramExportFilePath = Path.ChangeExtension(WordFilePath, "pdf");
                Helper.CheckExistingFile(paramExportFilePath);
                WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
                bool paramOpenAfterExport = false;
                WdExportOptimizeFor paramExportOptimizeFor =
                    Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
                int paramStartPage = 0;
                int paramEndPage = 0;
                WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
                bool paramIncludeDocProps = true;
                bool paramKeepIRM = true;
                WdExportCreateBookmarks paramCreateBookmarks =
                    WdExportCreateBookmarks.wdExportCreateWordBookmarks;
                bool paramDocStructureTags = true;
                bool paramBitmapMissingFonts = true;
                bool paramUseISO19005_1 = false;
                try
                {
                    // Open the source document.
                    wordDocument = wordApplication.Documents.Open(
                        ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing);

                    // Export it in the specified format.
                    if (wordDocument != null)
                        wordDocument.ExportAsFixedFormat(paramExportFilePath,
                            paramExportFormat, paramOpenAfterExport,
                            paramExportOptimizeFor, paramExportRange, paramStartPage,
                            paramEndPage, paramExportItem, paramIncludeDocProps,
                            paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                            paramBitmapMissingFonts, paramUseISO19005_1,
                            ref paramMissing);
                }
                catch (Exception ex)
                {
                    
                    returnValue = false;
                }
                finally
                {
                    // Close and release the Document object.
                    if (wordDocument != null)
                    {
                        wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                        wordDocument = null;
                    }

                    // Quit Word and release the ApplicationClass object.
                    if (wordApplication != null)
                    {
                        wordApplication.Quit(ref paramMissing, ref paramMissing,
                            ref paramMissing);
                        wordApplication = null;
                    }

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                }
            }
            catch(Exception ex)
            {
                returnValue = false;
                
            }
            return returnValue;
        }
 
Share this answer
 
v2
Thank you, I've adopted the solution reported in the 2° link.
I have a problem with the step SaveAs.
If I specify wdFormatPDF as output format it gives me an error ("outsize value").
If I specify any other output format (ex. wdFormatHTML) it works.
I have installed Office 2003.
Any suggestions?

C#
public Boolean Print_Interop(int FileTyp, string Doc_Name, string PDF_Name)
        {
            Word.ApplicationClass MSWordDoc;
            object UnknownType = Type.Missing;
            Boolean TOk = false;

            object InputLocation = Doc_Name;
            object OutputLocation = PDF_Name;

            MSWordDoc = new Word.ApplicationClass();

            try
            {
                //To Open the Word Document
                MSWordDoc.Documents.Open(ref InputLocation,    //Input File Name Location
                    ref UnknownType,    // Conversion Conformation
                    ref UnknownType,    // Set ReadOnly Property
                    ref UnknownType,    // Add to the Recent Files
                    ref UnknownType,    // Document Password Setting
                    ref UnknownType,    // Password Templete
                    ref UnknownType,    // Revert
                    ref UnknownType,    // Write Password to Document
                    ref UnknownType,    // Write Password Template
                    ref UnknownType,    // File Format
                    ref UnknownType,    // Encoding File
                    ref UnknownType,    // Visibility
                    ref UnknownType,    // To Open or Repair
                    ref UnknownType,    // Document Direction
                    ref UnknownType,    // Encoding Dialog
                    ref UnknownType);   // XML Text Transform

                //To Get Document in PDF Format
                object SavePDFFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;

                //To SaveAs the Document
                MSWordDoc.ActiveDocument.SaveAs(ref OutputLocation, //Output File Location
                ref SavePDFFormat,    // File Format
                ref UnknownType,    // Comment to PDF File
                ref UnknownType,    // Password
                ref UnknownType,    // Add to Recent File
                ref UnknownType,    // Write Password
                ref UnknownType,    // ReadOnly Propert
                ref UnknownType,    // Original Font Embeding
                ref UnknownType,    // Save Picture
                ref UnknownType,    // Saving Form Datas
                ref UnknownType,    // Save as AOVE Letter
                ref UnknownType,    // Encoding
                ref UnknownType,    // Inserting Line Breakes
                ref UnknownType,    // Allow Substitution
                ref UnknownType,    // Line Ending
                ref UnknownType);   // Add BiDi Marks

                //To Close the Document File
                MSWordDoc.Documents.Close(ref UnknownType, ref UnknownType, ref UnknownType);

                //To Exit the Word Application
                MSWordDoc.Quit(ref UnknownType, ref UnknownType, ref UnknownType);

                TOk = true;
            }
            catch (Exception e)
            {
                Console.WriteLine("{0} Exception caught.", e);

                TOk = false;
            }

            return TOk;
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900