I need to build a pdf file from an wpf window. that window contains a canvas with some draw and some textboxes and labels whith data.
a friend told me to use crysta
I hope this library helps you.
PDFsharp
is a .NET library for processing PDF file. You create PDF pages using drawing routines known from GDI+. Almost anything that can be done with GDI+ will also work with PDFsharp. Only basic text layout is supported by PDFsharp, and page breaks are not created automatically. The same drawing routines can be used for screen, PDF, or meta files.
http://www.pdfsharp.net/MainPage.ashx
PDFsharp is published under the MIT License.
ok, its for gdi+, but perhaps it is enough.
I found a solution to this problem using a free tool called iTextSharp (EDIT: iTextSharp is NOT free for commercial use - sorry for the misinformation). Actually, I needed to convert a WPF FixedDocument to PDF, so this is slightly different than what you want to do, but perhaps it can help you too. Basically, the approach is to take a WPF Fixed Document (this is actually XPS format) and convert it to a bitmap image. This image is then added as a page to a PDF document using iTextSharp's PdfWriter class. I tried several other methods, including a free utility called gxps, but this method worked best for me.
Here is an example from my code.
using iTextSharp.text;
using iTextSharp.text.pdf;
.
.
.
// create an iTextSharp document
Document doc = new Document(PageSize.LETTER, 0f, 0f, 0f, 0f);
PdfWriter.GetInstance(doc, new FileStream("C:\\myFile.pdf", FileMode.Create));
doc.Open();
// cycle through each page of the WPF FixedDocument
DocumentPaginator paginator = myFixedDocument.DocumentPaginator;
for (int i = 0; i < paginator.PageCount; i++)
{
// render the fixed document to a WPF Visual object
Visual visual = paginator.GetPage(i).Visual;
// create a temporary file for the bitmap image
string targetFile = Path.GetTempFileName();
// convert XPS file to an image
using (FileStream outStream = new FileStream(targetFile, FileMode.Create))
{
PngBitmapEncoder enc = new PngBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(CreateBitmapFromVisual(visual, 300, 300)));
enc.Save(outStream);
}
// add the image to the iTextSharp PDF document
using (FileStream fs = new FileStream(targetFile, FileMode.Open))
{
iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromStream(fs), System.Drawing.Imaging.ImageFormat.Png);
png.ScalePercent(24f);
doc.Add(png);
}
}
doc.Close();
Here is how to create a FixedDocument in C#:
using System.Windows.Documents;
using System.Windows.Documents.Serialization;
using System.Windows.Markup;
// create an instance of your XAML object (Window or UserControl)
var yourXAMLObj = new YourXAMLObject();
// create a FixedDocument and add a page of your XAML object
var fixedDocument = new FixedDocument();
fixedDocument.DocumentPaginator.PageSize = new Size(96 * 8.5, 96 * 11);
PageContent pageContent = new PageContent();
FixedPage fixedPage = new FixedPage();
fixedPage.Children.Add(yourXAMLObj);
fixedDocument.Pages.Add(pageContent);
((IAddChild)pageContent).AddChild(fixedPage);