使用 iTextSharp 创建多页 pdf
Creating a multiple pages pdf with iTextSharp
我正在尝试使用 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();
请转到 official documentation and click Q&A to go to the most frequently asked questions. Pick the 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
) / SetFontAndSize()
(Tf
)。这是创建 PDF 的艰难方式。如果很容易出错,如本题所示:What is causing syntax errors in a page created with iText?
如果您不记得 PDF 参考 (ISO 32000-1),您应该避免使用这样的代码。相反,您可以使用 Paragraph
、List
、PdfPTable
等对象...使用 Add()
方法将这些对象添加到 Document
的美妙之处在于一旦页面已满,就会自动触发新页面。您也可以使用以下方式自行触发新页面:
document.NewPage();
如果你想在绝对位置添加文本,iText 提供了方便的方法和对象,例如ColumnText
。请参阅我对
的回答
你可以定义一个Rectangle
,然后添加Paragraph
、List
、PdfPTable
等对象……看看问题的答案How to continue an ordered list on a second page? 灵感:
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();
}
while
循环就是您要查找的循环。
我正在尝试使用 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();
请转到 official documentation and click Q&A to go to the most frequently asked questions. Pick the 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
) / SetFontAndSize()
(Tf
)。这是创建 PDF 的艰难方式。如果很容易出错,如本题所示:What is causing syntax errors in a page created with iText?
如果您不记得 PDF 参考 (ISO 32000-1),您应该避免使用这样的代码。相反,您可以使用 Paragraph
、List
、PdfPTable
等对象...使用 Add()
方法将这些对象添加到 Document
的美妙之处在于一旦页面已满,就会自动触发新页面。您也可以使用以下方式自行触发新页面:
document.NewPage();
如果你想在绝对位置添加文本,iText 提供了方便的方法和对象,例如ColumnText
。请参阅我对
你可以定义一个Rectangle
,然后添加Paragraph
、List
、PdfPTable
等对象……看看问题的答案How to continue an ordered list on a second page? 灵感:
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();
}
while
循环就是您要查找的循环。