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?
Related
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);
In Apache POI 4.0 I want to set an Excel cell background color like this :
IndexedColorMap colorMap = workbook.getStylesSource().getIndexedColors();
style.setFillForegroundColor(new XSSFColor(java.awt.Color.BLUE, colorMap).getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("Title");
header.getCell(0).setCellStyle(style);
.. but all I get are black cells. I've tried many things, but result is always the same.
How can I set the background color of an Excel cell in Apache POI 4.0 ?
Try to use below code for background style
style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
I am using itext to create PDF's in java. I Can write images to PDF using java. How do I make sure that the size of the image is same as it was before writing it to the PDF.
e.g. if the image is of 32 X 32 pixels size. It should be like that only in the generated pdf.
Below is my code.
PdfPTable table = new PdfPTable(1);
Image img = Image.getInstance(photoLocation);
PdfPCell cell = new PdfPCell(img, false);
cell.setPadding(8);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell);
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
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 ;-)