1
votes

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 !

1
The drawings that you mention, that can simply be done by drawing the objects/pictures. I have a sample, i will look for it. If i find it, will share the code.Christopher H.
What does the code that opens the word doc have to do with the code that prints the PDF? I see no link between them - you open the word doc, then you just create some new printer document and save it. Just because you opened a word document at some point in the past doesn't mean that creating a print document will print it?Caius Jard
@Aousafrashid The drawings are old. They were CorelDraw object but at this time, this "Unknow Object" for the docx.BastienB
You aren't printing anything to that PrintDocument. You opened Word itself with appWord but then try to print in your application without sending any content to the printer. If you opened Word intentionally you can also print from it, with Document.PrintoutPanagiotis Kanavos

1 Answers

2
votes

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.