如何插入PdfPTable到现有的PDF模板? [英] How to insert a PdfPTable into an existing PDF template?

查看:2689
本文介绍了如何插入PdfPTable到现有的PDF模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

经过一番讨论后,我们决定去与TeX的,特别是与Windows兼容的 MIKTEX 。我们意识到,即使我们可以得到由微观布局(这似乎并不可能或者是计算每一行的行高为繁琐的)格式的动态lengthed表,有喜欢大的文本框等动态控件,我们也将需要微操,所以我们决定以动态生成整个文档。考虑到这一点,要TeX的是因为它的明显的选择的力量和我们与它以前的经验,现在一个星期后,我很高兴我们去,那是因为我们所有的报告都是动态生成的,后面的代码是干净的, 。最小

After some discussion, we decided to go with TeX, specifically the windows compatible MiKTeX. We realised that even if we could get the dynamic lengthed table formatted by micromanaging the layout (which doesn't seem possible or is as tedious as calculating row height for each row), there are other dynamic controls like large text boxes that we would also need to micromanage so we decided to generate the whole doc on the fly. With that in mind, going to TeX was the obvious choice because of it's power and our prior experience with it and now after a week, I'm glad we went that way because all our reports are dynamically generated and the code behind is clean and minimal.

原文:

我有生成PDF表单模板使用的LiveCycle,我想填补它(pdfstamper),并添加一些表(pdfptable),但它被证明是比我最初想象的更困难。

I've got a pdf form template generated using LiveCycle and I want to fill it in (pdfstamper) and add some tables (pdfptable), however it's proven to be more difficult than I initially thought.

我打开PDF和使用pdfstamper编辑静态字段:

I open a pdf and use pdfstamper to edit the static fields:

using (var outputPDF1 = new MemoryStream())
{
    var pdfReader = new PdfReader(pdf);
    var pdfStamper = new PdfStamper(pdfReader, outputPDF1);
    var pdfFields = pdfStamper.AcroFields;

    pdfFields.SetField("Field1", "Value1");

这是直接的,明确的。

This is straight forward and clear.

然后我尝试添加一个表,从一组位置去了好几页。有这样的 为3岁,一个更旧版本的iTextSharp的,这是非常手册。

Then I attempt to add a table that will go from a set location over several pages. There is an attempt of this here that is 3 years old with a much older version of itextsharp and it's very manual.

这是代码的关键是使用GetOverContent插入与生成的表ColumnText然而,这需要了解表高度和手动切割表大小

The crux of that code is to use GetOverContent to insert a ColumnText with the generated table however this requires knowing the table height and manually cutting the table to size

    var cb = pdfStamper.GetOverContent(1);
    var ct = new ColumnText(cb);
    ct.Alignment = Element.ALIGN_CENTER; 
    ct.SetSimpleColumn(36, 36, PageSize.A4.Width-36, PageSize.A4.Height-300); 
    ct.AddElement(table);
    ct.Go();

有另一种答案
iTextSharp的:调整2个元素于一个页面
这是仅限于单个页表。它可以扩展,但好像你不得不计算页眉/页脚,以及在随后的页面

There is another answer Itextsharp: Adjust 2 elements on exactly one page which is limited to a single page table. It can be extendable but it seems like you'd have to calculate headers/footers as well on subsequent pages.

和在同一个大方向各种悬而未决的问题:

And various unanswered questions in the same general direction:

加入了动态表到一个PDF模板

iTextSharp的地方Pdfptable在需要的位置

所以我的问题是,什么是使用所见即所得的编辑器静态创建PDF当前的最佳方式修改它以动态的大小添加的内容,如表或任意lengthed文字?我不想从头开始生成一个,如果我们可以使用一个所见即所得的LiveCycle得到模板的工作,但如果得到格式化为一个PDF模板表需要更多的努力不是生成整个事情上飞,那么我宁愿。生成使用iTextSharp的飞整个PDF

So my question is, what is the current best way to statically create a pdf using a WYSIWYG editor and modify it to add dynamic sized content such as a table or arbitrary lengthed text? I don't want to generate one from scratch if we can use a WYSIWYG LiveCycle to get the template working, but if getting a table formatted for a pdf template takes more effort than generating the whole thing on the fly, then i'd rather generate the whole pdf on the fly using itextsharp.

推荐答案

首先这样的:你的代码不会离开你们,如果你不工作'T扁平化的形式。在LiveCycle Designer中创建的文档,PDF格式作为XML的容器。 ,一个PdfPTable可以用来创建PDF语法,这是XML很大的不同。

First this: your code isn't ever going to work if you don't flatten the form. In a document created with LiveCycle Designer, the PDF acts as a container of XML. A PdfPTable can be used to create PDF syntax, which is very different from XML.

第二:你的代码示例中,要添加一个表OVER的现有内容的第一页。这是你的意图?为什么不创建一个表一个单独的文件,然后合并从头与填写的创建文档和扁平形状?
你假设现有的内容会回流?在这种情况下,你不明白PDF,请多做一些阅读

Secondly: in your code sample, you're adding a table OVER the existing content of the first page. Is this your intention? Why not create a separate document with a table, and then merge the document created from scratch with the filled-out and flattened form? Are you assuming that the existing content will reflow? In that case, you don't understand PDF, please do some more reading.

假设你正在使用此代码:

Suppose you are using this code:

var cb = pdfStamper.GetOverContent(1);
var ct = new ColumnText(cb);
ct.Alignment = Element.ALIGN_CENTER; 
ct.SetSimpleColumn(36, 36, PageSize.A4.Width-36, PageSize.A4.Height-300); 
ct.AddElement(table);
ct.Go();

如果这工作正常的你,除了当表不适合一个页面,为什么不ŧ您插入第二个页面?你指的是一个例子,其中有一行:

If this works fine for you, except when the table doesn't fit one page, why don't you insert a second page? You're referring to an example where there's a line:

int status = ct.go(); 

您应该使用的状态值,以找出是否符合表列或不。 。只要表没有完全呈现,你应该插入的页面,并添加表的部分这些新的页面

You should use the value of status to find out if the table fits the column or not. As long as the table isn't rendered completely, you should insert pages, and add parts of the table to those new pages.

当然:你的设计会更更好,如果你适应你的形式,而不是发明一些混合的方法。

Of course: your design would be much better if you adapted your form, rather than inventing some 'hybrid' approach.

这篇关于如何插入PdfPTable到现有的PDF模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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