How to give an absolute position to a table in iTe

2019-09-22 06:01发布

问题:

I am using a iTextsharp to create a table.I want a table at particular position in the page and I am using a table.WriteSelectedRows(0, -1, 300, 300, pcb); method but its not working. Here is my code.

           using (FileStream fs = new FileStream("TableTest.pdf", FileMode.Create))
           {
            Document doc = new Document(PageSize.A4);
            PdfWriter writer =  PdfWriter.GetInstance(doc, fs);
            doc.Open();
            PdfContentByte pcb = writer.DirectContent;
            PdfPTable table = new PdfPTable(9);
            table.TotalWidth = 595f;

            // Hdeaer row.
            table.AddCell(GetCell("Header 1", 1, 2));
            table.AddCell(GetCell("Header 2", 1, 2));
            table.AddCell(GetCell("Header 3", 5, 1));
            table.AddCell(GetCell("Header 4", 1, 2));
            table.AddCell(GetCell("Header 5", 1, 2));

            // Inner middle row.
            table.AddCell(GetCell("H1"));
            table.AddCell(GetCell("H2"));
            table.AddCell(GetCell("H3"));
            table.AddCell(GetCell("H4"));
            table.AddCell(GetCell("H5"));

            // Bottom row.
            table.AddCell(GetCell("D1"));
            table.AddCell(GetCell("D2"));
            table.AddCell(GetCell("D3"));
            table.AddCell(GetCell("D4"));
            table.AddCell(GetCell("D5"));
            table.AddCell(GetCell("D6"));
            table.AddCell(GetCell("D7"));
            table.AddCell(GetCell("D8"));
            table.AddCell(GetCell("D9"));
            table.WriteSelectedRows(0, -1, 300, 300, pcb);
            doc.Close();
   }
   private PdfPCell GetCell(string text)
    {
        return GetCell(text, 1, 1);
    }
    private PdfPCell GetCell(string text, int colSpan, int rowSpan)
    {
        PdfPCell cell = new PdfPCell(new Phrase(text));
        cell.HorizontalAlignment = 1;
        cell.Rowspan = rowSpan;
        cell.Colspan = colSpan;

        return cell;
    }

回答1:

You try to write the table before you've added any content to the table. It is normal that nothing is shown on the page. Move writeSelectedRows() down in your code:

PdfPTable table = new PdfPTable(9);
table.TotalWidth = 595;

// there isn't any content in the table: there's nothing to write yet

// Header row.
table.AddCell(GetCell("Header 1", 1, 2));
table.AddCell(GetCell("Header 2", 1, 2));
table.AddCell(GetCell("Header 3", 5, 1));
table.AddCell(GetCell("Header 4", 1, 2));
table.AddCell(GetCell("Header 5", 1, 2));

// Inner middle row.
table.AddCell(GetCell("H1"));
table.AddCell(GetCell("H2"));
table.AddCell(GetCell("H3"));
table.AddCell(GetCell("H4"));
table.AddCell(GetCell("H5"));

// Now we've added 2 rows: two rows will be shown:

table.WriteSelectedRows(0, -1, 300, 300, pcb);

Note that I've changed the number of columns in the table, because you're only adding 5 rows for the first row, and five rows for the second row. Note that incomplete rows aren't rendered, so that may be another reason why your code doesn't do what you expect.

Finally you should remove:

doc.Add(table);

This adds the table at the current position of the cursor in the page. That's probably not where you want it.



回答2:

You say its not working but did not explain what isn't working.

Thus, I ran your program to compare the output with your code. The result:

Considering that your code explicitly positions a table as wide as an A4 page

table.TotalWidth = 595f;

at a position horizontally located in mid page

table.WriteSelectedRows(0, -1, 300, 300, pcb);

this exactly is what was to be expected. Thus, it is working!


If your original intention was to have the table start horizontally at mid-page and extend to the right page border, you should have set the table width to the page width minus your center x, i.e.

table.TotalWidth = 295f;

If on the other hand you wanted the table start at the left page border and extend to the right one, you should have position at x = 0:

table.WriteSelectedRows(0, -1, 0, 300, pcb);


标签: c# itext