user7135817
user7135817

Reputation:

Generate a table and append it to a pdf using c#?

I have two parts to my c# project for my company and my company wants me to generate a pdf file from a csv.

I need to populate the fields of a pdf
I need to add a table below the populated section on the blank area of the page (and this table needs to be able to rollover to the next page).

I am able to do these things separately (populate the pdf and create a table). But I cannot effectively merge them. I have tried doing a doc.add(table) which will result in the table being on the next page of the pdf, which I don't want.

I essentially just need to be able to specify where the table starts on the page (so it wouldn't overlap the existing content) and then stamp the table onto the existing pdf.

My other option if this doesn't work is trying to add fields to the original pdf that will be filled by the table contents (so it will instead be a field-based table).

Any suggestions?

EDIT:

I'm new to itext and have not used columntext before, but I'm trying to test it out in the following code but the table is not being displayed. I looked at other columntext examples and I have not seen exactly where the columntext is added back into the pdf.

    //CREATE FILLED FORM PDF
PdfReader reader = new PdfReader(sourcePath);       
PdfStamper pdfStamper = new PdfStamper(reader, new FileOutputStream(destPath));
pdfStamper.setFormFlattening(true);
AcroFields form = pdfStamper.getAcroFields();

form.setField("ID", "99999");
form.setField("ADDR1", "425 Test Street");
form.setField("ADDR2", "Test, WA 91334");
form.setField("PHNBR", "(999)999-9999");
form.setField("NAME", "John Smith");

//CREATE TABLE
PdfPTable table = new PdfPTable(3);
Font bfBold12 = new Font(FontFamily.HELVETICA, 12, Font.BOLD, new BaseColor(0, 0, 0)); 
insertCell(table, "Table", Element.ALIGN_CENTER, 1, bfBold12);
table.completeRow(); 

ColumnText column = new ColumnText(pdfStamper.getOverContent(1));
column.addElement(table);

pdfStamper.close();
reader.close();

Upvotes: 1

Views: 1608

Answers (2)

COeDev
COeDev

Reputation: 356

Either your CSV data is invalid (not well formatted, e.g. with additional linebreaks or commas) or you are messing up your data with this "\n-1" string building. (your console output varies from 8 to 9 lines per dataset)

For debugging, skip building this "content" string and write the data directly from a CSV line to the PDF cells, like:

Document doc = new Document();
PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("file6.pdf"), FileMode.Create));
doc.Open();
PdfPTable table = new PdfPTable(7);

using (TextFieldParser parser = new TextFieldParser("t.csv"))
{
    parser.TextFieldType = FieldType.Delimited;
    parser.SetDelimiters(",");
    while (!parser.EndOfData)
    {
        //Processing row
        string[] fields = parser.ReadFields();
        table.AddCell(new PdfPCell(new Paragraph(field[2]));
        table.AddCell(new PdfPCell(new Paragraph(field[3]));
        table.AddCell(new PdfPCell(new Paragraph(field[4]));
        //......
    }
}
doc.Add(table);
doc.Close();

Upvotes: 1

jjj
jjj

Reputation: 1154

you are adding Cell to table directly , rather then you could set proper width of cells then Create cells using your data and then using WriteSelectedRows you could right row. Sample code below for reference only.

        var table = PdfWriter.CreateTable(9);
        table.SetWidths(new[] { 0.3f, 1.7f, 3f, 1f, 1f, 1f, 1f, 1f, 1f });

        var cells =
            from line in lines
            let values = new[]
            {
                new { Text = string.Empty, ColSpan = 1, Align = "<set value>" },
                new { Text = line.Description, ColSpan = 6, Align = "<set value>" },
                new { Text = string.Empty, ColSpan = 1, Align = "<set value>" },
                new { Text = FormatDecimal.Format(Math.Abs(line.Amount)), ColSpan = 1, Align = "<set value>"}
            }
            from value in values
            select new Cell
            {
                Grey = true,
                FontSize = "<set value>",
                HorizontalAlignment = value.Align,
                ColSpan = value.ColSpan,
                Text = value.Text
            };

        cells.ForEach(table.AddCell);

        var position = new Position(PdfWriter.DocLeft, PdfWriter.DocTop - 20 - 245);
        table.TotalWidth = PdfWriter.DocRight - PdfWriter.DocRightMargin;
        table.LockedWidth = "<set value>";
        table.WriteSelectedRows(0, -1, position.HorizontalPosition, position.VerticalPosition, Writer.DirectContent);

Upvotes: 0

Related Questions