iTextSharp填写表格并创建多个页面 [英] iTextSharp filling forms and creating multiple pages

查看:152
本文介绍了iTextSharp填写表格并创建多个页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下书面代码

  Dim template As String = Server.MapPath(files /)& 2_paged_form.pdf
Dim newFile As String = Server.MapPath(exports /)& newFile.pdf

Dim reader = New PdfReader(template)

Dim output = New FileStream(newFile,FileMode.Create,FileAccess.Write)

Dim stamp = New PdfStamper(reader,output)
stamp.AcroFields.SetField(client,hello)
stamp.AcroFields.SetField(name,test test)
stamp.AcroFields.SetField(address,Hellocourt)
stamp.AcroFields.SetField(postcode,xx 3xx)
stamp.AcroFields.SetField(dob ,11/02/1987)
stamp.FormFlattening = True
stamp.Close()

output.Close()
reader.Close()

我设法创建了一个newfile.pdf,只有2_paged_form.pdf的一次性条目。



但是我有多个信息需要循环,以便newfile.pdf有多个条目。例如newfile.pdf应该有10个页面,包含5个不同的条目。



有人可以帮忙吗?

解决方案

这是在官方的iText网站和书中记录的。



如果您更喜欢观看视频,可以观看本教程。您可以在此处尝试示例。您需要输入填充,拼合和合并:如何正确执行。这些示例的代码可以在这里找到: FillFlattenMerge2 。请注意,还有一个FillFlattenMerge1示例演示 NOT 如何执行此操作。请不要使用该示例; - )



如果您更喜欢读书,请下载iText in Action - Second Edition的第6章。您已经知道如何填写一个表单(如第185页所述),您现在想要合并不同的结果。同样,还有一个示例,说明如何执行此操作(第190页)以及应该如何执行此操作(第190-191页)。



我从来没有写过一行vb.net,但请把这个Java代码看成是伪代码:

  PdfCopy copy = new PdfSmartCopy(document,new FileOutputStream(dest)); 
document.open();
ByteArrayOutputStream baos;
PdfReader阅读器;
PdfStamper压模;
AcroFields字段;
while(data.hasMoreElements()){
//在内存中创建PDF
baos = new ByteArrayOutputStream();
reader = new PdfReader(SRC);
stamper = new PdfStamper(reader,baos);
fields = stamper.getAcroFields();
MyData myData = data.nextElement();
fields.setField(name,myData.getName());
fields.setField(address,myData.getAddress());
...
stamper.setFormFlattening(true);
stamper.close();
reader.close();
//将PDF添加到PdfCopy
reader = new PdfReader(baos.toByteArray());
copy.addDocument(读者);
reader.close();
}
document.close();

如您所见,您需要创建以填充表单,从而生成保存在PDF中的PDF记忆。然后,您需要从内存中读取此PDF并使用 addDocument()方法将其添加到 PdfSmartCopy 实例。 / p>

PS 1: 示例有什么问题?它会导致文本膨胀,因为表单的静态内容会在复制表单时多次添加。 PdfSmartCopy 检查冗余信息,并仅添加静态内容一次。



P.S。 2:为什么有糟糕的方式呢?如果您合并的文档都非常不同,那么糟糕的方式实际上是一种好方法。在这种情况下,方式更快,内存更少,因此实际上是方式。当您合并彼此非常相似的文档时,例如填充了不同数据集的相同表单,这是唯一不好的。


I have following written codes

    Dim template As String = Server.MapPath("files/") & "2_paged_form.pdf"
    Dim newFile As String = Server.MapPath("exports/") & "newFile.pdf"

    Dim reader = New PdfReader(template)

    Dim output = New FileStream(newFile, FileMode.Create, FileAccess.Write)

    Dim stamp = New PdfStamper(reader, output)
    stamp.AcroFields.SetField("client", "hello")
    stamp.AcroFields.SetField("name", "test test")
    stamp.AcroFields.SetField("address", "Hellocourt")
    stamp.AcroFields.SetField("postcode", "xx 3xx")
    stamp.AcroFields.SetField("dob", "11/02/1987")
    stamp.FormFlattening = True
    stamp.Close()

    output.Close()
    reader.Close()

I have managed to created a newfile.pdf with only onetime entry from 2_paged_form.pdf.

However I have multiple information to loop through so that newfile.pdf has multiple entries. for example newfile.pdf should have 10 pages with 5 different entries.

Could anyone help?

解决方案

This is documented on the official iText site and in the book.

If you prefer watching a video, you can watch this tutorial. You can try the examples here. You need the entry "Fill, Flatten and Merge: how to do it correctly." The code for these examples can be found here: FillFlattenMerge2. Note that there's also a FillFlattenMerge1 example that demonstrates how NOT to do it. Please don't use that example ;-)

If you prefer reading a book, please download Chapter 6 of "iText in Action - Second Edition". You already know how to fill out one form (as described on page 185), you now want to merge different results. Again, there's an example on how not to do it (on page 190) and on how you should do it (on page 190-191).

I have never written a line of vb.net, but please look at this Java code as if it were pseudo code:

PdfCopy copy = new PdfSmartCopy(document, new FileOutputStream(dest));
document.open();
ByteArrayOutputStream baos;
PdfReader reader;
PdfStamper stamper;
AcroFields fields;
while (data.hasMoreElements()) {
    // create a PDF in memory
    baos = new ByteArrayOutputStream();
    reader = new PdfReader(SRC);
    stamper = new PdfStamper(reader, baos);
    fields = stamper.getAcroFields();
    MyData myData = data.nextElement();
    fields.setField("name", myData.getName());
    fields.setField("address", myData.getAddress());
    ...
    stamper.setFormFlattening(true);
    stamper.close();
    reader.close();
    // add the PDF to PdfCopy
    reader = new PdfReader(baos.toByteArray());
    copy.addDocument(reader);
    reader.close();
}
document.close();

As you can see, you need to create to fill the form resulting in a PDF that is kept in memory. Then you need to read this PDF from memory and add it to a PdfSmartCopy instance using the addDocument() method.

P.S. 1: What is wrong with the bad example? It results in bloated PDFs because the static content of the form is added redundantly as many times as you copy the form. PdfSmartCopy checks for redundant information and will add the static content only once.

P.S. 2: Why is there a bad way of doing it? The bad way of doing it, is actually a good way if the documents you are merging are all very different. In this case, the bad way is much faster and less memory-extensive and therefore actually the good way. It's only bad when you're merging documents that are very similar to each other, such as the same form filled out with different data sets.

这篇关于iTextSharp填写表格并创建多个页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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