Reputation: 320
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 !
Upvotes: 3
Views: 5020
Reputation: 320
Thanks for all your aswers.
This (simple) lines work fine :
Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
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.
Upvotes: 2