[英]Creating a multiple pages pdf with iTextSharp
I'm trying the create a multiple-page pdf using iTextSharp, but I'm having some issue creating a loop to have more than a single page. 我正在尝试使用iTextSharp创建多页pdf,但是在创建具有多个页面的循环时遇到了一些问题。
My code below is working well for one page; 我的以下代码在一页上运行良好; however, my content will not fit into a single page.
但是,我的内容无法放入单个页面。 Thanks.
谢谢。
How can I write a loop to write contents on the first pdf page and the remaining to the second, third, etc...So far, I'm only seeing one page. 我该如何写一个循环以将内容写在第一个pdf页面上,其余的内容写在第二个,第三个等上……到目前为止,我只看到一个页面。 Thank you.
谢谢。
int height = 600;
int totalPage = 1;
int oldPage = 1;
bool cons = false;
bool st = false;
foreach (string al in combo)
foreach (string al in combo) //my loop to write on the pdf but "combo" has 100 lines, which would fit into a single page.
{
string[] word = al.Split(',');
int strIndex = combo.IndexOf(al);
if (al.Length != 0)
{
if (word[0].ToString().ToUpper().Contains("(REV")==true && cons == false)
{
cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 11);
cb.BeginText();
// put the alignment and coordinates here
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "CONSTRUCTION PRINTS", 80, height, 0);
cb.EndText();
height = height - 20;
cons = true;
}
if (word[0].ToString().ToUpper().Contains("(REV")==false && st == false)
{
cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 11);
cb.BeginText();
// put the alignment and coordinates here
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "SAG & TENSION CHARTS", 80, height, 0);
cb.EndText();
height = height - 20;
st = true;
}
cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 11);
totalPage = totalPage + oldPage;
// write the text in the pdf content
cb.BeginText();
// put the alignment and coordinates here
cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, word[0].ToString().ToUpper(), 80, height, 0);
cb.EndText();
// write the text in the pdf content
cb.BeginText();
// put the alignment and coordinates here
cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "......................................................................................", 335, height, 0);
cb.EndText();
// write the text in the pdf content
cb.BeginText();
// put the alignment and coordinates here
cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, totalPage.ToString(), 500, height, 0);
cb.EndText();
oldPage = Convert.ToInt32(word[1]);
}
height = height - 20;
}
//// create the new page and add it to the pdf
// reader = new PdfReader(oldFile);//old file
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
//// close the streams and voilá the file should be changed :)
document.Close();
reader.Close();
fs.Close();
writer.Close();
Please go to the official documentation and click Q&A to go to the most frequently asked questions. 请转到官方文档 ,然后单击“ 问答”以转到最常见的问题。 Pick the Getting started category.
选择入门类别。 The first thing you'll see, is the most popular iText example (which I am porting to C# for your convenience):
您会看到的第一件事是最受欢迎的iText示例(为了方便起见,我将其移植到C#中):
// step 1
Document document = new Document();
// step 2
FileStream fs = new FileStream("hello.pdf", FileMode.Create);
PdfWriter.GetInstance(document, fs);
// step 3
document.Open();
// step 4
document.Add(new Paragraph("Hello World!"));
// step 5
document.Close();
In your code, you are adding text at absolute positions by introducing PDF syntax, such as BeginText()
( BT
) / EndText()
( ET
) / SetFontAndSize()
( Tf
). 在您的代码中,您将通过引入PDF语法在绝对位置添加文本,例如
BeginText()
( BT
)/ EndText()
( ET
)/ SetFontAndSize()
( Tf
)。 This is creating PDF the hard way. 这是很难创建PDF的方法。 If is very easy to make mistakes, as shown in this question: What is causing syntax errors in a page created with iText?
如以下问题所示,if很容易出错: 是什么导致使用iText创建的页面中的语法错误?
If you don't know the PDF reference (ISO 32000-1) by heart, you should avoid code like this. 如果您根本不了解PDF参考(ISO 32000-1),则应避免使用此类代码。 Instead you can use objects such as
Paragraph
, List
, PdfPTable
... The beauty of adding these objects to a Document
using the Add()
method, is that a new page gets triggered automagically as soon as a page is full. 相反,您可以使用诸如
Paragraph
, List
, PdfPTable
之类的对象...使用Add()
方法将这些对象添加到Document
PdfPTable
是,只要页面已满,就会自动触发新页面。 You can also trigger a new page yourself using: 您还可以使用以下方法自己触发新页面:
document.NewPage();
If you want to add text at absolute positions, iText offers convenience methods and objects such as ColumnText
. 如果要在绝对位置添加文本,则iText提供便捷的方法和对象,例如
ColumnText
。 See my answer to Adding footer to existing PDF 查看我对将页脚添加到现有PDF的回答
You can define a Rectangle
and add objects such as Paragraph
, List
, PdfPTable
... Take a look at the answer to the question How to continue an ordered list on a second page? 您可以定义一个
Rectangle
并添加诸如Paragraph
, List
, PdfPTable
等对象。看一看问题的答案如何在第二页上继续一个有序列表? for inspiration: 灵感:
ColumnText ct = new ColumnText(cb);
ct.AddElement(list);
Rectangle rect = new Rectangle(36, 36, 559, 806);
ct.SetSimpleColumn(rect);
int status = ct.Go();
while (ColumnText.HasMoreText(status)) {
document.NewPage();
ct.SetSimpleColumn(rect);
ct.Go();
}
The while
loop is the loop you are looking for. while
循环是您要查找的循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.