ITextSharp PDFPtable PDF创建多个标题应该只有1 [英] ITextSharp PDFPtable PDF creation multiple headers should only be 1
本文介绍了ITextSharp PDFPtable PDF创建多个标题应该只有1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我目前正在使用ItextSharp来创建PDF文件。我正在制作一个标题以及下表。该表没有任何问题,但是当标题运行时,它会创建4个标题的副本。我一直在尝试并努力解决这个问题,但到目前为止,我在多个小时后都没有运气。
Hello all,
I am currently using ItextSharp to create a PDF file. I am working on generating a header along with a table below. The table works without any problems but when the header runs it creates a duplicate of 4 headers. I have been experimenting and working on solving this problem but so far I have had no luck after multiple hours.
class PrintForAccess {
Document printAccess2CareDoc;
const int LINES_PER_ACCESS2CARE_DOC = 5;
int pageCount = 0;
PdfWriter writer;
public void PrintAccessPDF(IEnumerable<ride> records, DateTime ridedate, int driverid, bool groupByDriver, string username, Stream outputstream, int tenantid, bool splitByDriver = false)
{
printAccess2CareDoc = new iTextSharp.text.Document(PageSize.LETTER.Rotate(), .2f, .2f, .2f, .2f);
writer = PdfWriter.GetInstance(printAccess2CareDoc, outputstream);
PdfAcroForm acroForm = writer.AcroForm;
printAccess2CareDoc.Open();
PdfPTable table = printAccess2CareInitTable(defaultFont);
PdfPTable headTable = printHeadTableAccess2Care(headerFont);
int detailLine = 0;
driver previousDriver = null;
bool firstRec = true;
foreach(ride record in records)
{
if(groupByDriver && record.driver != previousDriver || firstRec)
{
previousDriver = record.driver;
detailLine = 0;
printAccess2CareNewPage(ref table, ref headTable, defaultFont, headerFont, firstRec);
}
printAccess2CareHeader(headTable, record, ridedate);
printAccess2CareDetail(table, defaultFont, writer, record, ridedate);
if((++detailLine) == LINES_PER_ACCESS2CARE_DOC)
{
detailLine = 0;
printAccess2CareNewPage(ref table, ref headTable, defaultFont, headerFont, false);
}
firstRec = false;
}
printAccess2CareFooter(defaultFont);
printAccess2CareDoc.Add(table);
if (!splitByDriver)
printAccess2CareDoc.Close();
else
printAccess2CareDoc.CloseDocument();
return;
}
private void printAccess2CareNewPage(ref PdfPTable table, ref PdfPTable headTable, iTextSharp.text.Font defaultFont, iTextSharp.text.Font headerFont, bool suppressFooter)
{
if(!suppressFooter)
{
printAccess2CareDoc.Add(headTable);
printAccess2CareDoc.Add(table);
printAccess2CareFooter(defaultFont);
printAccess2CareDoc.NewPage();
}
headTable = printHeadTableAccess2Care(headerFont);
table = printAccess2CareInitTable(defaultFont);
}
private PdfPTable printHeadTableAccess2Care(iTextSharp.text.Font headfont) {
const int COLUMNS = 3;
PdfPTable table = new PdfPTable(COLUMNS);
table.DefaultCell.Phrase = new Phrase("", headfont);
table.DefaultCell.Border = Rectangle.NO_BORDER;
table.TotalWidth = 760f;
table.LockedWidth = true;
float[] widths = new float[] { 250f, 250f, 260f };
table.SetWidths(widths);
table.SpacingBefore = 1f;
return table;
}
private void printAccess2CareHeader(PdfPTable table, ride ride, DateTime rideDate)
{
BaseFont times = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
float titleFontSize = 10;
var boldHeaderFont = new iTextSharp.text.Font(times, titleFontSize, iTextSharp.text.Font.BOLD);
var headerFont = new iTextSharp.text.Font(times, titleFontSize);
var largeTitleFont = new iTextSharp.text.Font(times, 15, iTextSharp.text.Font.BOLD);
PdfPTable headerTable1 = new PdfPTable(2);// (6);
PdfPCell c1r1head = new PdfPCell(new Phrase("Provider: ", boldHeaderFont));
c1r1head.PaddingTop = 5f;
c1r1head.BorderWidth = 0f;
c1r1head.PaddingLeft = -30f;
PdfPCell c2r1head = new PdfPCell(new Phrase(/*strTenant*/"", headerFont));
c2r1head.PaddingTop = 5f;
c2r1head.PaddingLeft = -60f;
c2r1head.BorderWidth = 0f;
PdfPCell c3r1head = new PdfPCell(new Phrase("Date Of Service: ", boldHeaderFont));
c3r1head.PaddingTop = 5f;
c3r1head.PaddingLeft = -30f;
c3r1head.BorderWidth = 0f;
PdfPCell c4r1head = new PdfPCell(new Phrase(/*strDateToday*/"", headerFont));
c4r1head.PaddingTop = 5f;
c4r1head.PaddingLeft = -50f;
c4r1head.BorderWidth = 0f;
PdfPCell c5r1head = new PdfPCell(new Phrase("Check In____:____AM PM", headerFont));
c5r1head.PaddingTop = 5f;
c5r1head.PaddingLeft = -30f;
c5r1head.BorderWidth = 0f;
PdfPCell c6r1head = new PdfPCell(new Phrase("Check Out____:____AM PM", headerFont));
c6r1head.PaddingLeft = -10f;
c6r1head.PaddingRight = -20f;
c6r1head.PaddingTop = 5f;
c6r1head.BorderWidth = 0f;
//row 2
PdfPTable headerTable2 = new PdfPTable(6);
PdfPCell c1r2head = new PdfPCell(new Phrase("Vehicle/VIN: ", boldHeaderFont));
c1r2head.PaddingTop = 4f;
c1r2head.PaddingLeft = -30f;
c1r2head.BorderWidth = 0f;
PdfPCell c2r2head = new PdfPCell(new Phrase("vehicle# " /*+ vinLast4digits*/, headerFont));
c2r2head.PaddingLeft = -60f;
c2r2head.BorderWidth = 0f;
PdfPCell c3r2head = new PdfPCell(new Phrase("Driver Name: ", boldHeaderFont));
c3r2head.PaddingLeft = -30f;
c3r2head.BorderWidth = 0f;
PdfPCell c4r2head = new PdfPCell(new Phrase(/*record.driver.person.fullname*/"", headerFont));
c4r2head.PaddingLeft = -50f;
c4r2head.BorderWidth = 0f;
//PdfPCell c5r2head = new PdfPCell(new Phrase("", boldHeaderFont));
//row 3
PdfPTable headerTable3 = new PdfPTable(6);
PdfPCell c1r3head = new PdfPCell(new Phrase("Driver Signature: ", boldHeaderFont));
//c1r3head.PaddingTop = 10f;
c1r3head.PaddingLeft = -30f;
c1r3head.BorderWidth = 0f;
PdfPCell c2r3head = new PdfPCell(new Phrase(""));
c2r3head.BorderWidth = 0f;
PdfPCell c4r3head = new PdfPCell(new Phrase("Access2Care", largeTitleFont));
c4r3head.Colspan = 2;
c4r3head.Border = 0;
c4r3head.PaddingTop = 2f;
PdfPCell c5r3head = new PdfPCell(new Phrase("Check In____:____AM PM", headerFont));
//c5r3head.PaddingRight = 20f;
c5r3head.PaddingTop = 5f;
c5r3head.PaddingLeft = -30f;
c5r3head.BorderWidth = 0f;
PdfPCell c6r3head = new PdfPCell(new Phrase("Check Out____:____AM PM", headerFont));
c6r3head.PaddingRight = -20f;
c6r3head.PaddingTop = 5f;
c6r3head.PaddingLeft = -10f;
c6r3head.BorderWidth = 0f;
headerTable1.AddCell(c4r3head);
headerTable1.AddCell(c1r2head);
headerTable1.AddCell(c2r2head);
headerTable1.AddCell(c1r1head);
headerTable1.AddCell(c2r1head);
headerTable2.AddCell(c3r1head);
headerTable2.AddCell(c4r1head);
headerTable2.AddCell(c3r2head);
headerTable2.AddCell(c4r2head);
headerTable2.AddCell(c1r3head);
headerTable2.AddCell(c2r3head);
headerTable3.AddCell(c5r1head);
headerTable3.AddCell(c6r1head);
headerTable3.AddCell(c2r3head);
headerTable3.AddCell(c2r3head);
headerTable3.AddCell(c5r3head);
headerTable3.AddCell(c6r3head);
table.AddCell(headerTable1);
table.AddCell(headerTable2);
table.AddCell(headerTable3);
}
推荐答案
这是我的问题的答案
我终于解决了!!
Here is the answer to my question
I finally solved it!!
foreach(ride record in records)
{
if(groupByDriver && record.driver != previousDriver || firstRec)
{
if(!firstRec)
{
printAccessFivelengths(ref table, defaultFont, detailLine, headTable, headerFont);
}
previousDriver = record.driver;
if (record.driver != null && (driverid != 0 || groupByDriver))
{
//pageHeaderWithDriver.Add(new Phrase(titlec1 + record.driver.person.fullname, headerFont));
c4r2head = new PdfPCell(new Phrase(record.driver.person.fullname, headerFont));
headerTable.AddCell(c4r2head);
}
detailLine = 0;
printAccessHeader(headTable, record, ridedate, username);
printAccessNewPage(ref table, ref headTable, defaultFont, headerFont, firstRec);
table = printAccessInitTable(defaultFont);
headTable = printAccessHeaderInit(headerFont);
}
printAccessDetail(table, defaultFont, writer, record, ridedate);
if((++detailLine) == LINES_PER_ACCESS_DOC)
{
detailLine = 0;
//print new page
printAccessHeader(headTable, record, ridedate, username);
printAccessNewPage(ref table, ref headTable, defaultFont, headerFont, false);
}
firstRec = false;
}
这篇关于ITextSharp PDFPtable PDF创建多个标题应该只有1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文