1

Might gonna be a silly question:

I'm using C# and Microsoft.Office.Interop.Word and I'm trying to open a Word document from OpenFileDialog to convert it to PDF (without any changes) but my code decides to use Aspose.Words instead and creates a 134 pages PDF with Aspose watermarks on each page and with nonsense text.

I already have commented the line "//using Aspose.Words;" and have deleted its Nu-Get package but it always results the same 134 pages pdf document even if I choose different Word files for conversion.

My Code uses the following method:

// Convert method
public static void Convert_Word_To_Pdf(string input, string output, destination::Microsoft.Office.Interop.Word.WdSaveFormat format)
{
    // Create an instance of Word.exe
    Word.Application oWord = new Word.Application
    {

        // Make this instance of word invisible (Can still see it in the taskmgr).
        Visible = false
    };

    // Interop requires objects.
    object oMissing = System.Reflection.Missing.Value;
    oWord.Visible = false;
    oWord.ScreenUpdating = false;
    object isVisible = true;
    object readOnly = true;     // Does not cause any word dialog to show up
                                //object readOnly = false;  // Causes a word object dialog to show at the end of the conversion
    object oInput = (object)input;
    object oOutput = output;
    object oFormat = format;

    // Load a document into our instance of word.exe
    Word.Document oDoc = oWord.Documents.Open(
        ref oInput, ref oMissing, ref readOnly, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref isVisible, ref oMissing, ref oMissing, ref oMissing, ref oMissing
        );

    // Make this document the active document.
    oDoc.Activate();
    oOutput = Path.GetFileName(output).Replace(".doc", ".pdf");

    // Save this document using Word
    /*oDoc.SaveAs(ref oOutput, ref oFormat, 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
        );*/
    oDoc.ExportAsFixedFormat(oOutput.ToString(), destination::Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF, false, destination::Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForOnScreen,
            destination::Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument, 1, 1, destination::Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent, true, true,
            destination::Microsoft.Office.Interop.Word.WdExportCreateBookmarks.wdExportCreateHeadingBookmarks, true, true, false, ref oMissing);
    // Close the Word document, but leave the Word application open.
    // doc has to be cast to type _Document so that it will find the
    // correct Close method.                
    object saveChanges = destination::Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
    ((Word.Document)oDoc).Close(ref saveChanges, ref oMissing, ref oMissing);
    oDoc = null;


    // word has to be cast to type _Application so that it will find
    // the correct Quit method.
    ((Word.Application)oWord).Quit(ref oMissing, ref oMissing, ref oMissing);
    oWord = null;
            // Always close Word.exe.
            //oWord.Quit(ref oMissing, ref oMissing, ref oMissing);

As You can see from the comments I have tried different options (found as requested on the net).

Alexey Noskov
  • 1,722
  • 1
  • 7
  • 13
Oliver
  • 11
  • 1

0 Answers0