Project Description
Simple FlowDocument to PDF Converter with paging, header and footer
 

Project was created to convert FlowDocument to PDF, that enables inbuilt paging supported by WPF Xaml Engine. Target is to use this inside ASP.NET project to generate reports.

Code:

//xamlText contains XAML FlowDocument in text form
byte[] data = null;
NeuroSpeech.XamlToPDF.Xaml.XamlPdfWriter writer 
	= new NeuroSpeech.XamlToPDF.Xaml.XamlPdfWriter();
using (MemoryStream stream = new MemoryStream()) { writer.Write(xamlText, stream); data = stream.ToArray(); } // ASP.NET Response.AddHeader("content-disposition", "attachment;filename=\"PDF.pdf\""); Response.ContentType = "application/pdf"; Response.OutputStream.Write(data, 0, data.Length); //ASP.NET MVC return File(data, "application/pdf", fileName);

Sample Project containing Text, Font style and image works correctly.

Known Limitations

Due to memory leaks in WPF itself, process using this library eats up more memory per pdf conversion. Following is only workaround.

  1. Create a different process like console app and invoke it from your process. After console app exists, all memory is released automatically.
  2. In case of IIS, you can create a new website with a web service which you can invoke from other web sites, and keep this website with application pool to recycle after every 100 or 1000 requests.
  3. In case of IIS, if you are using this library directly in your website, then make sure your application pool recycles after every few request.

Code Organization

This project is organized as following,

  • PDF Objects
    • PDF objects are PDF file objects as specified in the PDF specs. Given FlowDocument is converted to PDF objects, and they are written to fie.
    • Most important object is PDFPage object and its ContentStream.
  • FlowDocument Paginator returns FixedDocument Sequence which contains Page Visuals, and we are navigating page visual tree. For each page, we create one PDFPage and we write contents into PDFPage while navigating visual tree.
  • XamlPdfWriter navigates visual tree and converts objects into corresponding PDF objects.

Features in development

  • Cirlce/Arcs

Only standard fonts are supported as of now, font embedding is not supported.

Last edited May 5, 2013 at 8:45 AM by neurospeech, version 22