Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

c# - How to give an absolute position to a table in iTextsharp

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;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...