to save docx as pdf in c# (.net) with policy label

Viewed 4845

i'm converting .docx files to .pdf with Microsoft.Office.Interop.Word library in C# (.Net Framework 4.6.2)

my code is:

    public static void DOCtoPDF(string docFullPath, string pdfFullPath)
    {
        Application appWord = new Application();
        var wordDocument = appWord.Documents.Open(docFullPath);

        wordDocument.SaveAs2(pdfFullPath, WdSaveFormat.wdFormatPDF);
        wordDocument.Close();
        appWord.Quit();
    }

i'm taking this error while i am saving doc file:

enter image description here

how to solve this problem?

2 Answers

As an alternative approach to Microsoft.Office.Interop.Word library, you may want to check out GemBox.Document library.

For example like this:

public static void DOCtoPDF(string docFullPath, string pdfFullPath)
{
    DocumentModel wordDocument = DocumentModel.Load(docFullPath);
    wordDocument.Save(pdfFullPath);
}

I solved my problem..

it's trying to save wordDocument so it show me that dialog..

i wrote

wordDocument.Close(false);

instead of

wordDocument.Close();

now, it is not trying to save my original word document. it only convert to pdf. :)

Related