简体   繁体   English

将FlowDocument转换为PDF的最佳方法是什么?

[英]What's the best way to convert a FlowDocument into PDF

How would ya'll recommend that I convert a FlowDocument to PDF to be attached to an EMail? 你会如何建议我将FlowDocument转换为PDF以附加到电子邮件?

The FlowDocument is dynamic, not static. FlowDocument是动态的,而不是静态的。

I would prefer to be able to store the PDF in memory as a byte[], rather than on disk, but that is negotiable. 我希望能够将PDF作为byte []存储在内存中,而不是存储在磁盘上,但这是可以协商的。

Thanks for your help! 谢谢你的帮助!

I am assuming you want this to occur programmatically rather than as a manual process. 我假设您希望以编程方式而不是手动过程进行此操作。

Method 1: Install a PDF driver such as Amyuni or PrimoPDF . 方法1:安装AmyuniPrimoPDF等PDF驱动程序。 Print your FlowDocument with the desired PrintTicket / page size, to the print driver. 将具有所需PrintTicket /页面大小的FlowDocument打印到打印驱动程序。 The PDF you get from it should be a fairly good conversion. 您从中获得的PDF应该是一个相当不错的转换。 Some of these drivers (such as Amyuni) have SDKs that you can control this process programmatically. 其中一些驱动程序(如Amyuni)具有SDK,您可以通过编程方式控制此过程。

Method 2: Print to XPS programmatically using an XPS driver without a Save As dialog; 方法2:使用XPS驱动程序以编程方式打印到XPS,而不使用“另存为”对话框; there's a sample for this in the Windows DDK you can build yourself fairly easily. 在Windows DDK中有一个示例,您可以相当容易地构建自己。 Then use an XPS to PDF converter such as NiXPS or the Adobe SDK (so expensive I won't post a link) or GhostXPS to convert the XPS directly to PDF. 然后使用XPS到PDF转换器,如NiXPS或Adobe SDK(如此昂贵,我不会发布链接)或GhostXPS将XPS直接转换为PDF。

Method 3: Convert the flow document directly into XPS using methods like This one and then use an XPS to PDF converter such as the ones mentioned above. 方法3:使用像这样的方法将流文档直接转换为XPS,然后使用XPS到PDF转换器,如上面提到的那些。

Disclaimer: I don't work for any of these companies or their competitors. 免责声明:我不为这些公司或其竞争对手工作。 I've used the Adobe SDK, the Amyuni printer, and various XPS tricks with fairly good success. 我已经使用了Adobe SDK,Amyuni打印机和各种XPS技巧并取得了相当不错的成功。 No method will convert with 100% accuracy. 没有方法可以100%准确转换。

Disclaimer: I am writer of XamlToPDF Library, however it is free for any type of use. 免责声明:我是XamlToPDF库的作者,但它可以免费用于任何类型的使用。

http://xamltopdf.codeplex.com/ http://xamltopdf.codeplex.com/

Its very easy to create PDF, it supports Tables and images as well. 它非常容易创建PDF,它也支持表格和图像。

You have two options that I know of. 你知道我有两个选择。

The first is to use a proprietary library called NiPDF , alternatively you can use Word Interop. 第一种是使用名为NiPDF的专有库,或者您可以使用Word Interop。

  1. Save the FlowDocument to a DOCX file using Open XML SDK 使用Open XML SDK将FlowDocument保存到DOCX文件
  2. Use Word Interop to load the saved document from some temporary store 使用Word Interop从某个临时存储加载已保存的文档
  3. Set the WdSaveFormat to wdFormatPDF WdSaveFormat设置为wdFormatPDF
  4. Save the document again (remember to rename the file to PDF) 再次保存文档(记得将文件重命名为PDF)

You can find further info here 你可以在这里找到更多信息

I managed to get this working with the PDFCreator printer driver. 我设法使用PDFCreator打印机驱动程序。 You need to install the driver for this to work, so it may not be an ideal solution for some people. 您需要安装驱动程序才能工作,因此对某些人来说可能不是理想的解决方案。 There is a COM interface available. 有一个COM接口可用。 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");

Another option to look at is FlowDocumentConverter . 另一个选项是FlowDocumentConverter This is available in a NuGet package , but per documentation does have a 10 page limitation. 这在NuGet包中可用,但每个文档确实有10页的限制。

You might want to consider the product called "Report Writer for .NET" by Seberix (http://www.siberix.com/). 您可能想要考虑Seberix(http://www.siberix.com/)上的“Report Writer for .NET”产品。 Its API is similar to the code behind in FlowDocument which I think is you want want when you said "dynamic, not static". 它的API类似于FlowDocument中的代码,我认为当你说“动态而不是静态”时你想要它。 (But I'm sure there are differences as well. Devils are in the details). (但我确信也存在差异。魔鬼在细节中)。

Once a Siberix.Report.Report object has been created, 一旦创建了Siberix.Report.Report对象,

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

Now bytes can be saved to a database table or whatever. 现在可以将字节保存到数据库表或其他任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Windows Universal应用程序将docx / pptx文档转换为PDF的最佳方法是什么? - What's the best way to convert docx/pptx documents to PDF from a Windows Universal App? 如何使用FlowDocument的IDocumentPaginatorSource DocumentPaginator将FlowDocument转换为FixedDocument? - How to convert FlowDocument to FixedDocument using FlowDocument's IDocumentPaginatorSource DocumentPaginator? 将枚举转换为字符串的最佳实践方法是什么? - What's the best practice way to convert enum to string? 将Newtonsoft JSON的JToken转换为JArray的最佳方法是什么? - What is the best way to convert Newtonsoft JSON's JToken to JArray? 将此转换为SQL的最佳方法是什么? - What is the best way to convert this to SQL 在线添加用户签名到PDF文档的最佳方法是什么? - What's the best way to adding a user's signature to a PDF document online? 将Flowdocument转换为XML,并将XML转换为Flowdocument - Convert a Flowdocument to XML and XML to Flowdocument 什么是多线程的最佳方式? - What's the best way to multithread? 在EmguCV中将视频转换为List的最佳方法是什么? - What is the best way to convert a video to a List in EmguCV 将Address对象转换为String的最佳方法是什么? - What is the best way to convert an Address object to a String?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM