Reputation: 121
I have created a pdf file that has graphics on it, now am trying to add a table under those graphics. My problem is the table is over the graphics, how do i specify the location/position where i want my table to be placed on the pdf document??
This is my code
docl.Open();
docl.Add(new Paragraph("My first PDF file"));
PdfContentByte cb = writer.DirectContent;
//employee
// position y,position x,length,height, unknown
cb.RoundRectangle( 20f, 745f, 200f, 35f, 10f);
//title
cb.RoundRectangle(235f, 745f, 35f, 35f, 10f);
//identity number
cb.RoundRectangle(280f, 745f, 105f, 35f, 10f);
//date of birth
cb.RoundRectangle(390f, 745f, 105f, 35f, 10f);
//employee number
cb.RoundRectangle(500f, 745f, 105f, 35f, 10f);
//address
cb.RoundRectangle(20f, 660f, 200f, 80f, 10f);
//pay method
cb.RoundRectangle(235f, 700f, 35f, 35f, 10f);
//brantch code
cb.RoundRectangle(235f, 660f, 35f, 35f, 10f);
//bank
cb.RoundRectangle(280f, 700f, 215f, 35f, 10f);
//account type
cb.RoundRectangle(500f, 700f, 105f, 35f, 10f);
//account number
cb.RoundRectangle(280f, 660f, 160f, 35f, 10f);
//pay point
cb.RoundRectangle(445f, 660f, 35f, 35f, 10f);
//date of payment
cb.RoundRectangle(506f, 660f, 90f, 35f, 10f);
//marital status
cb.RoundRectangle(20f, 600f, 35f, 35f, 10f);
//gender
cb.RoundRectangle(60f, 600f, 35f, 35f, 10f);
//date of appointment
cb.RoundRectangle(100f, 600f, 70f, 35f, 10f);
//Tax number
cb.RoundRectangle(175f, 600f, 70f, 35f, 10f);
cb.Stroke();
PdfPTable table = new PdfPTable(2);
table.HorizontalAlignment = 0;
table.SetTotalWidth(new float[] { 800, 200 });
PdfPCell cell = new PdfPCell(new Phrase("EARNINGS"));
cell.Colspan = 2;
cell.HorizontalAlignment = 1;
table.AddCell(cell);
table.AddCell("Description");
table.AddCell("Amount");
I used this line to specify the position of the graphics on the document: // position y, position x,length,height, unknown cb.RoundRectangle( 20f, 745f, 200f, 35f, 10f);
I want to place the table below the graphics.
Upvotes: 5
Views: 17841
Reputation: 63
private static void DemoTableSpacing() {
using (FileStream fs = new FileStream("SpacingTest.pdf", FileMode.Create)) {
Document doc = new Document();
PdfWriter.GetInstance(doc, fs);
doc.Open();
Paragraph paragraphTable1 = new Paragraph();
paragraphTable1.SpacingAfter = 15f;
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Phrase("This is table 1"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1;
table.AddCell(cell);
table.AddCell("Col 1 Row 1");
table.AddCell("Col 2 Row 1");
table.AddCell("Col 3 Row 1");
//table.AddCell("Col 1 Row 2");
//table.AddCell("Col 2 Row 2");
//table.AddCell("Col 3 Row 2");
paragraphTable1.Add(table);
doc.Add(paragraphTable1);
Paragraph paragraphTable2 = new Paragraph();
paragraphTable2.SpacingAfter = 10f;
table = new PdfPTable(3);
cell = new PdfPCell(new Phrase("This is table 2"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1;
table.AddCell(cell);
table.AddCell("Col 1 Row 1");
table.AddCell("Col 2 Row 1");
table.AddCell("Col 3 Row 1");
table.AddCell("Col 1 Row 2");
table.AddCell("Col 2 Row 2");
table.AddCell("Col 3 Row 2");
paragraphTable2.Add(table);
doc.Add(paragraphTable2);
doc.Close();
}
}
I HAVE USE THIS ONE FOR MY TABLE POSITION IN iTEXTSHARP LINK: https://www.codeproject.com/Questions/351802/Its-possible-put-a-table-in-absolute-position-with
Upvotes: 0
Reputation: 77528
You are mixing the low-level approach (adding content at absolute positions) with the high-level approach (using document.add()
) for page content.
Either you stick to the high-level approach, by using a table to create the round rectangles. You can create tables with borders that have rounded corners using cell and table events. When you use document.add()
, iText will take care of positioning everything (including splitting the table if it doesn't fit the page).
Or you stick to the low-level approach, by adding the table at an absolute position, but be aware that itext won't split the table if it doesn't fit the page.
Take a look at this example: Java | C# | PDF
It shows how to create rounded borders for tables by using cell events and/or table events. See the other examples of chapter 5 for less complex sample code.
As you can see in the calendar example, the table is added at an absolute position using the method table.WriteSelectedRows(...)
. As you know the coordinates of the rounded rectangles, you can use this method to add your table at an absolute position.
Upvotes: 1