问题
Headers 本身位于第一页 . 如果我的身体中有太多人用于照片,它将在第二页上打印 . 请参阅下面的我的问题的图形说明:
现在,请阅读下面的一些代码 . 请将以下内容视为伪代码(因为整个代码太长),但它非常接近我要修复的代码 .
代码
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);
Footer
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();
Code Wrap Up
虽然可以创建三个对象cell1,cell2和cell2到表 .
table.Add(cell1);
table.Add(cell2);
table.Add(cell3);
尝试
-
使用语法
pdfPTableMain.KeepTogether = true
-
使用
skipFirstHeader(true)
-
我应该将其定义为
table1.add(table2)
,其中table2包含3个单元格? (更新:我试过这个 . 它不起作用) .
我认为发生的事情是,如果我的cell2变得太大,它只会把它放在另一页上 . 我想的方式是我有3行而第2行太大而不适合(因此它将它放在另一页上) . 我不是itext或itextsharp的专家,非常感谢任何帮助 . 我尝试过其他一些东西,但这似乎只会让情况变得更糟 . 我谦卑地联系更有经验的人并寻求帮助 .
1 回答
你链接的问题是正确的,但措辞有点奇怪,因为它说
PdfPTable
类不支持列和行 Span ,这是真的 . 但是PdfPCell
类实际上支持Colspan
,这就是你需要的 .以下是在桌面上的文件夹中创建20个PDF的示例 . 第一个文件只有1张照片,第二个文件只有2张,依此类推,只是为了显示各种可能的迭代 . 你的桌面上需要一张名为“photo.jpg”的照片才能正常工作 . 有关更多详细信息,请参阅代码中的注释 .