我正在尝试使用iTextSharp创建一个多页的pdf,但是我遇到了一些问题,即创建一个包含多个页面的循环。
下面的代码适用于一页;但是,我的内容不适合单个页面。感谢。
如何编写循环以在第一个pdf页面上写入内容,剩下的内容写入第二个,第三个等等...到目前为止,我只看到一个页面。谢谢。
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();
答案 0 :(得分:1)
请转到official documentation,然后点击Q&A转到最常见的问题。选择Getting started类别。您将看到的第一件事是最受欢迎的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();
在您的代码中,您通过引入PDF语法在绝对位置添加文本,例如BeginText()
(BT
)/ EndText()
(ET
)/ {{1 (SetFontAndSize()
)。这是以艰难的方式创建PDF。如果很容易犯错误,请参阅此问题:What is causing syntax errors in a page created with iText?
如果您不熟悉PDF参考(ISO 32000-1),则应避免使用此类代码。相反,您可以使用Tf
,Paragraph
,List
等对象...使用PdfPTable
方法将这些对象添加到Document
的美妙之处在于一旦页面满了,新页面就会自动触发。您也可以使用以下方式自行触发新页面:
Add()
如果要在绝对位置添加文本,iText会提供方便的方法和对象,例如document.NewPage();
。请参阅我对Adding footer to existing PDF
您可以定义ColumnText
并添加Rectangle
,Paragraph
,List
等对象...查看问题{{3}的答案为了灵感:
PdfPTable
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();
}
循环是您正在寻找的循环。