简体   繁体   English

在c#中使用xml模板的PDFSharp

[英]PDFSharp with xml template in c#

I am using PDFSharp in my c# project. 我在我的c#项目中使用PDFSharp。 In my previous work place I used a PDF component from aspose and in that component ASPOSE provided a xml template feature, where I simply defined the pdf document template in xml format and saved it to hard drive and then from c# code i was opening that template pumping my data and as a outcome it was writing a pdf. 在我之前的工作地点,我使用了aspose的PDF组件,在该组件中ASPOSE提供了一个xml模板功能,我只是以xml格式定义了pdf文档模板并将其保存到硬盘驱动器然后从c#代码中打开了该模板抽取我的数据,结果是写了一个pdf。

It was quiet fast and I was able to achieve performance. 它很安静,我能够达到性能。

Can I achive the same using PDFSharp or any other free opensource component. 我可以使用PDFSharp或任何其他免费的开源组件来实现相同的目标。

I mean i want to define my pdf document in xml format and use from any open good opensource components like pdfshpar or Itext etc.. 我的意思是我想以xml格式定义我的pdf文档,并使用任何开放的好开源组件,如pdfshpar或Itext等。

Please help 请帮忙

Thanks 谢谢

I don't know if you're still working on this but here is what I have tried. 我不知道你是否还在努力,但这是我尝试过的。

MigraDoc can import and interpret the MDDDL format. MigraDoc可以导入和解释MDDDL格式。 Using this it's easy to write a "text" file that contains all the static text of the template. 使用它可以很容易地编写包含模板的所有静态文本的“文本”文件。 When it gets to dynamic stuff, all depends on what you are adding : 当它涉及动态内容时,一切都取决于你要添加的内容:

  • For text, just add markers like when writing, say, ASP.Net <%MyMarker%> 对于文本,只需添加标记,例如写入ASP.Net <%MyMarker%>
  • For images, you can try putting a marker like <%ImageURL%> in the url of the image or in the comments 对于图像,您可以尝试在图像的网址或注释中添加<%ImageURL%>等标记
  • For tables, this gets complicated, either : 对于表,这变得复杂,要么:
    • you can put a blank table with a marker <%dataTable%> in the comments 您可以在注释中放置一个带有标记<%dataTable%>的空白表
    • you can create the table with the columns and the first row (if it has headers) and put a marker in the comments 您可以使用列和第一行(如果它有标题)创建表,并在注释中添加标记
    • you can create 2 tables, one for the 'empty data' and one like previous with columns and first row. 您可以创建2个表,一个用于“空数据”,另一个用于之前的列和第一行。 In this case the marker would be something like <%MyTable{0}%> in the comments for the empty table and <%MyTable{1}%> in the comments in the case of data in the source. 在这种情况下,标记将类似于空表的注释中的<%MyTable{0}%> ,以及源中数据的注释中的<%MyTable{1}%>

Once MigraDoc has loaded the "template" it's easy to scan recursively the elements and replace the markers. 一旦MigraDoc加载了“模板”,就可以很容易地递归扫描元素并替换标记。 It might not be the fastest method for creating documents but it saves me from recompiling the entire application each time the client wants to add a space in a report. 它可能不是创建文档的最快方法,但每次客户端想要在报表中添加空格时,它都会使我无法重新编译整个应用程序。

PDFsharp/MigraDoc does not support XML templates. PDFsharp / MigraDoc不支持XML模板。

You can create documents from code (any .NET language) and pump in your data where you need it. 您可以从代码(任何.NET语言)创建文档,并在您需要的地方输入数据。

That may be a little bit more work than normal report generators, but it gives you much more flexibility than any template-based solution. 这可能比普通报告生成器多一点工作,但它比任何基于模板的解决方案都更灵活。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM