Redesign output by removing left cell - java

If you see the below table I have separated two cells one cell is added as a left cell(Name) and one more cell added as a table.
I have tried below code :
I am using the package as import com.lowagie.text.pdf.*;
PdfWriter.getInstance(document,
new FileOutputStream("C:/Temp/TableWidthAlignment.pdf"));
document.open();
//Main table
PdfPTable mainTable = new PdfPTable(2);
mainTable.setWidths(new int[] { 10,90 });
//cell one is Name cell
PdfPCell innerCellKeyName = new PdfPCell(new Phrase("Name", boldFont));
//innerCellKeyName.setBorder(Rectangle.NO_BORDER);
mainTable.addCell(innerCellKeyName);
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
cell.setColspan(3);
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("3.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("3.2");
table.addCell("4.1");
table.addCell("4.2");
table.addCell("4.3");
//cell two is as table
PdfPCell cell2 = new PdfPCell(table);
mainTable.addCell(cell2);
document.add(mainTable);
Output is:
Expected output is : Cross box need to be removed form box in the left cell.

I have tried some thing to make work out of expected result
Solution:
I have copy pasted the same above table and made the left cell as no boarder.
document.open();
PdfPTable mainTable = new PdfPTable(2);
mainTable.setWidths(new int[] { 10,90 });
PdfPCell innerCellKeyName = new PdfPCell(new Phrase("Name", boldFont));
//innerCellKeyName.setBorder(Rectangle.NO_BORDER);
mainTable.addCell(innerCellKeyName);
// step4
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
cell.setColspan(3);
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("3.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("3.2");
table.addCell("4.1");
table.addCell("4.2");
table.addCell("4.3");
PdfPCell cell2 = new PdfPCell(table);
mainTable.addCell(cell2);
document.add(mainTable);
PdfPTable mainTable2 = new PdfPTable(2);
mainTable2.setWidths(new int[] { 10,90 });
PdfPCell innerCellKeyName2 = new PdfPCell(new Phrase("", boldFont));
innerCellKeyName2.setBorder(Rectangle.NO_BORDER);
mainTable2.addCell(innerCellKeyName2);
// step4
PdfPTable table2 = new PdfPTable(3);
PdfPCell cell3 = new PdfPCell(new Paragraph("header with colspan 3"));
cell3.setColspan(3);
table2.addCell(cell3);
table2.addCell("1.1");
table2.addCell("2.1");
table2.addCell("3.1");
table2.addCell("1.2");
table2.addCell("2.2");
table2.addCell("3.2");
table2.addCell("4.1");
table2.addCell("4.2");
table2.addCell("4.3");
PdfPCell cell4 = new PdfPCell(table2);
mainTable2.addCell(cell4);
document.add(mainTable2);

Related

How to give separations between the cells of a table in Itext7?

Problem Statement:-
I am using Itext7 in JAVA to create a PDF having a table. I need to give the separations between the cells of the table.
Red and blue arrows in the image are the pin points from where I want to separate them.
Any help regarding the issue is highly appreciated!!
Code:-
package com.example.pdfcreator;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.itextpdf.*;
#SpringBootApplication
public class PdfcreatorApplication {
public static final String DEST = "D:\\generate_pdf\\hello.pdf";
public static void main(String args[]) throws IOException, java.io.IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(DEST));
Document document = new Document(pdf);
var table = new Table(new float[] { 3,3,3,3,3,3,3}).setWidth(UnitValue.createPercentValue(100)).setFixedLayout().setFontSize(8).setMarginTop(4);
Cell cell11 = new Cell(1, 2).setBorder(Border.NO_BORDER).add(new Paragraph("label1 :"));
Cell cell12 = new Cell(1, 5).add(new Paragraph(""));
Cell cell21 = new Cell(1, 2).setBorder(Border.NO_BORDER).add(new Paragraph("label2 :"));
Cell cell22 = new Cell(1, 5).add(new Paragraph(""));
Cell cell31 = new Cell(1, 2).setBorder(Border.NO_BORDER).add(new Paragraph("label3 :"));
Cell cell32 = new Cell(1, 5).add(new Paragraph(""));
Cell cell41 = new Cell(1, 2).setBorder(Border.NO_BORDER).add(new Paragraph("label4 :"));
Cell cell42 = new Cell(1, 5).add(new Paragraph(""));
table.addCell(cell11);
table.addCell(cell12);
table.addCell(cell21);
table.addCell(cell22);
table.addCell(cell31);
table.addCell(cell32);
table.addCell(cell41);
table.addCell(cell42);
document.add(table);
var table99 = new Table(new float[] { 3,3,3,3,3,3,3}).setWidth(UnitValue.createPercentValue(100)).setFixedLayout().setFontSize(8);
Cell cell = new Cell(1,2).setBorder(Border.NO_BORDER).add(new Paragraph("label9 : "));
table99.addCell(cell);
cell = new Cell(1,4).add(new Paragraph(" "));
table99.addCell(cell);
Cell cell23 = new Cell(5, 1).add(new Paragraph("Photo").setMarginLeft(23).setMarginTop(28));
table99.addCell(cell23);
cell = new Cell(1,2).setBorder(Border.NO_BORDER).add(new Paragraph(" label10: "));
table99.addCell(cell);
cell = new Cell(1,4).add(new Paragraph(" "));
table99.addCell(cell);
cell = new Cell(1,2).setBorder(Border.NO_BORDER).add(new Paragraph(" label11: "));
table99.addCell(cell);
cell = new Cell(1,4).add(new Paragraph(" "));
table99.addCell(cell);
cell = new Cell(1,2).setBorder(Border.NO_BORDER).add(new Paragraph(" label12: "));
table99.addCell(cell);
cell = new Cell(1,4).add(new Paragraph(" "));
table99.addCell(cell);
cell = new Cell(1,2).setBorder(Border.NO_BORDER).add(new Paragraph(" label13: "));
table99.addCell(cell);
cell = new Cell(1,4).add(new Paragraph(" "));
table99.addCell(cell);
document.add(table99); }}
I have given the seperations using the empty cells with borders removed and it has worked properly as per my requirement. Demo code line which i have implemented are written as below.
I have created one style in which i have given the minimum height required for my spacing and removed the borders from the cell.
Style spacingCellStyle = new Style().setBorder(Border.NO_BORDER).setHeight(1);
Then, i have simply just added the cell with this style wherever in the table, spacing was required.
Table.addCell(new Cell(1, 6).addStyle(spacingCellStyle));
As per my requirement, this was the only possible way to do so and it executed perfectly.

iText split tables printing multiple times

I am trying to split a table after x rows so I do not have an orphaned signature block on a page (i.e., there is always at least one row of the table before the signature block on the page). In my test I have six rows to be printed in the table. The first set of two is being printed once the second set is printed as two identical tables with three blank rows between them the third set is printed as three identical tables with three blank rows between them.
How do I remove the duplicate tables please? The code is:
Paragraph preface = new Paragraph();
PdfPTable table = new PdfPTable(3);
table.setWidths(new int[]{1, 3, 1});
table.setHeaderRows(1);
PdfPCell c1 = new PdfPCell(new Phrase("Section"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Award"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Date"));
c1.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(c1);
table.setHeaderRows(1);
String storedName = null;
int noRows = 0;
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
DateFormat df2 = new SimpleDateFormat("dd-MM-yyyy");
for (final Transcript scoutNamesDescription : listymAwards) {
if (noRows > 1){ // Change this to number of rows required
noRows = 0;
preface.add(table);
document.add(preface);
document.newPage();
// We add three empty lines
addEmptyLine(preface, 1);
addEmptyLine(preface, 1);
addEmptyLine(preface, 1);
table.flushContent();
}
noRows++;
if (scoutNamesDescription.getSection().equals(storedName)){
table.addCell(" ");
}else{
storedName = scoutNamesDescription.getSection();
table.addCell(scoutNamesDescription.getSection());
}
table.addCell(scoutNamesDescription.getAwardName());
Date awardedDate = df1.parse(scoutNamesDescription.getAwardedDate());
String awardedString = df2.format(awardedDate);
table.addCell(awardedString);
}
preface.add(table);
document.add(preface);

Header and Footer with pdf by using itextsharp but footer is overiding on document

/**
* Adds the header and the footer.
* #see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
* com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
public void onEndPage(PdfWriter writer, Document document) {
//Rectangle rect = writer.getBoxSize("art");
CalibrateItLogger.publish("Page Number :"+writer.getPageNumber());
PdfPTable footterPdfPTable = new PdfPTable(1);
PdfPTable fotterLeftPdfPTable =new PdfPTable(1);
PdfPTable fotterRightPdfPTable =new PdfPTable(1);
//document.newPage();
document.add(Chunk.NEWLINE);
footterPdfPTable = new PdfPTable(3);
footterPdfPTable.setWidthPercentage(100);
fotterLeftPdfPTable = new PdfPTable(1);
fotterLeftPdfPTable.setWidthPercentage(100);
PdfPCell planBExtendedPdfPCell = new PdfPCell();
planBExtendedPdfPCell.addElement(new Paragraph("PLAN B EXTENDED"));
planBExtendedPdfPCell.setBorder(Rectangle.NO_BORDER);
fotterLeftPdfPTable.addCell(planBExtendedPdfPCell);
//Calibration Company Name
PdfPCell comapnyNameLablePdfPCell=new PdfPCell (new Paragraph (companyname(),font4));
comapnyNameLablePdfPCell.setBorder(Rectangle.NO_BORDER);
fotterLeftPdfPTable.addCell(comapnyNameLablePdfPCell);
//Address
PdfPCell addressPdfPCell=new PdfPCell (new Paragraph ("Address data",font3));
addressPdfPCell.setBorder(Rectangle.NO_BORDER);
fotterLeftPdfPTable.addCell(addressPdfPCell);
//Zipcode
PdfPCell zipPdfPCell=new PdfPCell (new Paragraph ("Postal code",font3));
zipPdfPCell.setBorder(Rectangle.NO_BORDER);
fotterLeftPdfPTable.addCell(zipPdfPCell);
//Certificate and Serial Number
PdfPCell cityStateAndZipPdfPCell=new PdfPCell (new Paragraph ("Serial No ",font3));
cityStateAndZipPdfPCell.setBorder(Rectangle.NO_BORDER);
fotterLeftPdfPTable.addCell(cityStateAndZipPdfPCell);
//Model Name and Asset No
PdfPCell pipetteModelNamePdfPCell=new PdfPCell (new Paragraph ("Asset No",font3));
pipetteModelNamePdfPCell.setBorder(Rectangle.NO_BORDER);
fotterLeftPdfPTable.addCell(pipetteModelNamePdfPCell);
PdfPCell footerLeftPdfPCell = new PdfPCell(fotterLeftPdfPTable);
footerLeftPdfPCell.setBorder(Rectangle.NO_BORDER);
footterPdfPTable.addCell(footerLeftPdfPCell);
PdfPCell emptyPdfPCell = getEmptyPdfPCell();
emptyPdfPCell.addElement(getEmptyParagraph());
PdfPTable pageNumberPdfPTable = new PdfPTable(1);
pageNumberPdfPTable.addCell(emptyPdfPCell);
pageNumberPdfPTable.addCell(emptyPdfPCell);
pageNumberPdfPTable.addCell(emptyPdfPCell);
pageNumberPdfPTable.addCell(emptyPdfPCell);
pageNumberPdfPTable.addCell(emptyPdfPCell);
Paragraph pageNumberParagraph = new Paragraph("Page "+pagenumber,font3);
pageNumberParagraph.setAlignment(Element.ALIGN_CENTER);
//pageNumberPdfPTable.addCell(pageNumberParagraph);
PdfPCell pegeNumberPdfPCell = new PdfPCell(pageNumberParagraph);
pegeNumberPdfPCell.setBorder(Rectangle.NO_BORDER);
footterPdfPTable.addCell(pegeNumberPdfPCell);
fotterRightPdfPTable = new PdfPTable(2);
fotterRightPdfPTable.setWidthPercentage(100);
//Date
Paragraph dateParagraph = new Paragraph(new Phrase("Date :"+CurrentDate.getDateFormat(Date,"dd/MM/yyyy"),font3));
dateParagraph.setAlignment(Element.ALIGN_LEFT);
PdfPCell datePdfPCell = new PdfPCell(dateParagraph);
datePdfPCell.setBorder(Rectangle.NO_BORDER);
fotterRightPdfPTable.addCell(datePdfPCell);
//Next Due Data
Paragraph nextDueParagraph = new Paragraph(new Phrase("Next Due :"+CurrentDate.getDateFormat(Date,"dd/MM/yyyy"),font3));
dateParagraph.setAlignment(Element.ALIGN_LEFT);
PdfPCell nextDuePdfPCell = new PdfPCell(nextDueParagraph);
nextDuePdfPCell.setBorder(Rectangle.NO_BORDER);
fotterRightPdfPTable.addCell(nextDuePdfPCell);
fotterRightPdfPTable.addCell(getEmptyPdfPCell());
fotterRightPdfPTable.addCell(getEmptyPdfPCell());
fotterRightPdfPTable.addCell(getEmptyPdfPCell());
fotterRightPdfPTable.addCell(getEmptyPdfPCell());
emptyPdfPCell = getEmptyPdfPCell();
emptyPdfPCell.setColspan(2);
fotterRightPdfPTable.addCell(emptyPdfPCell);
PdfPCell underlinePdfPCell = new PdfPCell(new Phrase(new Chunk("Sign :").setUnderline(+1f, -2f)));
underlinePdfPCell.setBorder(Rectangle.NO_BORDER);
underlinePdfPCell.setColspan(2);
fotterRightPdfPTable.addCell(underlinePdfPCell);
Paragraph employeeParagraph = new Paragraph("Govardhan Rao",font3);
employeeParagraph .setAlignment(Element.ALIGN_CENTER);
PdfPCell employeePdfPCell = new PdfPCell(employeePdfPCell );
employeePdfPCell .setBorder(Rectangle.NO_BORDER);
calibrationEmployeePdfPCell.setColspan(2);
fotterRightPdfPTable.addCell(employeePdfPCell );
PdfPCell footerRightPdfPCell = new PdfPCell(fotterRightPdfPTable);
footerRightPdfPCell.setBorder(Rectangle.NO_BORDER);
footterPdfPTable.addCell(footerRightPdfPCell);
//footterPdfPTable.writeSelectedRows(0, -1, 34, 803, writer.getDirectContent());
document.add(footterPdfPTable);

itext-rtf Table within a Cell

I'm using itext-rtf 2.1.7 to generate an RTF document.
The parameter table below for the method writeSectionHeaderTableInACell() will have two columns.
For each column, I need to insert a new inner Table, with two columns as well. This inner table will have the first column as left-aligned, and the second as right-aligned.
However, the code below causes a corrupted Table in the generated RTF document. The two inner Tables which supposed to be both on a single row, they appear as one row for each inner Table.
Anyone, has any idea why it seems we cannot add an inner Table within a Cell for RTF document? Thanks.
private static void writeSectionHeaderTableInACell(PdfPTable table) {
PdfPTable sectionTable1 = new PdfPTable(2);
Phrase phrase1 = new Phrase("? 1 ?", FontFactory.getFont("MS Mincho", 10, Font.NORMAL));
Paragraph p1 = new Paragraph(phrase1);
p1.setAlignment(Element.ALIGN_LEFT);
PdfPCell cell1 = new PdfPCell(p1);
sectionTable1.addCell(cell1);
Phrase phrase2 = new Phrase("2 Chi", FontFactory.getFont("MS Mincho", 10, Font.NORMAL));
Paragraph p2 = new Paragraph(phrase2);
p2.setAlignment(Element.ALIGN_RIGHT);
PdfPCell cell2 = new PdfPCell(p2);
sectionTable1.addCell(cell2);
table.addCell(new PdfPCell(sectionTable1));
PdfPTable sectionTable2 = new PdfPTable(2);
Phrase phrase3 = new Phrase("Section 1", FontFactory.getFont("Times New Roman", 10, Font.NORMAL));
Paragraph p3 = new Paragraph(phrase3);
p3.setAlignment(Element.ALIGN_LEFT);
PdfPCell cell3 = new PdfPCell(p3);
sectionTable2.addCell(cell3);
Phrase phrase4 = new Phrase("2 Eng", FontFactory.getFont("Times New Roman", 10, Font.NORMAL));
Paragraph p4 = new Paragraph(phrase4);
p4.setAlignment(Element.ALIGN_RIGHT);
PdfPCell cell4 = new PdfPCell(p4);
sectionTable2.addCell(cell4);
table.addCell(new PdfPCell(sectionTable2));
}

Why doesn't iText add tables which are near the page bottom to the document?

I have a lot of small tables, which i encapsulate in a sorrounding 1x1 table, and set the SplitRows attribute to false on the sorrounding table. This way, i can avoid my table getting split when it reaches the bottom of a page. When i get to the end of a page, and there is a little space for text, but not enough for the next table, iText doesn't add the table at all, but continues to add the next table in the list.
If there is not enough space for the table on the current page, id like to send it to the next. What can i do?
http://compgroups.net/comp.text.pdf/Avoid-page-breaks-in-PdfPTable-using-iText-1.2
This is my code:
public static void CreateMatrixProcentQuestionTable(ShowQuestionViewModel model, Document doc)
{
ShowMatrixQuestionViewModel sm = (ShowMatrixQuestionViewModel)model;
Font fontsize = new Font(Font.FontFamily.HELVETICA, 9f);
Font QuestionFont = new Font(Font.FontFamily.HELVETICA, 12f);
PdfPTable table = new PdfPTable(sm.columns.Count + 2);
// Tilføj spørgsmålet i en række for sig selv, ellers er der chance for at
// svarmulighederne ikke kommer med ved page breaks
PdfPCell question = new PdfPCell(new Phrase(sm.Question_Wording + Environment.NewLine, QuestionFont));
question.Border = Rectangle.NO_BORDER;
question.Colspan = table.NumberOfColumns;
table.AddCell(question);
// Tilføj et mellemrum mellem spørgsmålet og svarmulighederne
PdfPCell mellemrum = new PdfPCell(new Phrase(Environment.NewLine));
mellemrum.Border = Rectangle.NO_BORDER;
mellemrum.Colspan = table.NumberOfColumns;
table.AddCell(mellemrum);
// Tilføj rækker og kolonner
// Dette er den første tomme celle
table.AddCell(new PdfPCell(new Phrase("", fontsize)));
foreach (MatrixColumns column in sm.columns)
{
PdfPCell cell = new PdfPCell(new Phrase(column.Column_Description, fontsize));
cell.HorizontalAlignment = 1;
table.AddCell(cell);
}
PdfPCell ialt = new PdfPCell(new Phrase("I alt", fontsize));
ialt.HorizontalAlignment = 1;
table.AddCell(ialt);
foreach (var pair in sm.columnrow)
{
MatrixRows row = pair.Key;
PdfPCell rowcell = new PdfPCell(new Phrase(row.Row_Description == null ? "*" : row.Row_Description, fontsize));
rowcell.HorizontalAlignment = 1;
table.AddCell(rowcell);
foreach (MatrixColumns column in pair.Value)
{
PdfPCell cell = new PdfPCell(new Phrase("%", fontsize));
cell.HorizontalAlignment = Element.ALIGN_RIGHT;
table.AddCell(cell);
}
PdfPCell sumcell = new PdfPCell(new Phrase("100%", fontsize));
sumcell.HorizontalAlignment = Element.ALIGN_RIGHT;
table.AddCell(sumcell);
}
// Man laver en 1x1 table uden om den rigtige table, og sætter
// SplitRows = False. Dette gør at tabellen ikke bliver knækket over
// ved page breaks
PdfPTable sorroundingTable = new PdfPTable(1);
PdfPCell innerTable = new PdfPCell(table);
innerTable.Border = Rectangle.NO_BORDER;
sorroundingTable.AddCell(innerTable);
sorroundingTable.SplitRows = false;
doc.Add(sorroundingTable);
doc.Add(new Phrase(Environment.NewLine));
}
This solves the problem:
table.setKeepTogether(true)
document.add(table)

Categories