使用iTextSharp将多个页面添加到pdf表单 [英] Add multiple pages to pdf form with iTextSharp
问题描述
我正在尝试填写pdf表单并且表单可能有多个相同表单的页面,因此我不想将每个页面写入磁盘,而是想在内存中创建一个包含所有页面的pdf文档。
我已经看到很多关于使用 FileStream
从现有pdf文件添加页面的示例,但没有真正用于填充论坛并添加它到 MemoryStream
。
基本上我需要用 PdfStamper $填写论坛c $ c>然后将该表单附加到master
MemoryStream
,因为我不想将其写入文件。
<这>基本上我到目前为止
List< Person> people = arson.getPeople();
MemoryStream main = new MemoryStream();
foreach(人物中的人)
{
PdfReader reader = new PdfReader(pathToPdf);
MemoryStream mem = new MemoryStream();
PdfStamper压模=新PdfStamper(读者,mem);
//在论坛中填写个人数据然后追加
}
填写表单不是问题,它只是将新页面附加到 main
流,以便它们都在同一个文档中。如何在内存中添加多个页面而不是先将其写入磁盘?
您是否观看过iText视频教程?您可以在此处找到它,并且有一个示例完全你要求的是什么。
如果你不喜欢看视频,你也可以咨询我的书第6章。在那里你会找到一个解释该做什么的例子。
如果你喜欢食谱书风格,请看看 StackOverflow上最好的iText问题。它至少有一个例子涉及 PdfStamper
和 MemoryStream
的组合。
如果您确实阅读了文档,那么您将找到示例 DataSheets2 :
//步骤1
使用(文件文件=新文件()){
//步骤2
使用(PdfSmartCopy copy = new PdfSmartCopy(document,ms)){
//步骤3
文件。打开();
//步骤4
AddDataSheets(copy);
}
}
您可以找到<4>中的步骤4 a href =http://sourceforge.net/p/itextsharp/code/HEAD/tree/book/iTextExamplesWeb/iTextExamplesWeb/iTextInAction2Ed/Chapter06/DataSheets1.cs =nofollow> DataSheets1 找出来第4步是关于:
public void AddDataSheets(PdfCopy copy){
IEnumerable< Movie> movies = PojoFactory.GetMovies();
//遍历所有电影并填写数据表
foreach(电影中的电影电影){
PdfReader reader = new PdfReader(DATASHEET_PATH);
使用(var ms = new MemoryStream()){
using(PdfStamper stamper = new PdfStamper(reader,ms)){
Fill(stamper.AcroFields,movie);
stamper.FormFlattening = true;
}
reader = new PdfReader(ms.ToArray());
copy.AddPage(copy.GetImportedPage(reader,1));
}
}
如您所见, DATASHEET_PATH
是我们将一遍又一遍地重用的表单的路径。我们使用 PdfStamper
来填写内存中的表单。我们创建一个新的 PdfReader
实例,其中 ms.ToArray()
作为参数。
在这种情况下,我们只将第1页添加到新PDF中。如果有更多页面,则需要遍历不同的页面。
您可能想要比较 DataSheets1 $ c生成的结果$ c>使用
DataSheets2
生成结果。您会注意到 DataSheets1
是不的示例。
您已经知道 FillDataSheet()
方法是什么。如果没有,请查看 FillDataSheet 示例:
public static void Fill(AcroFields表格,电影电影){
表格.SetField(title,movie.MovieTitle);
form.SetField(director,GetDirectors(movie));
form.SetField(year,movie.Year.ToString());
form.SetField(duration,movie.Duration.ToString());
form.SetField(category,movie.entry.category.Keyword);
foreach(在movie.entry.Screenings中放映筛选){
form.SetField(screening.Location.Replace('。','_'),是);
}
}
I am trying to fill out a pdf form and the form could have multiple pages of the same form, so instead of writing each one to disk I want to create one pdf document in memory with all the pages.
I have seen plenty of example on adding pages from an existing pdf file with FileStream
but nothing really for filling out forums and adding it to MemoryStream
.
basically I need to fill out the forum with PdfStamper
then append that form to a "master" MemoryStream
because I dont want to write it to file.
this is basically what I have so far
List<Person> people = arson.getPeople();
MemoryStream main = new MemoryStream();
foreach (Person person in people )
{
PdfReader reader = new PdfReader(pathToPdf);
MemoryStream mem = new MemoryStream();
PdfStamper stamper = new PdfStamper(reader, mem);
//fill in forum with data from person then append
}
filling out the form is not the problem its just appending a new page to the main
stream so that they are all in the same document. How can I add multiple pages in memory a not write it to disk first?
Did you watch the iText video tutorial? You can find it here and there's an example that is doing exactly what you're asking for.
If you don't like watching videos, you can also consult chapter 6 of my book. There too you will find an example that explains what to do.
If you have a preference for a "recipe book" style, please take a look at The Best iText Questions on StackOverflow. It has at least one example involving a combination of PdfStamper
and MemoryStream
.
If you did read the documentation, you would have found the example DataSheets2:
// step 1
using (Document document = new Document()) {
// step 2
using (PdfSmartCopy copy = new PdfSmartCopy(document, ms)) {
// step 3
document.Open();
// step 4
AddDataSheets(copy);
}
}
You can find out what step 4 is about in DataSheets1 to find out what step 4 is about:
public void AddDataSheets(PdfCopy copy) {
IEnumerable<Movie> movies = PojoFactory.GetMovies();
// Loop over all the movies and fill out the data sheet
foreach (Movie movie in movies) {
PdfReader reader = new PdfReader(DATASHEET_PATH);
using (var ms = new MemoryStream()) {
using (PdfStamper stamper = new PdfStamper(reader, ms)) {
Fill(stamper.AcroFields, movie);
stamper.FormFlattening = true;
}
reader = new PdfReader(ms.ToArray());
copy.AddPage(copy.GetImportedPage(reader, 1));
}
}
As you can see, DATASHEET_PATH
is the path to the form that we are going to reuse over and over again. We use PdfStamper
to fill out a form in memory. We create a new PdfReader
instance with ms.ToArray()
as parameter.
In this case, we only add page 1 to the new PDF. If there are more pages, you need to loop over the different pages.
You may want to compare the result generated by DataSheets1
with the results generated with DataSheets2
. You will notice that DataSheets1
is an example on how not to do it.
You already know what the FillDataSheet()
method is about. If not, please take a look at the FillDataSheet example:
public static void Fill(AcroFields form, Movie movie) {
form.SetField("title", movie.MovieTitle);
form.SetField("director", GetDirectors(movie));
form.SetField("year", movie.Year.ToString());
form.SetField("duration", movie.Duration.ToString());
form.SetField("category", movie.entry.category.Keyword);
foreach (Screening screening in movie.entry.Screenings) {
form.SetField(screening.Location.Replace('.', '_'), "Yes");
}
}
这篇关于使用iTextSharp将多个页面添加到pdf表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!