I'm attempting to generate PDFs for college transcripts using itext 5.5.9. I've decided on using PdfPTable for controlling the layout. The beginning of the document includes an overview section with the following lines:
Name:
Date of Birth:
Major:
Minor:
Right now the output adds more space between these lines than desired. (It looks as if the lines are double-spaced.) Here are code snippets that seem relevant:
PdfPCell overviewCell = new PdfPCell();
//other codes here...
overviewCell.setPadding(0f);;
overviewCell.setLeading(0f, 0f);
overviewCell.addElement(new Phrase("Name:", normalFont));
overviewCell.addElement(Chunk.NEWLINE);
overviewCell.addElement(new Phrase("Date of Birth:", normalFont));
overviewCell.addElement(Chunk.NEWLINE);
overviewCell.addElement(Chunk.NEWLINE);
overviewCell.addElement(new Phrase("Major:", normalFont));
I also tried obtaining the PdfWriter's ColumnText (ct), and working with that object directly before calling overviewCell.setColumn(ct) but that didn't fix the spacing either.
Please let me know how do control the text's line spacing within the cell.
Thanks!
Why don't you just add create a sub table and add it to the overview cell with your desired amount. This will handle a lot of the formatting for you.
private static final String _name = "Name:";
private static final String _dateOfBirth = "Date Of Birth:";
private static final String _major = "Major:";
PdfPTable table = new PdfPTable(2);
PdfPCell spaceCell = new PdfPCell(new Phrase(" ", normalFont));
PdfPCell nameCell = new PdfPCell(new Phrase(_name, normalFont));
PdfPCell dateOfBirthCell = new PdfPCell(new Phrase(_dateOfBirth, normalFont));
PdfPcell majorCell = new PdfPCell(new Phrase(_major, normalFont));
table.addCell(nameCell);
table.addCell(spaceCell);
table.addCell(dateOfBirthCell);
table.addCell(spaceCell);
table.addCell(spaceCell); // if you want an extra line break here a fill a line
table.addCell(spaceCell); // with space cells
table.addCell(majorCell);
overviewCell.addElement(table);
Related
Please check the code here. Facing issue to merge 2 PDFPtable top to bottom.
PdfPTable table = new PdfPTable(new float[] { 1.0f, 2.0f, 5.0f, 1.0f, 2.0f });
table.setWidthPercentage(100.0f);
ArrayList<String> lHeaders = new ArrayList<>();
lHeaders.add("S. No.");
lHeaders.add("Course Code");
lHeaders.add("Course Name");
lHeaders.add("Credit");
lHeaders.add("Letter Grade");
ArrayList<String> lData = new ArrayList<>();
lData.add("1");
lData.add("D 210");
lData.add("COMPUTER & INFORMATION TECHNOLOGY FUNDAMENTAL LAB-II");
lData.add("1.0");
lData.add("BB");
// Create and add a title across both columns.
Font headerfont = new Font(Font.TIMES_ROMAN, 15, Font.BOLD);
// PdfPCell cell = new PdfPCell (new Paragraph ("New Mustang
// Features"));
PdfPCell cell;
for (String i : lHeaders) {
cell = new PdfPCell(new Paragraph(i, headerfont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBackgroundColor(new Color(255, 0, 0));
// cell.setPadding (10.0f);
table.addCell(cell);
}
// Add header cells for these columns.
// cell = new PdfPCell (new Paragraph ("Feature"));
// PdfPCell cell1;
Font datafont = new Font(Font.HELVETICA, 10, Font.NORMAL);
for (String j : lData) {
cell = new PdfPCell(new Paragraph(j, datafont));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
// cell.setPadding (10.0f);
table.addCell(cell);
}
Font headerfont2 = new Font(Font.TIMES_ROMAN, 10, Font.BOLD);
PdfPTable table2 = new PdfPTable(8);
table2.setWidthPercentage(100.0f);
PdfPCell cell2;
cell2 = new PdfPCell(new Paragraph("CURRENT SEMESTER RECORD", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setColspan(4);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("CUMULATIVE SEMESTER RECORD", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setColspan(4);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("TOTAL CREDIT POINTS", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("TOTAL CREDIT", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("CREDITS EARNED", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("SGPA", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("TOTAL CREDIT POINTS", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("TOTAL CREDIT", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("CREDITS EARNED", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("CGPA", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("Result declared on :", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_LEFT);
cell2.setPaddingTop(40);
cell2.setBorderWidthLeft(0.5f);
cell2.setBorderWidthBottom(0.5f);
cell2.setColspan(4);
table2.addCell(cell2);
cell2 = new PdfPCell(new Paragraph("Controller of Examinations", headerfont2));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setPaddingTop(40);
cell2.setBorderWidthRight(0.5f);
cell2.setBorderWidthBottom(0.5f);
cell2.setColspan(4);
table2.addCell(cell2);
I have created two PDFPtable instances one is table and another is table2, i just want to merge these two instances and return from my code. Is it possible? I don't want to add it to the doc from my code and return the document instance. The pdf looks like the attached image.
To summarize, you essentially want to merge those two tables into a single one because your method is designed to return only a single table.
If possible, the best solution would be to change the design of the method and allow it to return multiple tables, as an Iterable (e.g. a List) or as an array.
If that is not possible (in your case returning a single table appears to be a project requirement), a solution would be to create another, single-column table and put your first table into its first row and your second one into the second row. For proper appearances you may want to also adapt table properties for margins and borders accordingly.
Be aware, though, that iText 5 tables were not designed for intense table stacking; having tables inside tables inside tables is likely to cause interesting effects during e.g. page breaks and may be quite resource hungry.
I want to keep two cells of a table keep together. So as per below code i gave cell as keep together.
I gave here example for two inner tables actually there are more.
So if both heading and content cell are not able to fit in the page then it starts from next page. But requirement is part of page should not be blank and heading and content should always be together. If content size is more then only content may split.
Table outerTable =new Table(1);
Table innerTable1 =new Table(1);
String content="";/*Large content from a text file*/
Cell cell1 = new Cell().add(new Paragraph("Heading1"));
Cell cell2 = new Cell().add(new Paragraph(content));
innerTable1.add(cell1);
innerTable1.add(cell2);
Cell cell =new Cell().add(innerTable1).setKeepTogether(true);
outerTable.add(cell);
Table innerTable2 =new Table(1);
cell1 = new Cell().add(new Paragraph("Heading2"));
cell2 = new Cell().add(new Paragraph(content));
innerTable2.add(cell1);
innerTable2.add(cell2);
cell =new Cell().add(innerTable2).setKeepTogether(true);
outerTable.add(cell);
I am creating a PDF using iText 5.5.6.
I wanted to tag table, row and cell as a DIV.
I have written the following code but only the table gets marked as a DIV and not the row and cell.
PdfPTable table = new PdfPTable(2);
table.setRunDirection(PdfAWriter.RUN_DIRECTION_LTR);
table.setRole(PdfName.DIV);
table.setWidthPercentage(80);
table.setWidths(new float[] { 0.8f, 1.2f });
table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT);
table.setSpacingBefore(10);
table.setSpacingAfter(15);
Paragraph p3 = new Paragraph("Name:", font12);
Paragraph p4 = new Paragraph("FirstName LastName", font12);
Paragraph p5 = new Paragraph("DOB: ", font12);
Paragraph p6 = new Paragraph("00-00-0000", font12);
PdfPCell cellp3 = new PdfPCell(p3);
cellp3.setBorder(0);
PdfPCell cellp4 = new PdfPCell(p4);
cellp4.setBorder(0);
cellp3.setRole(PdfName.DIV);
cellp4.setRole(PdfName.DIV);
table.addCell(cellp3);
table.addCell(cellp4);
PdfPCell cellp5 = new PdfPCell(p5);
cellp5.setBorder(0);
PdfPCell cellp6 = new PdfPCell(p6);
cellp6.setBorder(0);
cellp5.setRole(PdfName.DIV);
cellp6.setRole(PdfName.DIV);
table.addCell(cellp5);
table.addCell(cellp6);
How to solve this?
If I test your code the table and all the cells are tagged as DIV. The rows are indeed not.
In iText the classes PdfPTable, PdfPRow and PdfPCell are responsible for table functionality. PdfPRow is typically only used in the iText implementation internally, because a PdfPTable instance accepts PdfPCells directly.
But you are able to retrieve the table rows to set their role. After you have added all the cells to the table, do this:
for (PdfPRow row : table.getRows())
row.setRole(PdfName.DIV);
I have a main report which is printed horizontally. It has 5 columns.
On every column i want to put a sub report. So i created this:
And the sub-report just like this:
The problem is, when i run i get the following exception:
net.sf.jasperreports.engine.JRRuntimeException: Subreport overflowed on a band that does not support overflow.
Looks like jasper reports can't stretch the detail band vertically when there is a sub-report in it and the print order is set to horizontal.
What can I do to avoid this error and achieve what i want?
I found the solution for this problem. After a deep search i found that, sadly, there's no way to do this on Jasper Reports because, no matter what, when you have a report printed horizontally, the "Detail" band will never change it's height. So, subreports or text fields which overflows will throw an exception.
The workaround for this problem is to work without reports, with a PDF generator like iText, for example.
This is the code i did to achive what i wanted with iText if somebody needs it:
Document document = new Document();
File arquivo = new File("C:\\Users\\Mateus\\Desktop\\testezãozarãozão.pdf");
PdfWriter.getInstance(document, new FileOutputStream(arquivo));
document.open();
LinkedHashMap<Produto, LinkedHashMap<String, List<PrePedidoItem>>> produtos = createStructuredHashMap();
for (Produto produto : produtos.keySet()) {
PdfPTable table = new PdfPTable(5);
PdfPCell cellProduto = new PdfPCell();
Phrase phraseProduto = new Phrase(String.valueOf(produto));
phraseProduto.setFont(new Font(Font.FontFamily.HELVETICA, 11, Font.BOLD|Font.UNDERLINE, new BaseColor(50, 65, 200)));
cellProduto.addElement(phraseProduto);
cellProduto.setColspan(5);
cellProduto.setHorizontalAlignment(PdfPCell.ALIGN_MIDDLE);
cellProduto.setBorder(Rectangle.NO_BORDER);
cellProduto.setPaddingBottom(10);
cellProduto.setPaddingTop(20);
table.addCell(cellProduto);
LinkedHashMap<String, List<PrePedidoItem>> mapas = produtos.get(produto);
int mapasAdicionados = 0;
for (String mapa : mapas.keySet()) {
PdfPCell cellMapa = new PdfPCell();
Phrase phraseMapa = new Phrase(mapa);
phraseMapa.setFont(new Font(Font.FontFamily.HELVETICA, 9, Font.BOLD, new BaseColor(215, 100, 0)));
cellMapa.addElement(phraseMapa);
List<PrePedidoItem> itensDoMapa = mapas.get(mapa);
for (PrePedidoItem item : itensDoMapa) {
DecimalFormat df = new DecimalFormat("###,##0.00");
Phrase phraseItem = new Phrase(df.format(item.getLargura()) + " x " + df.format(item.getComprimento()));
phraseItem.setFont(new Font(Font.FontFamily.HELVETICA, 9, Font.NORMAL, BaseColor.BLACK));
cellMapa.addElement(phraseItem);
}
cellMapa.setBorder(Rectangle.NO_BORDER);
table.addCell(cellMapa);
mapasAdicionados ++;
if(mapasAdicionados == 5) {
mapasAdicionados = 0;
}
}
PdfPCell celulaPreenchimentoMapas = new PdfPCell();
celulaPreenchimentoMapas.setColspan(5 - mapasAdicionados);
celulaPreenchimentoMapas.setBorder(Rectangle.NO_BORDER);
table.addCell(celulaPreenchimentoMapas);
document.add(table);
}
document.close();
Desktop.getDesktop().open(arquivo);
I have table in itext.
I need to change a specific cell width in the last row.
Is it possible?
edit: colspan does not cover my problem. I need completely different widths in this row.
There is many ways to change your last row cell width :
A. With colspan, of course you'll need cells to fit with other rows.
Example :
PdfPTable table = new PdfPTable(3);
table.addCell("cell A0");
table.addCell("cell A1");
table.addCell("cell A2");
table.addCell("cell B0");
table.addCell("cell B1");
table.addCell("cell B2");
PdfPCell cell = new PdfPCell();
cell.setColspan(2);
cell.addElement(new Phrase("C0 & C1"));
table.addCell(cell);
table.addCell("cell C2");
B. Two tables with the same total width, no space between tables.
Example :
PdfPTable table = new PdfPTable(1);
PdfPTable table1 = new PdfPTable(3);
table1.setWidthPercentage(100f);
table1.addCell("cell A0");
table1.addCell("cell A1");
table1.addCell("cell A2");
PdfPTable table2 = new PdfPTable(2);
table2.setWidthPercentage(100f);
table2.addCell("cell B0");
table2.addCell("cell B1");
table.addCell(table1);
table.addCell(table2);