将wpf窗口打印为pdf文件 [英] print wpf window to pdf file

查看:646
本文介绍了将wpf窗口打印为pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从wpf窗口构建pdf文件.该窗口包含一个画布,其中包含一些绘图,一些文本框和标签以及数据.

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 crystal reports, but seems to not be a good solution for me....

我想在画布上打印图像,并写一些带有texbox和标签数据的行.

I want to print the image on canvas and write some lines whith data of texboxes and labels.

我需要一个非付费解决方案.

I need a non-paid solution.

我该怎么办?

推荐答案

我使用名为iTextSharp的免费工具( EDIT :iTextSharp并非免费用于商业用途,为该问题找到了解决方案-对不起错误信息).实际上,我需要将WPF FixedDocument转换为PDF,因此这与您要执行的操作略有不同,但是也许它也可以为您提供帮助.基本上,方法是获取WPF固定文档(实际上是XPS格式)并将其转换为位图图像.然后,使用iTextSharp的PdfWriter类将该图像作为页面添加到PDF文档中.我尝试了其他几种方法,包括一个名为gxps的免费实用程序,但是这种方法最适合我.

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();

以下是在C#中创建FixedDocument的方法:

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);

这篇关于将wpf窗口打印为pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆