Reputation: 458
i'm having dynamic rows and coloumns. Columns: it contains company details, Rows: on particular scale. Data: it contains charts which are drawn
Explaination:
grid in my application (lets say application's row a_row and col a_col), where a_col is company names(dynamic and user can add more a_col) and a_row heading contains some metrics.
in the a_row and a_col contains charts (application table format grid contains charts in rowXcol format)
now both a_row and a_col are dynamic, user can add and delete charts and the coloumns.
(rows for pdf abb. as p_row adn col as p_col)
for pdfexport: PdfPTable(3) is what m having fixed, i.e each a_col headers will be in pdftable row(only 3 ) after 3, charts of that a_row will be drawn for those a_col headers.
Application:
COL1 COL2 <--- company name
ROW1 chart00 chart01
ROW2 chart10 chart11
ROW3 chart20 chart21
rows are metrics in application
PDF TO DRAW: (please check image - pdf snapshot)
Code done: its a dynamic as no of charts (row and coloumns), no of rows are the different pages to be drawn. below code is working fine and accurate(as per old needs).
CREATE PDF:
page2 = new Paragraph();
page2.Add(new Chunk(CriteriaAll, CriteriaFONT));
page2.Alignment = Element.ALIGN_CENTER;
newtable();
int j = 0, temp_tablecount = 0, tablecount = 0;
int checkrcnt = (int)(Math.Ceiling(chartnameLIST.Count / 3.0d)) * 3; //chartnameList is total charts rowXcol)
int col_newpg_temp = 1;
int col_newpg = (int)(Math.Ceiling(col_count / 12.0d)); (col_count is no of col.)
for (int row_tb = 0; row_tb < row_count; row_tb++)
{
col_newpg_temp = 1;
newpagedatafeed(pdfDoc, Metric[row_tb]);
newtable();
tablepg1.AddCell(new Phrase(" ")); tablepg1.AddCell(new Phrase(" ")); tablepg1.AddCell(new Phrase(" "));
for (int i = 0; i < checkrcnt; i++)
{
if (i < col_count)
{
if (i == col_newpg_temp * 12)
{
pdfDoc.Add(tablepg1);
newtable();
newpagedatafeed(pdfDoc, Metric[row_tb]);
col_newpg_temp++;
}
tablepg1.AddCell(new Phrase(Companies[i], companiesH));
}
else
tablepg1.AddCell("");
j++;
if (j > 2)
{
j = 0;
for (int k = 0; k < 3; k++)
{
if (temp_tablecount < col_count)
{
tablepg1.AddCell(ClipImpageProcess(chartnameLIST[tablecount], 8, writer));
tablecount++; temp_tablecount++;
}
else
tablepg1.AddCell("");
}
}
}//coll ends
temp_tablecount = 0;
pdfDoc.Add(tablepg1);
}
private static void newtable() //adds a new page
{
tablepg1 = new PdfPTable(3);
tablepg1.DefaultCell.Border = 0;
tablepg1.WidthPercentage = 95;
}
private static void newpagedatafeed(Document pdfDoc, string p) //somethng as a header
{
pdfDoc.NewPage();
DrawLine(pdfDoc, writer.DirectContent);
pdfDoc.Add(page2);
tablepg1.AddCell(new Phrase(" ")); tablepg1.AddCell(new Phrase(" ")); tablepg1.AddCell(new Phrase(" "));
p = "\n" + p;
PdfPTable temptable = new PdfPTable(3);
temptable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;
temptable.DefaultCell.Border = 0;
temptable.AddCell(""); temptable.AddCell(new Phrase(p, MetricH)); temptable.AddCell("");
pdfDoc.Add(temptable);
}
TO do:(new needs)
Need a coding help,
1. If there are only 2 or 3 columns, then the charts should be in the middle of the page
2. since, there are only few charts to be on pdf page, then the next row should not go to the next page, it should be drawn on the same page.
Upvotes: 0
Views: 1172
Reputation: 77528
I'm going to make some assumptions regarding your question. If the assumptions are wrong, you should update your question in order to clarify it.
Please take a look at the CenterVertically example. In this example, I create a cell with five lines of text (you have images instead of text, but that shouldn't matter):
PdfPCell cell = new PdfPCell();
for (int i = 1; i <= 5; i++)
cell.addElement(new Paragraph("Line " + i));
I then create two tables. A table with three rows, and a table with ten rows.
This is the short table that should fit a single page:
table = new PdfPTable(1);
table.addCell(cell);
table.addCell(cell);
table.addCell(cell);
This is the long table that doesn't fit a page, and that therefore should be distributed over different pages:
table = new PdfPTable(1);
table.addCell(cell);
table.addCell(cell);
table.addCell(cell);
table.addCell(cell);
table.addCell(cell);
table.addCell(cell);
table.addCell(cell);
table.addCell(cell);
table.addCell(cell);
table.addCell(cell);
If I understand your question correctly, you want the former table NOT to be split, but to be centered vertically. The second table NEEDS to be split, as it doesn't fit on a single page.
This is achieved through using these two methods:
private int status = ColumnText.START_COLUMN;
private float y_position = 0;
public void addTable(Document document, PdfContentByte canvas, PdfPTable table)
throws DocumentException {
Rectangle pagedimension = new Rectangle(36, 36, 559, 806);
drawColumnText(document, canvas, pagedimension, table, true);
Rectangle rect;
if (ColumnText.hasMoreText(status)) {
rect = pagedimension;
}
else {
rect = new Rectangle(36, 36, 559, 806 - ((y_position - 36) / 2));
}
drawColumnText(document, canvas, rect, table, false);
}
public void drawColumnText(Document document, PdfContentByte canvas, Rectangle rect, PdfPTable table, boolean simulate)
throws DocumentException {
ColumnText ct = new ColumnText(canvas);
ct.setSimpleColumn(rect);
ct.addElement(table);
status = ct.go(simulate);
y_position = ct.getYLine();
while (!simulate && ColumnText.hasMoreText(status)) {
document.newPage();
ct.setSimpleColumn(rect);
status = ct.go(simulate);
}
}
In the first method, we add the PdfPTable
to an A4 Document
with margins of half an inch:
Rectangle pagedimension = new Rectangle(36, 36, 559, 806);
We first use the second method in simulation mode. This means that we pretend to add the table, but we don't really add it, we only want to know (1.) if the table fits the page (status
) and if so, (2.) how much space it took (y_position
).
Based on that information, we either add the table for real using the page dimensions (if the table doesn't fit the page), or we add the table using new dimensions that center the table vertically (if the table fits the page).
See the resulting PDF: center_vertically.pdf
This example shows you how to find out how much space a table takes vertically. If that was not the missing link in your application, then please rephrase the question.
Upvotes: 1