I am trying to generate a pdf using Itext.where i need to build a table with header. But i am adding all the content on the page dyamically by iterating the list and adding the cells to the Table ,and finally adding table to the Document after the interation is done.
document.add(table);
My writer and document doesnt show me a updated page number if my content is moved on to the next page,until the table content is added to the document.I need to iterate and build the dynamic table where at the begining of the next page it should have the header of the table.I tried my level best following many approaches.But,I am unable to know when the content is moved on to next page in the middle of the iteration process.PDFWriter is showing me the page number before entering the loop.I need to get the updated pagenumber when ever the tables content goes on to next page.And i dont need this header to be on all pages.I need it only until the table content carries over.Please let me know which is the best way to handle this situation. Thanks
The other answer to this question isn't a real answer, it refers to an answer to another question. If that is actually the answer to this current question, the current question should have been marked as duplicate.
This being said, I think the answer is not entirely correct. I have written an example that produces something like this:
As you can see, there is no header on the first page, there is only a header on the second and third page that says "Table XYZ (Continued)". I even added a footer that says "Continue on next page". As you can see, this footer only appears at the moment the table is split. It isn't present on the last page. This wasn't asked in the question, and it is easy to remove from my code.
You can find the complete code sample here: SimpleTable5
These are the relevant parts:
PdfPTable table = new PdfPTable(5);
table.setWidthPercentage(100);
PdfPCell cell = new PdfPCell(new Phrase("Table XYZ (Continued)"));
cell.setColspan(5);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Continue on next page"));
cell.setColspan(5);
table.addCell(cell);
table.setHeaderRows(2);
table.setFooterRows(1);
table.setSkipFirstHeader(true);
table.setSkipLastFooter(true);
for (int i = 0; i < 350; i++) {
table.addCell(String.valueOf(i+1));
}
document.add(table);
If you only want a header:
PdfPTable table = new PdfPTable(5);
table.setWidthPercentage(100);
PdfPCell cell = new PdfPCell(new Phrase("Table XYZ (Continued)"));
cell.setColspan(5);
table.addCell(cell);
table.setHeaderRows(1);
table.setSkipFirstHeader(true);
for (int i = 0; i < 350; i++) {
table.addCell(String.valueOf(i+1));
}
document.add(table);
The difference with your code, is that you add the following line to avoid that the header appears on the first page:
table.setSkipFirstHeader(true);
This is not mentioned in the answer to Repeat PdfPTable header in all the continuation pages using iText
Related
I'm using itext5 and when I set the border width for the table I get an issue like below:
This way I used to making the pdf cell:
PdfPCell cell = new PdfPCell(new Phrase("SUBSTATION",f));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(2);
cell.setBorderWidth(2);
cell.setFixedHeight(30f);
table.addCell(cell);
and for the second cell should be under the complete border surrounded i used the following code:
cell.setBorderWidthLeft(2);
How can I modify this issue?
I have a cell that should contain rather long text with multiple \n.
I'm using setWrapText(true) on a cell style object but it seems not working.
I still can only see the first line of a cell text.
Here is the code I'm using
CellStyle mainHeaderCellStyle = wb.createCellStyle();
mainHeaderCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
mainHeaderCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
mainHeaderCellStyle.setWrapText(true);
Cell headersCell = infoRow.createCell(cellIndex);
headersCell.setCellStyle(mainHeaderCellStyle);
headersCell.setCellValue(mainHeader);
But the header cell (colored in grey) is still like this
XLSX page with header
This is the text when I double click the cell
XLSX page with clicked header
deHaar answer is the correct one. Resizing row height helped to fix the problem.
The working code is this
Row infoRow = companyDataSheet.createRow(currentRowIndex++);
CellStyle mainHeaderCellStyle = wb.createCellStyle();
mainHeaderCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
mainHeaderCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
mainHeaderCellStyle.setWrapText(true);
Cell headersCell = infoRow.createCell(cellIndex);
headersCell.setCellStyle(mainHeaderCellStyle);
headersCell.setCellValue(mainHeader);
infoRow.setHeight((short) 2300);
How do i numbering of pages in word file by Java.
I am using Apache POI driver to interact JAVA and word .
i want border and page number as well in my word file while i am creating file from JAVA.
Please help.
The question marked as a duplicate has a complex answer to a relatively simple question.
The simple answer (for page number) is very similar to this answer: https://stackoverflow.com/a/40264237/2296441. The difference is just which field to insert. The afore mentioned answer shows how to insert a TOC field. In your case you want a PAGE field.
XWPFParagraph p;
...
// get or create your paragraph
....
CTP ctP = p.getCTP();
CTSimpleField page = ctP.addNewFldSimple();
page.setInstr("PAGE");
page.setDirty(STOnOff.TRUE);
Note: setDirty tells Word to update the field which causes a dialog to be opened when the document is opened. This dialog is MS Word making sure you want to update the field. I don't think you can disable the dialog and still have the field calculated on open.
To set page borders you are once again going to have to break into the CT classes. In this case the appropriate location in the document is the section properties. Here is how to set a double line border around the whole page set back 24 points from the page edge.
// Page Borders
CTDocument1 ctDoc = doc.getDocument();
CTBody ctBody = ctDoc.getBody();
CTSectPr ctSectPr = ctBody.isSetSectPr() ? ctBody.getSectPr() : ctBody.addNewSectPr();
CTPageBorders ctPgBorders = ctSectPr.isSetPgBorders() ? ctSectPr.getPgBorders() : ctSectPr.addNewPgBorders();
ctPgBorders.setOffsetFrom(STPageBorderOffset.PAGE);
CTBorder ctBorder = CTBorder.Factory.newInstance();
ctBorder.setVal(STBorder.DOUBLE);
ctBorder.setSpace(new BigInteger("24"));
ctPgBorders.setTop(ctBorder);
ctPgBorders.setBottom(ctBorder);
ctPgBorders.setRight(ctBorder);
ctPgBorders.setLeft(ctBorder);
Disclaimer
The MS-Word functionality in POI is still largely unfinished, and subject to change.
Currently I'm working with exporting reports using Aspose Words in Java. I'm populating table with dynamic data but my main problem is when data reaches next page there is no header in exported docs. How can I get header for all pages in the exported file. Any suggestions will be appreciated.
We can solve this issue in JAVA in the following way:
1.Load the document
Document doc = new Document(getMyDir() + "Table.SimpleTable.doc");
2.Get the first table
Table table = doc.getChild(NodeType.TABLE, 0, true); //i.e. second parenthesis as index of table in doc file
3. Get the rows which is used for heading of table
Row rows = (Row) table.getRows().get(0);//we can set multiple rows as heading by passing it's index
4. set the rows as HeadingFormat = true
rows.getRowFormat().setHeadingFormat(true);
Select the header portion (row/rows) to be repeated in the template.
Right click it. Go to table properties and select row tab where there is a second option as "Repeat as header row at the top of each page".
I am trying to export records from database into pdf using itext pdf library in java..But i am getting following problems in alignment of pdf table inside pdf file..
1.Table is not showing in the full pdf page .It is leaving spaces from left and right of the pdf page.
2.Every page is showing values in half of the page only .Means pdf table is showing in half of the pdf pages..
Here is my code..
Document document = new Document();
PdfWriter.getInstance(document, fos);
PdfPTable table = new PdfPTable(10);
table.setWidthPercentage(100);
table.setSpacingBefore(0f);
table.setSpacingAfter(0f);
PdfPCell cell = new PdfPCell(new Paragraph("DateRange"));
cell.setColspan(10);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setPadding(5.0f);
cell.setBackgroundColor(new BaseColor(140, 221, 8));
table.addCell(cell);
table.addCell("Calldate");
table.addCell("Calltime");
table.addCell("Source");
table.addCell("DialedNo");
table.addCell("Extension");
table.addCell("Trunk");
table.addCell("Duration");
table.addCell("Calltype");
table.addCell("Callcost");
table.addCell("Site");
while (rs.next()) {
table.addCell(rs.getString("date"));
table.addCell(rs.getString("time"));
table.addCell(rs.getString("source"));
table.addCell(rs.getString("destination"));
table.addCell(rs.getString("extension"));
table.addCell(rs.getString("trunk"));
table.addCell(rs.getString("dur"));
table.addCell(rs.getString("toc"));
table.addCell(rs.getString("callcost"));
table.addCell(rs.getString("Site"));
}
table.setSpacingBefore(5.0f); // Space Before table starts, like margin-top in CSS
table.setSpacingAfter(5.0f); // Space After table starts, like margin-Bottom in CSS
document.open();//PDF document opened........
document.add(Chunk.NEWLINE); //Something like in HTML :-)
document.add(new Paragraph("TechsoftTechnologies.com"));
document.add(new Paragraph("Document Generated On - " + new Date().toString()));
document.add(table);
document.add(Chunk.NEWLINE); //Something like in HTML :-)
document.newPage(); //Opened new page
//In the new page we are going to add list
document.close();
fos.close();
I had to read your question multiple times before I understood that you wanted to suppress the margins. I copy/pasted (and adapted) your example, and I thought I couldn't reproduce your problem because I (and many other developers) am used to the fact that pages have margins.
Anyway, this is my version of your example: FullPageTable and it creates the following PDF: full_page_table.pdf
I've made some minor changes, for instance: it doesn't make sense to pass a ´Paragraph´ as a parameter for a PdfPCell because that Paragraph will be treated as a Phrase, but I think the line you're looking for is:
Document document = new Document(PageSize.A4, 0, 0, 0, 0);
The zeros define the width of the margin, and if I understand your question correctly, you want to suppress those margins.
As for your allegation Every page is showing values in half of the page only .Means pdf table is showing in half of the pdf pages, I think you're causing that yourself by introducing document.newPage(); otherwise your allegation doesn't make sense ;-)