简体   繁体   中英

How can I transform DOCX to PDF with Microsoft Print to PDF in C#?

I'm trying to convert DOCX to PDF with "Microsoft Print to PDF" in C#. Some objects of my document are drawings and i can't "Save As" without destructuration.

With a printing "Microsoft Print to PDF", all is fine so I want to do this action with my C# program. I've 3000 files to process.

I'm trying this code. It executes a PDF printing and create the wrong file but, it's only blank pages.

//path is my docx path
Application appWord = new Application();
wordDocument = appWord.Documents.Open(path);

PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = "Microsoft Print to PDF";
pd.PrinterSettings.PrintToFile = true;
pd.PrinterSettings.PrintFileName = pdf_path;
pd.Print();

I'm thinking I miss something by I don't understand what. And I don't know if the wordDocument can be the streamReader in some examples on Internet.

Thanks for your help !

Thanks for all your aswers.

This (simple) lines work fine :

Application appWord = new Application();
wordDocument = appWord.Documents.Open(path);
wordDocument.PrintOut(
    OutputFileName:pdf_path,
    PrintToFile: true
);

path is my docx source path

pdf_path is the destination pdf file path

I hope this topic can help someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM