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.
//xamlText contains XAML FlowDocument in text form
byte data = null;
= new NeuroSpeech.XamlToPDF.Xaml.XamlPdfWriter();
using (MemoryStream stream = new MemoryStream())
data = stream.ToArray();
Response.ContentType = "application/pdf";
Response.OutputStream.Write(data, 0, data.Length);
return File(data, "application/pdf", fileName);
Sample Project containing Text, Font style and image works correctly.
Due to memory leaks in WPF itself, process using this library eats up more memory per pdf conversion. Following is only workaround.
- Create a different process like console app and invoke it from your process. After console app exists, all memory is released automatically.
- 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.
- 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.
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
Only standard fonts are supported as of now, font embedding is not supported.