Table 在 iTextSharp 中没有按要求分页?
Table in iTextSharp is not page breaking as desired?
问题
header 单独在第一页上。如果我的 body 里面的人太多而无法拍照,它会打印在第二页上。请参阅下面的图形描述我的问题:
现在,请阅读下面的一些代码。请将以下内容视为伪代码(因为整个代码太长),但它非常接近我要修复的代码。
代码
Header
System.IO.FileInfo tempFileInfo
= new System.IO.FileInfo(System.IO.Path.GetTempFileName());
tempFileInfo.MoveTo(tempFileInfo.FullName + ".pdf");
Document document = new Document(PageSize._11X17);
PdfWriter.GetInstance(document, new System.IO.FileStream(tempFileInfo.FullName, System.IO.FileMode.Create));
document.AddTitle("Sample ");
document.Open();
document.SetMargins(0.5f, 0.5f, 0.5f, 0.5f);
PdfPTable pdfPTableMain = new PdfPTable(1); // 1 columns
//Page Title
iTextSharp.text.Font titleFont = iTextSharp.text.FontFactory.GetFont("Arial", 20, iTextSharp.text.Font.BOLD);
iTextSharp.text.Paragraph titleParagraph = new Paragraph(terminalName + "\n\n"+ "Authorized Personnel", titleFont);
PdfPCell cellTitle = new PdfPCell(titleParagraph);
cellTitle.BorderColor = BaseColor.WHITE;
cellTitle.NoWrap = true;
cellTitle.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
cellTitle.PaddingBottom = 10;
pdfPTableMain.AddCell(cellTitle);
Body
PdfPTable pdfPTableList = new PdfPTable(6);
SqlConnection sqlConnection= Sample.Library.DB_Connection.OpenDatabase();
foreach (DataRow row in personDataSet.Tables[0].Rows)
{
PdfPTable pdfPTablePhoto = new PdfPTable(1);
iTextSharp.text.Image image =
iTextSharp.text.Image.GetInstance(GetPersonPhoto(personId),System.Drawing.Imaging.ImageFormat.Png);
PdfPCell cellFullName = new PdfPCell(new Paragraph(fullName, fullnameFont));
cellFullName.BorderColor = BaseColor.WHITE;
cellFullName.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
pdfPTablePhoto.AddCell(cellFullName);
pdfPTableList.AddCell(pdfPTablePhoto);
}
pdfPTableList.CompleteRow();
pdfPTableMain.AddCell(pdfPTableList);
页脚
iTextSharp.text.Font footerFont1 = iTextSharp.text.FontFactory.GetFont("Arial", 10, iTextSharp.text.Font.BOLD);
iTextSharp.text.Paragraph footerParagraph1 = new Paragraph(
"POSTER GENERATED: " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(), footerFont1);
PdfPCell cellFooter1 = new PdfPCell(footerParagraph1);
cellFooter1.BorderColor = BaseColor.WHITE;
cellFooter.DisableBorderSide(Left);
cellFooter.DisableBorderSide(Right);
cellFooter.DisableBorderSide(Bottom);
cellFooter.DisableBorderSide(Top);
cellFooter1.NoWrap = true;
cellFooter1.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
cellFooter1.PaddingBottom = 5;
pdfPTableMain.AddCell(cellFooter1);
pdfPTableMain.KeepTogether = true;
document.Add(pdfPTableMain);
document.Close();
代码总结
它可以看作是创建三个 objects cell1、cell2 和 cell2 到一个 table。
table.Add(cell1);
table.Add(cell2);
table.Add(cell3);
尝试
- 使用语法
pdfPTableMain.KeepTogether = true
- 使用
skipFirstHeader(true)
- 我应该将其定义为一个
table1.add(table2)
,其中 table2 包含 3 个单元格吗? (更新:我试过这个。它没有用)。
我认为发生的情况是,如果我的 cell2 变得太大,它只会将其放在另一个页面上。我的想法是我有 3 行,第二行太大放不下(所以它放在另一页上)。我不是 itext 或 itextsharp 方面的专家,非常感谢任何帮助。我尝试了一些其他的东西,但这似乎只会让事情变得更糟。我谦虚地联系更有经验的人并寻求帮助。
您链接到的问题是正确的,但措辞有点奇怪,因为它说 PdfPTable
class 不支持列和行跨度,这是事实。然而 PdfPCell
class 才是真正支持 Colspan
的东西,这正是你所需要的。
下面是一个在桌面文件夹中创建 20 个 PDF 的示例。第一个文件只有 1 张照片,第二个文件有 2 张照片,依此类推,只是为了展示各种可能的迭代。您的桌面上需要一张名为 "photo.jpg" 的照片才能正常工作。详情请见代码中的注释。
//Just a sample photo
var photoPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "photo.jpg");
//Folder to export to
var exportFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Table Test");
//Create our export folder if it doesn't exist already
System.IO.Directory.CreateDirectory(exportFolder);
//We're going to make 20 different files to show off 20 different
//possible counts of people, each file will be named test_{count}.pdf
var exportFilePathFormat = System.IO.Path.Combine(exportFolder, "test_{0}.pdf");
//Do the PDF creation steps 20 times
for (var i = 1; i <= 20; i++) {
//Get our specific file name for this loop
var thisFile = string.Format(exportFilePathFormat, i);
//Standard iTextSharp bootstrapping, nothing special here
using (var fs = new FileStream(thisFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (var doc = new Document()) {
using (var writer = PdfWriter.GetInstance(doc, fs)) {
//Open the document for writing
doc.Open();
//Create a two column table
var t = new PdfPTable(2);
//Create our header cell
var headerCell = new PdfPCell(new Phrase("This is my header"));
//Span it two columns
headerCell.Colspan = 2;
//Add it to the document
t.AddCell(headerCell);
//Create between 1 and 20 image cells
for (var j = 1; j <= i; j++) {
t.AddCell(iTextSharp.text.Image.GetInstance(photoPath));
}
//Just in case we have an odd number of images, complete the row
t.CompleteRow();
//Footer is same as header
var footerCell = new PdfPCell(new Phrase("This is my footer"));
footerCell.Colspan = 2;
t.AddCell(footerCell);
//Add the table to the document
doc.Add(t);
doc.Close();
}
}
}
}
问题
header 单独在第一页上。如果我的 body 里面的人太多而无法拍照,它会打印在第二页上。请参阅下面的图形描述我的问题:
现在,请阅读下面的一些代码。请将以下内容视为伪代码(因为整个代码太长),但它非常接近我要修复的代码。
代码
Header
System.IO.FileInfo tempFileInfo
= new System.IO.FileInfo(System.IO.Path.GetTempFileName());
tempFileInfo.MoveTo(tempFileInfo.FullName + ".pdf");
Document document = new Document(PageSize._11X17);
PdfWriter.GetInstance(document, new System.IO.FileStream(tempFileInfo.FullName, System.IO.FileMode.Create));
document.AddTitle("Sample ");
document.Open();
document.SetMargins(0.5f, 0.5f, 0.5f, 0.5f);
PdfPTable pdfPTableMain = new PdfPTable(1); // 1 columns
//Page Title
iTextSharp.text.Font titleFont = iTextSharp.text.FontFactory.GetFont("Arial", 20, iTextSharp.text.Font.BOLD);
iTextSharp.text.Paragraph titleParagraph = new Paragraph(terminalName + "\n\n"+ "Authorized Personnel", titleFont);
PdfPCell cellTitle = new PdfPCell(titleParagraph);
cellTitle.BorderColor = BaseColor.WHITE;
cellTitle.NoWrap = true;
cellTitle.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
cellTitle.PaddingBottom = 10;
pdfPTableMain.AddCell(cellTitle);
Body
PdfPTable pdfPTableList = new PdfPTable(6);
SqlConnection sqlConnection= Sample.Library.DB_Connection.OpenDatabase();
foreach (DataRow row in personDataSet.Tables[0].Rows)
{
PdfPTable pdfPTablePhoto = new PdfPTable(1);
iTextSharp.text.Image image =
iTextSharp.text.Image.GetInstance(GetPersonPhoto(personId),System.Drawing.Imaging.ImageFormat.Png);
PdfPCell cellFullName = new PdfPCell(new Paragraph(fullName, fullnameFont));
cellFullName.BorderColor = BaseColor.WHITE;
cellFullName.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
pdfPTablePhoto.AddCell(cellFullName);
pdfPTableList.AddCell(pdfPTablePhoto);
}
pdfPTableList.CompleteRow();
pdfPTableMain.AddCell(pdfPTableList);
页脚
iTextSharp.text.Font footerFont1 = iTextSharp.text.FontFactory.GetFont("Arial", 10, iTextSharp.text.Font.BOLD);
iTextSharp.text.Paragraph footerParagraph1 = new Paragraph(
"POSTER GENERATED: " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString(), footerFont1);
PdfPCell cellFooter1 = new PdfPCell(footerParagraph1);
cellFooter1.BorderColor = BaseColor.WHITE;
cellFooter.DisableBorderSide(Left);
cellFooter.DisableBorderSide(Right);
cellFooter.DisableBorderSide(Bottom);
cellFooter.DisableBorderSide(Top);
cellFooter1.NoWrap = true;
cellFooter1.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
cellFooter1.PaddingBottom = 5;
pdfPTableMain.AddCell(cellFooter1);
pdfPTableMain.KeepTogether = true;
document.Add(pdfPTableMain);
document.Close();
代码总结
它可以看作是创建三个 objects cell1、cell2 和 cell2 到一个 table。
table.Add(cell1);
table.Add(cell2);
table.Add(cell3);
尝试
- 使用语法
pdfPTableMain.KeepTogether = true
- 使用
skipFirstHeader(true)
- 我应该将其定义为一个
table1.add(table2)
,其中 table2 包含 3 个单元格吗? (更新:我试过这个。它没有用)。
我认为发生的情况是,如果我的 cell2 变得太大,它只会将其放在另一个页面上。我的想法是我有 3 行,第二行太大放不下(所以它放在另一页上)。我不是 itext 或 itextsharp 方面的专家,非常感谢任何帮助。我尝试了一些其他的东西,但这似乎只会让事情变得更糟。我谦虚地联系更有经验的人并寻求帮助。
您链接到的问题是正确的,但措辞有点奇怪,因为它说 PdfPTable
class 不支持列和行跨度,这是事实。然而 PdfPCell
class 才是真正支持 Colspan
的东西,这正是你所需要的。
下面是一个在桌面文件夹中创建 20 个 PDF 的示例。第一个文件只有 1 张照片,第二个文件有 2 张照片,依此类推,只是为了展示各种可能的迭代。您的桌面上需要一张名为 "photo.jpg" 的照片才能正常工作。详情请见代码中的注释。
//Just a sample photo
var photoPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "photo.jpg");
//Folder to export to
var exportFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Table Test");
//Create our export folder if it doesn't exist already
System.IO.Directory.CreateDirectory(exportFolder);
//We're going to make 20 different files to show off 20 different
//possible counts of people, each file will be named test_{count}.pdf
var exportFilePathFormat = System.IO.Path.Combine(exportFolder, "test_{0}.pdf");
//Do the PDF creation steps 20 times
for (var i = 1; i <= 20; i++) {
//Get our specific file name for this loop
var thisFile = string.Format(exportFilePathFormat, i);
//Standard iTextSharp bootstrapping, nothing special here
using (var fs = new FileStream(thisFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
using (var doc = new Document()) {
using (var writer = PdfWriter.GetInstance(doc, fs)) {
//Open the document for writing
doc.Open();
//Create a two column table
var t = new PdfPTable(2);
//Create our header cell
var headerCell = new PdfPCell(new Phrase("This is my header"));
//Span it two columns
headerCell.Colspan = 2;
//Add it to the document
t.AddCell(headerCell);
//Create between 1 and 20 image cells
for (var j = 1; j <= i; j++) {
t.AddCell(iTextSharp.text.Image.GetInstance(photoPath));
}
//Just in case we have an odd number of images, complete the row
t.CompleteRow();
//Footer is same as header
var footerCell = new PdfPCell(new Phrase("This is my footer"));
footerCell.Colspan = 2;
t.AddCell(footerCell);
//Add the table to the document
doc.Add(t);
doc.Close();
}
}
}
}