none
How can I define an output directory and use the original filename when exporting docx to pdf files? D: RRS feed

  • Question

  • Currently I am using Office 2016 and C# and using the ExportAsFixedFormat method I was able to automatically export a directory of docx files to pdf files but I cannot figure out how to use the original names without the file extension for the pdf names. I also cannot figure out how to output the pdf's to a directory of my choosing. Can someone point me in the right direction? I'm a supernewb and I have been trying to learn on my own. Code I managed to get working is below:

     private void Form1_Load(object sender, EventArgs e)
            {
                notifyIcon1.Visible = true;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    listBox1.Items.Clear();
                    label1.Text = "-";
                    string folderPath = "";
                    FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
                    if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                    {
                        folderPath = folderBrowserDialog1.SelectedPath;
                        textBox1.Text = (folderPath);
                    }
    
                    DirectoryInfo dinfo = new DirectoryInfo(folderPath);
                    FileInfo[] Files = dinfo.GetFiles("*.docx");
                    foreach (FileInfo file in Files)
                    {
                        listBox1.Items.Add(file.Name);
                    }
                    label1.Text = listBox1.Items.Count + " .docx Files Detected.";
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("The following error occurred: " + ex.Message);
                }
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                try
                {
                    notifyIcon1.ShowBalloonTip(500, "Exporter", "Exporting..Please Wait.", ToolTipIcon.Info);
                    Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                    var wordDocumentFilenames = Directory.EnumerateFiles(textBox1.Text, "*.docx").ToList();
    
                    foreach (var wordDocumentFilename in wordDocumentFilenames)
                    {
                        Document doc = word.Documents.Open(wordDocumentFilename);
    
                        string outfilename = DateTime.Now.ToString("MM.dd.yyyy_HH.mm.ss_fff.ffff.p\\d\\f");
                        object paramMissing = Type.Missing;
                        WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
                        bool paramOpenAfterExport = false;
                        WdExportOptimizeFor paramExportOptimizeFor = 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;
                        doc.ExportAsFixedFormat(outfilename, paramExportFormat, paramOpenAfterExport,
                            paramExportOptimizeFor, paramExportRange, paramStartPage,
                            paramEndPage, paramExportItem, paramIncludeDocProps,
                            paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                            paramBitmapMissingFonts, paramUseISO19005_1, paramMissing);
                        doc.Close();
                    }
                    word.Application.Quit();
                    notifyIcon1.ShowBalloonTip(500, "Exporter", "Finished Exporting.", ToolTipIcon.Info);
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("The following error occurred: " + ex.Message);
                }
            }

    Wednesday, July 11, 2018 10:19 PM

Answers

  • Hello Sniffles,

    You could try to use Path.GetFileNameWithoutExtension to get the file name without extension and then combine the filename and your save folder path and a ".pdf" extension to save the document as a pdf.

    Such As

                string DocxFileName = "New Microsoft Word Document.docx";
                string NameWithOutExtension = Path.GetFileNameWithoutExtension(DocxFileName);
                string SaveFolderPath = @"C:\Users\terryx\Desktop\TestFolder";
                string outfilename = SaveFolderPath + @"\" + NameWithOutExtension + ".pdf";

    Best Regards,

    Terry


    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.

    • Marked as answer by _Sniffles_ Thursday, July 12, 2018 3:47 PM
    Thursday, July 12, 2018 5:31 AM

All replies

  • Hello Sniffles,

    You could try to use Path.GetFileNameWithoutExtension to get the file name without extension and then combine the filename and your save folder path and a ".pdf" extension to save the document as a pdf.

    Such As

                string DocxFileName = "New Microsoft Word Document.docx";
                string NameWithOutExtension = Path.GetFileNameWithoutExtension(DocxFileName);
                string SaveFolderPath = @"C:\Users\terryx\Desktop\TestFolder";
                string outfilename = SaveFolderPath + @"\" + NameWithOutExtension + ".pdf";

    Best Regards,

    Terry


    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.

    • Marked as answer by _Sniffles_ Thursday, July 12, 2018 3:47 PM
    Thursday, July 12, 2018 5:31 AM
  • It took a little time to figure out how to get this to work but I finally managed to get it working. You are a genius thank you so much!
    Thursday, July 12, 2018 3:47 PM