What is the best way to convert FlowDocument to PDF

How would I recommend converting FlowDocument to PDF to join EMail?

FlowDocument is dynamic, not static.

I would prefer to save the PDF file in memory as a byte [] rather than on disk, but this can be discussed.

Thanks for your help!

+5
source share
6 answers

You have two options that I know of.

First, use a proprietary library called NiPDF , otherwise you can use Word Interop.

  • Save FlowDocument to DOCX file using Open XML SDK
  • Use Word Interop to load a saved document from some temporary storage.
  • Set WdSaveFormat to wdFormatPDF
  • Save the document again (do not forget to rename the file to PDF)

You can find more information here.

+3
source

I assume that you want this to happen programmatically and not as a manual process.

Method 1: Install a PDF driver, such as Amyuni or PrimoPDF . Print the FlowDocument with the desired PrintTicket / page size in the printer driver. The PDF you get from it should be a pretty good conversion. Some of these drivers (e.g. Amyuni) have SDKs that you can programmatically manage this process.

Method 2: Print to XPS programmatically using the XPS driver without the Save As dialog; there you can build a sample for this in the Windows DDK yourself quite easily. Then use an XPS to PDF converter such as NiXPS or Adobe SDK (so expensive, I wonโ€™t post the link) or GhostXPS to convert XPS directly to PDF.

Method 3: Convert the flow document directly to XPS using methods such as This , and then use XPS to PDF Converter, such as those mentioned above.

Disclaimer: I do not work for any of these companies or their competitors. I used the Adobe SDK, Amyuni printer and various XPS tricks with pretty good success. No method will convert to 100% accuracy.

+4
source

Disclaimer: I am the author of the XamlToPDF library, however it is free for any type of use.

http://xamltopdf.codeplex.com/

It is very easy to create PDF, it also supports tables and images.

+2
source

You might want to consider Seberix's "Report Writer for.NET" product (http://www.siberix.com/). Its API is similar to the code located in the FlowDocument, which I think you need if you said โ€œdynamic, not staticโ€. (But I'm sure there are differences, as well as devils).

Once the Siberix.Report.Report object has been created,

Siberix.Report.Report report = CreateMyPdfReport(); //You write this Stream stream = new MemoryStream(); report.Publish(stream, Siberix.Report.FileFormat.PDF); byte[] bytes = stream.ToArray(); 

Bytes can now be stored in a database table or whatever.

0
source

I managed to get this working with the PDFCreator printer driver. You need to install a driver for this to work, so this may not be the ideal solution for some people. There is a COM interface. The code more or less looks something like this:

  PDFCreator.clsPDFCreator _PDFCreator; PDFCreator.clsPDFCreatorError pErr; if (_PDFCreator.cStart(parameters, false)) { _PDFCreator.cClearCache(); _PDFCreator.set_cOption("UseAutosave", 1); _PDFCreator.cPrinterStop = false; } _PDFCreator.set_cOption("AutosaveFilename", file); _PDFCreator.set_cOption("AutosaveDirectory", folder); PrintDialog printDialog = new PrintDialog(); printDialog.PrintDocument(((IDocumentPaginatorSource)flowDoc).DocumentPaginator, "Report"); 
0
source

Another option to view is FlowDocumentConverter . This is available in the NuGet package , but there is a 10 page limit for each documentation .

0
source

Source: https://habr.com/ru/post/1395291/


All Articles