none
Convert a Word Document into a PDF document using Visual C# RRS feed

  • Question

  • Hello,

    I am trying to convert a Microsoft Word document into a PDF document. Has anybody done this before?

    R\Curtis Poulson

    Monday, August 5, 2019 4:29 PM

Answers

  • Monday, August 5, 2019 4:49 PM
  • Hi cpoulson1,

    Thank you for posting here.

    Based on your description, you want to convert a word document into a pdf document.

    You could try the following code to do it.

     static void Main(string[] args)
            {
                var m = CreatePDF("D:\\test1.docx","D:\\pdfdir");
            }
            public static string CreatePDF(string path, string exportDir)
            {
                Application app = new Application();
                app.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                app.Visible = true;
    
                var objPresSet = app.Documents;
                var objPres = objPresSet.Open(path, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
    
                var pdfFileName =Path.GetFileName(Path.ChangeExtension(path, ".pdf"));
                var pdfPath = Path.Combine(exportDir, pdfFileName);
    
                try
                {
                    objPres.ExportAsFixedFormat(
                        pdfPath,
                        WdExportFormat.wdExportFormatPDF,
                        false,
                        WdExportOptimizeFor.wdExportOptimizeForPrint,
                        WdExportRange.wdExportAllDocument
                    );
                }
                catch
                {
                    pdfPath = null;
                }
                finally
                {
                    objPres.Close();
                }
                return pdfPath;
            }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 6, 2019 3:18 AM
    Moderator

All replies

  • Monday, August 5, 2019 4:49 PM
  • Hi cpoulson1,

    Thank you for posting here.

    Based on your description, you want to convert a word document into a pdf document.

    You could try the following code to do it.

     static void Main(string[] args)
            {
                var m = CreatePDF("D:\\test1.docx","D:\\pdfdir");
            }
            public static string CreatePDF(string path, string exportDir)
            {
                Application app = new Application();
                app.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                app.Visible = true;
    
                var objPresSet = app.Documents;
                var objPres = objPresSet.Open(path, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
    
                var pdfFileName =Path.GetFileName(Path.ChangeExtension(path, ".pdf"));
                var pdfPath = Path.Combine(exportDir, pdfFileName);
    
                try
                {
                    objPres.ExportAsFixedFormat(
                        pdfPath,
                        WdExportFormat.wdExportFormatPDF,
                        false,
                        WdExportOptimizeFor.wdExportOptimizeForPrint,
                        WdExportRange.wdExportAllDocument
                    );
                }
                catch
                {
                    pdfPath = null;
                }
                finally
                {
                    objPres.Close();
                }
                return pdfPath;
            }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 6, 2019 3:18 AM
    Moderator
  • Hi 

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer. So that it will help other members to find the solution quickly if they face the similar issue.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, August 21, 2019 8:04 AM
    Moderator