Reputation: 103
I have the following task to complete: based on a dataset, I have to fill a PDF file's form fields using itextsharp
while also exporting the said dataset on a table "format" so it could go on the second page of said PDF file.
The table will go below the title on the second page wich is landscape-oriented. But I'm also accepting a solution/hint which involves deleting the second page and create-write one by code-side.
This is by far the work I've made
class PDFFormFiller
{
public void FillForm(UInt32 tipo, string campo, string numeroConsultas, DataSet ds)
{
// I use this on another specific case, dont bother with it.
string data = System.DateTime.Today.ToLongDateString();
switch (tipo)
{
case 1: // Medical report
{
// Get the previous created pdf file...
string pdfTemplate = Application.StartupPath + "\\Templates\\Template Medico.pdf";
// ...and generate a new one with the filled forms and the table
string newFile = Application.StartupPath + "\\Relatórios\\Relatório Médico_" + data + ".pdf";
PdfReader pdfReader = new PdfReader(pdfTemplate);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create));
AcroFields pdfFormFields = pdfStamper.AcroFields;
// Form fields filling by the arguments up there, dont bother with'em
pdfFormFields.SetField("data", data);
pdfFormFields.SetField("medico_nome", campo);
pdfFormFields.SetField("numero_consultas", numeroConsultas);
// Table build
int numColumns = ds.Tables[0].Columns.Count;
PdfPTable datatable = new PdfPTable(numColumns);
datatable.DefaultCell.Padding = 0;
datatable.WidthPercentage = 100; // percentage
datatable.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
float[] columnWidths = {20, 20, 20, 20, 40, 20, 20, 20, 20};
datatable.SetWidths(columnWidths);
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);
}
}
// The table code goes here
// Final Message
string relatorio = ("Relatório para o médico" + pdfFormFields.GetField("medico_nome") + "gerado com sucesso na pasta Relatórios");
MessageBox.Show(relatorio);
// Form flattening set to true to avoid
// further edition of the form fields on the file created
pdfStamper.FormFlattening = true;
// Clode the pdf stamper
pdfStamper.Close();
break;
}
Upvotes: 1
Views: 1724
Reputation: 103
Damm, solved with two lines....
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);
}
}
+ PdfContentByte content = pdfStamper.GetUnderContent(2);
+ datatable.WriteSelectedRows(0, -1, 70.0f, 400.0f, content);
Not to say that i had to search the entire internet to sort this thing out..
Upvotes: 2