如何使用itextsharp在不同页面上编写和拆分数据表
本文关键字:数据表 拆分 itextsharp 何使用 | 更新日期: 2023-09-27 18:34:21
首先,这是我的代码:
string pdfTemplate = Application.StartupPath + "''Templates''Template Medico.pdf";
string newFile = Application.StartupPath + "''Relatórios''Relatório Médico_" + dataArquivo + ".pdf";
PdfReader pdfReader = new PdfReader(pdfTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
// Form Filling
pdfFormFields.SetField("data", data);
pdfFormFields.SetField("medico_nome", campo);
pdfFormFields.SetField("numero_consultas", numeroConsultas);
// Table Building
int numColumns = ds.Tables[0].Columns.Count;
PdfPTable datatable = new PdfPTable(numColumns);
datatable.DefaultCell.Padding = 10;
datatable.WidthPercentage = 100;
datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
float[] columnWidths = { 80, 80, 80, 80, 80, 80, 80, 80, 80 };
datatable.SetWidths(columnWidths);
float[] totalWidth = { 80, 80, 80, 80, 80, 80, 80, 80, 80 };
datatable.SetTotalWidth(totalWidth);
// Table Header
for (int k = 0; k < ds.Tables[0].Columns.Count; k++)
{
Phrase collumname = new Phrase(ds.Tables[0].Columns[k].ColumnName, FontFactory.GetFont("Verdana", 9));
datatable.AddCell(collumname);
}
// Lines and Columns
if (ds.Tables[0].Rows.Count <= 9) // less than 9 rows = no problem, no new pages and table spliting needed
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
datatable.DefaultCell.BackgroundColor = iTextSharp.text.BaseColor.WHITE;
datatable.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
Phrase phrase = new Phrase(ds.Tables[0].Rows[i][j].ToString(), FontFactory.GetFont("Verdana", 9));
datatable.AddCell(phrase);
}
}
// Write down the table on page 2
PdfContentByte content = pdfStamper.GetUnderContent(2);
datatable.WriteSelectedRows(0, -1, 70.0f, 495.0f, content);
}
else
{
int newPage = 3;
int currentPage = 2;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
datatable.DefaultCell.BackgroundColor = iTextSharp.text.BaseColor.WHITE;
datatable.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
Phrase phrase = new Phrase(ds.Tables[0].Rows[i][j].ToString(), FontFactory.GetFont("Verdana", 9));
datatable.AddCell(phrase);
}
if (i > 0 && i % 9 == 0)
{ //How do i print the table ONLY every 9 iterations? (9 iterations = 9 rows)
PdfContentByte content = pdfStamper.GetUnderContent(currentPage);
// Or how do i make a loop using the rowStart and rowEnd arguments of the
// WriteSelectedRows function in order to write only a set of 9 rows for each page?
datatable.WriteSelectedRows(0, -1, 70.0f, 495.0f, content);
pdfStamper.InsertPage(newPage, pdfReader.GetPageSize(2));
newPage++;
currentPage++;
}
}
}
代码对我目前遇到的主要问题进行了很好的注释,但正如线程标题中所述,真正更大的问题是以某种方式"拆分"或"控制"行和列的循环/迭代以匹配页面,跳转到另一个页面,并继续编写表格。
任何帮助都将不胜感激。
伙计们,花了我 2 天的时间和很多咖啡,但我做到了,这是代码:
else
{
int newPageNumber = 3;
int currentPage = 2;
int lastLinePrinted = 0;
int maxLine = 9;
bool lastPage = false;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
{
datatable.DefaultCell.BackgroundColor = iTextSharp.text.BaseColor.WHITE;
datatable.DefaultCell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_LEFT;
Phrase phrase = new Phrase(ds.Tables[0].Rows[i][j].ToString(), FontFactory.GetFont("Verdana", 9));
datatable.AddCell(phrase);
}
// must decrement the ds.Tables[0].Rows.Count here,
// otherwise it'll never enter the code below, we'll
// increment it back when we reach the last page
if ((i > 0 && i % 9 == 0) || i == ds.Tables[0].Rows.Count - 1)
{
PdfContentByte content = pdfStamper.GetUnderContent(currentPage);
datatable.WriteSelectedRows(lastLinePrinted, maxLine, 70.0f, 495.0f, content);
if (!lastPage)
{
pdfStamper.InsertPage(newPage, pdfReader.GetPageSize(2));
newPage++;
currentPage++;
lastLinePrinted = maxLine;
maxLine += 9;
}
if (maxLine > ds.Tables[0].Rows.Count)
{
maxLine = ds.Tables[0].Rows.Count+1;
lastPage = true;
}
}
}
}
希望它能帮助更多的人:D
完全使用 WriteSelectedRows
可以更轻松地做到这一点
int pageRows=0;
while (pageRows<datatable.Rows.Count+8)
{
content.BeginText();
table.WriteSelectedRows(pageRows, pageRows+9, 20, 550, cb);
pageRows = pageRows + 9;
content.EndText();
document.NewPage();
}
您一次编写 9 行,然后调用一个新页面。