How to show two PDFTables next to each other in iText - java

I need your help in showing two PDFTables next to each other in iText. Now the first table is above the second table where now I need to keep them next to each other with a short space. Here is my code:
//First Table
dfPTable table = new PdfPTable(2);
Font earningsTitlefont = new Font(Font.TIMES_ROMAN,12, Font.BOLD);
PdfPCell c1 = new PdfPCell(new Phrase("Earnings Description",earningsTitlefont));
table.addCell(c1);
c1 = new PdfPCell(new Phrase("Earnings Amount",earningsTitlefont));
table.addCell(c1);
for (int i = 0; i < listEarnings.size(); i++) {
String temp1 = listEarnings.get(i).getEarningsDescriptionSS();
String temp2 = listEarnings.get(i).getEarningsAmountSS();
table.addCell(temp1);
table.addCell(temp2);
}
//Second Table
dfPTable tableDeductions = new PdfPTable(2);
Font fontTitleDeductions = new Font(Font.TIMES_ROMAN,12, Font.BOLD);
PdfPCell c2= new PdfPCell(new Phrase("Deductions Description",fontTitleDeductions ));
tableDeductions.addCell(c2);
c2 = new PdfPCell(new Phrase("Deductions Amount",fontTitleDeductions));
tableDeductions.addCell(c2);
for (int i = 0; i < listDeductionss.size(); i++) {
String temp3 = listDeductions.get(i).getDeductionssDescriptionSS();
String temp4 = listDeductions.get(i).getDeductionssAmountSS();
tableDeductions.addCell(temp3);
tableDeductions.addCell(temp4);
}
doc.add(table);
doc.add(Chunk.NEWLINE);
doc.add(tableDeductions);

(side by side tables) I'm not sure just check it.
// Main table
PdfPTable mainTable = new PdfPTable(2);
mainTable.setWidthPercentage(100.0f);
// First table
PdfPCell firstTableCell = new PdfPCell();
firstTableCell.setBorder(PdfPCell.NO_BORDER);
PdfPTable firstTable = new PdfPTable(2);
//......... add some cells here ...........
firstTableCell.addElement(firstTable);
mainTable.addCell(firstTableCell);
// Second table
PdfPCell secondTableCell = new PdfPCell();
secondTableCell.setBorder(PdfPCell.NO_BORDER);
PdfPTable secondTable = new PdfPTable(2);
//......... add some cells here ...........
secondTableCell.addElement(secondTable);
mainTable.addCell(secondTableCell);
paragraph.add(mainTable);
document.add(paragraph);

use
doc.add(new Phrase("\n"));
in place of
doc.add(Chunk.NEWLINE);
definably it's work for you. :)

Related

How to set rowspan in itext using do while loop in android studio

My datas in PDF consists of 20 rows. I'm using do while loop for getting this 20 values from database. I trying to set rowspan for the last set of datas, which am unable to do so, as the last values are getting broken. Below is my codings...
PdfPTable tablel = new PdfPTable(3);
tablel.setWidthPercentage(100);
//Header
PdfPCell cell1 = new PdfPCell(new Phrase("S No"));
PdfPCell cell2 = new PdfPCell(new Phrase("Parameter"));
PdfPCell cell3 = new PdfPCell(new Phrase("Unit"));
tablel.addCell(cell1);
tablel.addCell(cell2);
tablel.addCell(cell3);
//Datas
Cursor c= dbs.query("kiosk_trn_tsoilparameter", new String[]{"soilparam_gid","soilparam_soil_gid","soil_parameter","soil_uom","soil_results"
}, "soilparam_soil_gid" + "=? COLLATE NOCASE",
new String[]{id}, null, null, null, null);
int i = 1;
if (c.moveToFirst()) {
do {
//to get increment values of i
String group = String.valueOf(i);
cell1 = new PdfPCell(new Phrase(group));
tablel.addCell(cell1);
String selectQuery1 = "SELECT * FROM core_mst_tmaster....";
Cursor cursor1 = dbs.rawQuery(selectQuery1, null);
if(cursor1.moveToNext())
{
String machine = cursor1.getString(0);
cell1 = new PdfPCell(new Phrase(machine));
tablel.addCell(cell1);
}
String selectQuery2 = "SELECT * FROM core_mst_tmaster ....";
Cursor cursor2 = dbs.rawQuery(selectQuery3, null);
if(cursor2.moveToNext())
{
String machine = cursor2.getString(0);
cell2 = new PdfPCell(new Phrase(machine));
tablel.addCell(cell2);
}
String selectQuery3 = "SELECT * FROM core_mst_tmaster.... ";
Cursor cursor3 = dbs.rawQuery(selectQuery3, null);
if(cursor3.moveToNext())
{
String machine = cursor3.getString(0);
cell3 = new PdfPCell(new Phrase(machine));
cell3.setRowspan(3);
tablel.addCell(cell3);
}
i++;
while (c.moveToNext());
}
document.add(tablel);
I have also tried to put if loop to get first set of values and for the remaining set of values I assigned empty values and tried to assigned rowspan for the remaining set of values, but still it is not working.
Below is the coding...
//to get first set of values
if(i==1){
String machine = cursor3.getString(0);
cell3 = new PdfPCell(new Phrase(machine));
tablel.addCell(cell3);
}else{
// to get remaining values
String machine = cursor3.getString(0);
cell3 = new PdfPCell(new Phrase(machine));
cell3.setRowspan(3);
tablel.addCell(cell3);
}
```
Any codings I missed out...please help me out...

Header Is Overlapped By Main Content In IText PDF

I'm Using iText 2.1.7, some table's are overlapping header when the table is moved to new page how can i fix this
i tried changing margins,but no luck with it..
i expected it to move below the header automatically but instead it overlaps it
here are some images :
it's happening only on some pages where table automatically moves to new page because of space constraints
Document document = new Document(PageSize.A4,50, 45, 50, 60);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
PdfWriter writer = PdfWriter.getInstance(document, stream);
// PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:/Users/sukant.singhal/Desktop/ApplicationFormPdf/"+fileName));
BaseFont bf_times = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", false);
Font headerFont = new Font(Font.TIMES_ROMAN, 14, Font.NORMAL);
Font abc = new Font(Font.TIMES_ROMAN, 6, Font.NORMAL);
abc.setColor(255, 0, 0);
Font headerFont1 = new Font(Font.TIMES_ROMAN, 10, Font.NORMAL);
BaseFont base = BaseFont.createFont("/arial.ttf", BaseFont.WINANSI, false);
Font footFont = new Font(base, 8);
Font headerBold = new Font(base,7,Font.BOLD);
Font headerBoldBig = new Font(base,10,Font.BOLD);
// headers and footers must be added before the document is opened
HeaderFooter footer = new HeaderFooter(new Phrase("App ID : "+id+" | Page No. : ", footFont), true);
footer.setBorder(Rectangle.NO_BORDER);
footer.setAlignment(Element.ALIGN_RIGHT);
document.setFooter(footer);
document.open();
PdfContentByte cb = writer.getDirectContent();
Phrase vHeaderphrase = new Phrase();
PdfPTable table = headerandborder(document,headerFont,headerFont1,writer,cb);
vHeaderphrase.add(table);
HeaderFooter head = new HeaderFooter(vHeaderphrase, false);
head.setBorder(Rectangle.NO_BORDER);
document.setHeader(head);
//Adding First table
List<ManagementDetails> managementDetails = promoterdirectorDTO.getManagementDetails();
if(managementDetails!=null && managementDetails.size()>0) {
for(ManagementDetails a :managementDetails)
{
document.add(new Paragraph("\n\n"));
managementDetailsTable(footFont, document,lkpmap,a, clipServiceManager);
}
using the helper methods
public static void managementDetailsTable(Font footFont,Document document, Map<Long, String> lkpmap, ManagementDetails a, ClipServiceManager clipServiceManager) {
try {
document.add(new Paragraph("\n"));
PdfPTable ManagementDetailTable = new PdfPTable(5);
ManagementDetailTable.setWidthPercentage(95);
ManagementDetailTable.setKeepTogether(true);
float[] columnwidth = {1,1,1,1,1};
ManagementDetailTable.setWidths(columnwidth);
//Header
PdfPCell sectionHeaderCell = new PdfPCell(new Phrase("Management Details\r\n" +
" header",footFont));
sectionHeaderCell.setBackgroundColor(new Color(91,155,253));
sectionHeaderCell.setHorizontalAlignment(Element.ALIGN_LEFT);
sectionHeaderCell.setPadding(3);
sectionHeaderCell.setBorderWidth(.5f);
sectionHeaderCell.setColspan(5);
ManagementDetailTable.addCell(sectionHeaderCell);
ManagementDetailTable.completeRow();
//Row 1
PdfPCell pdfcell;
pdfcell = new PdfPCell(new Phrase("Type",footFont));
pdfcell.setColspan(1);
String s= null;
ManagementDetailTable.addCell(pdfcell);
/*if(a.getType()!=null && !a.getType().isEmpty()) {
s= lkpmap.get(Long.parseLong(a.getType()));
}*/
pdfcell = new PdfPCell(new Phrase(nullCheck(a.getType()),footFont));
s=null;
pdfcell.setColspan(4);
ManagementDetailTable.addCell(pdfcell);
ManagementDetailTable.completeRow();
//Row 2
PdfPCell pdfcell1;
pdfcell1 = new PdfPCell(new Phrase("Title",footFont));
ManagementDetailTable.addCell(pdfcell1);
if(a.getTitle()!=null && !a.getTitle().isEmpty()) {
s= lkpmap.get(Long.parseLong(a.getTitle()));
}
pdfcell1 = new PdfPCell(new Phrase(nullCheck(s),footFont));
s=null;
ManagementDetailTable.addCell(pdfcell1);
pdfcell1 = new PdfPCell(new Phrase("First Name",footFont));
ManagementDetailTable.addCell(pdfcell1);
pdfcell1 = new PdfPCell(new Phrase(nullCheck(a.getFirstName()),footFont));
ManagementDetailTable.addCell(pdfcell1);
/*Image image;
InputStream is = a.getImage().getInputStream();
byte[] bytes = IOUtils.toByteArray(is);
image = Image.getInstance(bytes);
*/
pdfcell1 = new PdfPCell(new Phrase("Affix recent Photo\r\n" +
"Cross sign",footFont));
pdfcell1.setRowspan(5);
pdfcell1.setHorizontalAlignment(Element.ALIGN_CENTER);
pdfcell1.setVerticalAlignment(Element.ALIGN_CENTER);
ManagementDetailTable.addCell(pdfcell1);
ManagementDetailTable.completeRow();
//Row 3
PdfPCell pdfcell2;
pdfcell2 = new PdfPCell(new Phrase("Middle Name",footFont));
ManagementDetailTable.addCell(pdfcell2);
pdfcell2 = new PdfPCell(new Phrase(nullCheck(a.getMiddleName()),footFont));
ManagementDetailTable.addCell(pdfcell2);
pdfcell2 = new PdfPCell(new Phrase("Last Name",footFont));
ManagementDetailTable.addCell(pdfcell2);
pdfcell2 = new PdfPCell(new Phrase(nullCheck(a.getLastName()),footFont));
ManagementDetailTable.addCell(pdfcell2);
ManagementDetailTable.completeRow();
//Row 4
PdfPCell pdfcell3;
pdfcell3 = new PdfPCell(new Phrase("Pan No",footFont));
pdfcell3.setColspan(1);
ManagementDetailTable.addCell(pdfcell3);
pdfcell3 = new PdfPCell(new Phrase(nullCheck(a.getPanNo()),footFont));
pdfcell3.setColspan(3);
ManagementDetailTable.addCell(pdfcell3);
ManagementDetailTable.completeRow();
//Row 5
PdfPCell pdfcell4;
pdfcell4 = new PdfPCell(new Phrase("Date of Birth",footFont));
pdfcell4.setColspan(1);
ManagementDetailTable.addCell(pdfcell4);
pdfcell4 = new PdfPCell(new Phrase(nullCheck(a.getDob()),footFont));
pdfcell4.setColspan(3);
ManagementDetailTable.addCell(pdfcell4);
ManagementDetailTable.completeRow();
//Row 6
PdfPCell pdfcell5;
pdfcell5 = new PdfPCell(new Phrase("Din / DPIN No. (if applicable)",footFont));
pdfcell5.setColspan(1);
ManagementDetailTable.addCell(pdfcell5);
pdfcell5 = new PdfPCell(new Phrase(nullCheck(a.getDin()),footFont));
pdfcell5.setColspan(3);
ManagementDetailTable.addCell(pdfcell5);
ManagementDetailTable.completeRow();
//Row 7
PdfPCell pdfcell6;
pdfcell6 = new PdfPCell(new Phrase("Father’s / Spouse Name",footFont));
pdfcell6.setColspan(1);
ManagementDetailTable.addCell(pdfcell6);
pdfcell6 = new PdfPCell(new Phrase(nullCheck(a.getFatherOrSpouse()),footFont));
pdfcell6.setColspan(4);
ManagementDetailTable.addCell(pdfcell6);
ManagementDetailTable.completeRow();
//Row 8
PdfPCell pdfcell7;
pdfcell7 = new PdfPCell(new Phrase("Email ID",footFont));
pdfcell7.setColspan(1);
ManagementDetailTable.addCell(pdfcell7);
pdfcell7 = new PdfPCell(new Phrase(nullCheck(a.getEmail()),footFont));
pdfcell7.setColspan(4);
ManagementDetailTable.addCell(pdfcell7);
ManagementDetailTable.completeRow();
//Row 9
PdfPCell pdfcell8;
pdfcell8 = new PdfPCell(new Phrase("Residential Status",footFont));
pdfcell8.setColspan(1);
ManagementDetailTable.addCell(pdfcell8);
if(a.getResidentialStatus()!=null && !a.getResidentialStatus().isEmpty()) {
s= lkpmap.get(Long.parseLong(a.getResidentialStatus()));
}
pdfcell8 = new PdfPCell(new Phrase(nullCheck(s),footFont));
s=null;
pdfcell8.setColspan(4);
ManagementDetailTable.addCell(pdfcell8);
ManagementDetailTable.completeRow();
//Row 10
PdfPCell pdfcell9;
pdfcell9 = new PdfPCell(new Phrase("Nationality",footFont));
pdfcell9.setColspan(1);
ManagementDetailTable.addCell(pdfcell9);
if(a.getNationality()!=null && !a.getNationality().isEmpty()) {
s= lkpmap.get(Long.parseLong(a.getNationality()));
}
pdfcell9 = new PdfPCell(new Phrase(nullCheck(s),footFont));
s=null;
pdfcell9.setColspan(4);
ManagementDetailTable.addCell(pdfcell9);
ManagementDetailTable.completeRow();
//Row 11
PdfPCell pdfcell10;
pdfcell10 = new PdfPCell(new Phrase("Telephone No",footFont));
pdfcell10.setColspan(1);
ManagementDetailTable.addCell(pdfcell10);
pdfcell10 = new PdfPCell(new Phrase(nullCheck(a.getTelephoneNo()),footFont));
ManagementDetailTable.addCell(pdfcell10);
pdfcell10 = new PdfPCell(new Phrase("Mobile No",footFont));
ManagementDetailTable.addCell(pdfcell10);
pdfcell10 = new PdfPCell(new Phrase(nullCheck(a.getMobileNo()),footFont));
pdfcell10.setColspan(2);
ManagementDetailTable.addCell(pdfcell10);
ManagementDetailTable.completeRow();
//Row 12
PdfPCell pdfcell11;
pdfcell11 = new PdfPCell(new Phrase("Address for Correspondence",footFont));
pdfcell11.setColspan(1);
pdfcell11.setRowspan(3);
ManagementDetailTable.addCell(pdfcell11);
pdfcell11 = new PdfPCell(new Phrase(nullCheck(a.getAddressLine1()),footFont));
pdfcell11.setColspan(2);
ManagementDetailTable.addCell(pdfcell11);
pdfcell11 = new PdfPCell(new Phrase("Zip Code",footFont));
ManagementDetailTable.addCell(pdfcell11);
pdfcell11 = new PdfPCell(new Phrase(nullCheck(a.getZipCode()),footFont));
ManagementDetailTable.addCell(pdfcell11);
ManagementDetailTable.completeRow();
//Row 13
pdfcell11 = new PdfPCell(new Phrase(nullCheck(a.getAddressLine2()),footFont));
pdfcell11.setColspan(2);
ManagementDetailTable.addCell(pdfcell11);
pdfcell11 = new PdfPCell(new Phrase("City",footFont));
ManagementDetailTable.addCell(pdfcell11);
City c= new City();
c.setState(new State());
Country country = new Country();
if(a.getCity()!=null && !a.getCity().isEmpty()) {
c =clipServiceManager.getCityService().getCityById(Short.valueOf(a.getCity()));
}
pdfcell11 = new PdfPCell(new Phrase(nullCheck(c.getCityName()),footFont));
ManagementDetailTable.addCell(pdfcell11);
ManagementDetailTable.completeRow();
//Row 14
pdfcell11 = new PdfPCell(new Phrase(" ",footFont));
pdfcell11.setColspan(2);
ManagementDetailTable.addCell(pdfcell11);
pdfcell11 = new PdfPCell(new Phrase("District",footFont));
ManagementDetailTable.addCell(pdfcell11);
pdfcell11 = new PdfPCell(new Phrase(nullCheck(a.getDistrict()),footFont));
ManagementDetailTable.addCell(pdfcell11);
ManagementDetailTable.completeRow();
//Row 15
ManagementDetailTable.addCell(new PdfPCell(new Phrase("State",footFont)));
ManagementDetailTable.addCell(new PdfPCell(new Phrase(nullCheck(c.getState().getStateName()),footFont)));
ManagementDetailTable.addCell(new PdfPCell(new Phrase("Country",footFont)));
if(a.getState()!=null && c.getState().getCountry()!=null) {
country= clipServiceManager.getCountryService().getCountryById(Short.valueOf(c.getState().getCountry().getCountryId().toString()));
}
PdfPCell cell = new PdfPCell(new Phrase(nullCheck(country.getCountryName()),footFont));
cell.setColspan(2);
c= new City();
c.setState(new State());
country = new Country();
ManagementDetailTable.addCell(cell);
//Row 16
if(a.getAddressProof()!=null && !a.getAddressProof().isEmpty()) {
s= lkpmap.get(Long.parseLong(a.getAddressProof()));
}
ManagementDetailTable.addCell(new PdfPCell(new Phrase("Address Proof Submitted*",footFont)));
ManagementDetailTable.addCell(new PdfPCell(new Phrase(nullCheck(s),footFont)));
s= null;
ManagementDetailTable.addCell(new PdfPCell(new Phrase("ID Proof Submitted*",footFont)));
cell = new PdfPCell(new Phrase(nullCheck(a.getIdProof()),footFont));
cell.setColspan(2);
ManagementDetailTable.addCell(cell);
//Row 17
if(a.getRoleAssignedbyTreds()!=null && !a.getRoleAssignedbyTreds().isEmpty()) {
s= lkpmap.get(Long.parseLong(a.getRoleAssignedbyTreds()));
}
ManagementDetailTable.addCell(new PdfPCell(new Phrase("Role Assigned under TReDS",footFont)));
ManagementDetailTable.addCell(new PdfPCell(new Phrase(nullCheck(s),footFont)));
s= null;
ManagementDetailTable.addCell(new PdfPCell(new Phrase("Date of Authorisation for TReDS",footFont)));
cell = new PdfPCell(new Phrase(nullCheck(a.getTredsAuthorizationDate()),footFont));
cell.setColspan(2);
ManagementDetailTable.addCell(cell);
//Row 18
if(a.getCategory()!=null && !a.getCategory().isEmpty()) {
s= lkpmap.get(Long.parseLong(a.getCategory()));
}
ManagementDetailTable.addCell(new PdfPCell(new Phrase("Category",footFont)));
ManagementDetailTable.addCell(new PdfPCell(new Phrase(nullCheck(s),footFont)));
s=null;
if(a.getWomenEntrepreneur()!=null && !a.getWomenEntrepreneur().isEmpty()) {
s= lkpmap.get(Long.parseLong(a.getWomenEntrepreneur()));
}
ManagementDetailTable.addCell(new PdfPCell(new Phrase("Women Entrepreneur",footFont)));
cell = new PdfPCell(new Phrase(nullCheck(s),footFont));
cell.setColspan(2);
ManagementDetailTable.addCell(cell);
s= null;
ManagementDetailTable.setKeepTogether(true);
document.add(ManagementDetailTable);
} catch (Exception e) {
e.printStackTrace();
}
}
and
public static PdfPTable headerandborder(Document document, Font headerFont,Font headerFont1, PdfWriter writer,PdfContentByte cb ) {
try {
PdfPTable fileHeader = new PdfPTable(2);
fileHeader.getDefaultCell().setBorderColor(Color.WHITE);
fileHeader.setWidthPercentage(100);
float[] fileHeaderColumnWidth = { 10, 60 };
fileHeader.setWidths(fileHeaderColumnWidth);
Paragraph footerPara = new Paragraph("cba ", headerFont);
footerPara.setAlignment(Element.ALIGN_RIGHT); //ALIGN Right
Paragraph footerHeadOfficeAdd = new Paragraph("APPLICATION FORM FOR SELLERS", headerFont1); // Phrase 1
footerHeadOfficeAdd.setAlignment(Element.ALIGN_MIDDLE);
Paragraph headerPara = new Paragraph();
headerPara.add( footerPara ); // Company Name
headerPara.add( footerHeadOfficeAdd ); // Form Title
PdfPCell fileHeaderCell = new PdfPCell(new Phrase("REdskvbkjds ", headerFont));
fileHeaderCell.setBackgroundColor(Color.WHITE);
fileHeaderCell.setHorizontalAlignment(Element.ALIGN_LEFT);
fileHeaderCell.setVerticalAlignment(Element.ALIGN_TOP);
//fileHeaderCell.setPadding(20);
fileHeaderCell.setPaddingLeft(40);
fileHeaderCell.setBorderColor(Color.WHITE);
fileHeaderCell.addElement(headerPara);
Image image;
InputStream is = new ClassPathResource("/logo.png").getInputStream();
byte[] bytes = IOUtils.toByteArray(is);
image = Image.getInstance(bytes);
fileHeader.addCell(image);
fileHeader.addCell(fileHeaderCell);
document.add(fileHeader);
return fileHeader;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}

IText 5 Pdf Table column width not working

I'm trying to create a PDF file formatted for an Avery name label 5395, which has 8 labels on an 8 1/2 x 11 sheet. I set up a PdfPTable with measurements from the actual sheet, and write it out. The problem is the column widths and overall table width are wrong. Instead of columns 3.5" they are about 2.8". My code is below. What am I missing?
public class TryNameLabelPdf {
public static void main(String[] args) {
PdfPTable theTable;
Document theDoc;
try {
// Avery 5395 Name Badges
theDoc = new Document();
Rectangle pageSize = new Rectangle(612f,792f); // 8.5*72 = 612, 11*72= 792
theDoc.setPageSize(pageSize);
theDoc.setMargins(49.5f, 49.5f,41.06f, 41.06f); // left, right, top, bottom
theTable = new PdfPTable(2);
float[] columnWidths = {252f, 252f}; // 3.5*72 = 252
theTable.setWidths(columnWidths);
theTable.setTotalWidth(504f); // 2*252 = 504
PdfWriter.getInstance(theDoc, new FileOutputStream("MyTestTable.pdf"));
theDoc.open();
PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1.1"));
cell1.setFixedHeight(174);
PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 1.2"));
cell2.setFixedHeight(174);
PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 2.1"));
cell3.setFixedHeight(174);
PdfPCell cell4 = new PdfPCell(new Paragraph("Cell 2.2"));
cell4.setFixedHeight(174);
PdfPCell cell5 = new PdfPCell(new Paragraph("Cell 3.1"));
cell5.setFixedHeight(174);
PdfPCell cell6 = new PdfPCell(new Paragraph("Cell 3.2"));
cell6.setFixedHeight(174);
PdfPCell cell7 = new PdfPCell(new Paragraph("Cell 4.1"));
cell7.setFixedHeight(174);
PdfPCell cell8 = new PdfPCell(new Paragraph("Cell 4.2"));
cell8.setFixedHeight(174);
theTable.addCell(cell1);
theTable.addCell(cell2);
theTable.addCell(cell3);
theTable.addCell(cell4);
theTable.addCell(cell5);
theTable.addCell(cell6);
theTable.addCell(cell7);
theTable.addCell(cell8);
theDoc.add(theTable);
theDoc.close();
} catch (DocumentException ex) {
Logger.getLogger(TryNameLabelPdf.class.getName()).log(Level.SEVERE, null, ex);
} catch (FileNotFoundException ex) {
Logger.getLogger(TryNameLabelPdf.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Found the problem! Turns out the column width needs to be locked after setting:
theTable.setLockedWidth(true); // Without this the table width won't stick!

How to create new page based on the condition using itextpdf and java

I am generating pdf using itextpdf library and created table with different fields. In which Accode is one field it represents the account code of each employee.I am getting the Accode by using type,type1 fields from database and assigning the value for it.
Here type = M(Manager),type1=G(General), So I concatenated these two fields MN and assigning the Accode for employee based on it.
For Ex: Employee name: Rajesh has Accode: WA1555
Now i want to generate separate table in pdf based on the Accode.
Ex: The Employees having the Accode WA1555 in one table, and employees having other Accode like WD1589 in one table.
I need to check the Accode for the purpose of getting separate table for each Accode.
table.addCell(createRowCell(getAccode.get(0)));
At present i am getting all in one table.This is my code
public Element createMedicalOrgTable() {
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
// get current date time with Date()
Date date = new Date();
PdfPTable table = new PdfPTable(8);
int orderTotal, total = 0;
// table.setWidthPercentage(100);
table.setWidths(new int[] { 10, 10, 15, 60, 15, 18, 15, 18 });
table.setTotalWidth(550);
PdfPCell cell = new PdfPCell();
table.setLockedWidth(true);
table.getDefaultCell().setFixedHeight(22);
StringBuilder addr;
String name, addr1, addr2, addr3;
Font font = new Font(FontFamily.HELVETICA, 12, Font.BOLD,
BaseColor.BLACK);
Font font1 = new Font(FontFamily.HELVETICA, 8, Font.NORMAL,
BaseColor.BLACK);
Paragraph para = new Paragraph(
"The Company\nMedical Payment Voucher-Cum-Bill",
font);
para.setLeading(0, 1);
para.setAlignment(Element.ALIGN_CENTER);
cell.setColspan(7);
cell.setMinimumHeight(50);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.addElement(para);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
Paragraph para1 = new Paragraph("Orginal\nFor\nPayment", font1);
para1.setAlignment(Element.ALIGN_CENTER);
cell = new PdfPCell();
cell.getBorder();
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setColspan(1);
cell.addElement(para1);
table.addCell(cell);
String reportSql = "select rawdata.type1,rawdata.type,rawdata.brno,medtest.MCODE,medtest.NAME,medtest.ADDRESS1,medtest.ADDRESS2,medtest.ADDRESS3,rawdata.pamount,rawdata.tax,rawdata.pamount1 from rawdata inner join medtest on medtest.MCODE = rawdata.mcode";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(reportSql);
cell = new PdfPCell(new Paragraph("Tr Code", font1));
cell.setColspan(4);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("Division Code ", font1));
cell.setColspan(4);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("Voucher Date "
+ dateFormat.format(date), font1));
cell.setColspan(4);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("Voucher Number ", font1));
cell.setColspan(4);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("For ", font1));
cell.setColspan(4);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("By Cheque", font1));
cell.setColspan(4);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
table.addCell(createLabelCell("S.NO."));
table.addCell(createLabelCell("BR NO"));
table.addCell(createLabelCell("MED CODE"));
table.addCell(createLabelCell("PAYEE"));
table.addCell(createLabelCell("A/C CODE"));
table.addCell(createLabelCell("AMOUNT CLIAMED"));
table.addCell(createLabelCell("TAX(CR)"));
table.addCell(createLabelCell("NET PAYMENT\n(AFTER TAX)"));
int i = 0;
while (rs.next()) {
addr = new StringBuilder();
String data = rs.getString("type") + rs.getString("type1");
ArrayList<String> getAccode = getData(data);
table.addCell(createRowCell(String.valueOf(++i)));
table.addCell(createRowCell(rs.getString("brno")));
table.addCell(createRowCell(rs.getString("mcode")));
name = rs.getString("NAME");
addr1 = rs.getString("ADDRESS1");
addr2 = rs.getString("ADDRESS2");
addr3 = rs.getString("ADDRESS3");
String nameAddr = addr.append(name).append(",").append(addr1)
.append(",").append(addr2).append(",").append(addr3)
.toString();
table.addCell(createRowCell(nameAddr));
table.addCell(createRowCell(getAccode.get(0)));
table.addCell(createRowCell(rs.getString("pamount")));
table.addCell(createRowCell(rs.getString("tax")));
table.addCell(createRowCell(rs.getString("pamount1")));
orderTotal = rs.getInt("pamount1");
total = total + orderTotal;
}
Paragraph para2 = new Paragraph("Total Amount Rs." + total, font1);
para2.setAlignment(Element.ALIGN_RIGHT);
cell = new PdfPCell();
cell.setBorder(Rectangle.NO_BORDER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(8);
cell.addElement(para2);
table.addCell(cell);
Paragraph para3 = new Paragraph(
"Cheque Favouring As Per Name Indicated Above", font1);
para3.setAlignment(Element.ALIGN_LEFT);
cell = new PdfPCell();
cell.setBorder(Rectangle.NO_BORDER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(8);
cell.addElement(para3);
table.addCell(cell);
Paragraph para4 = new Paragraph("Pay Rs." + total, font1);
para4.setAlignment(Element.ALIGN_LEFT);
cell = new PdfPCell();
cell.setBorder(Rectangle.NO_BORDER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(8);
cell.addElement(para4);
table.addCell(cell);
Paragraph para5 = new Paragraph("(Rupees "
+ convertNumToWord(total) + " only)", font1);
para5.setAlignment(Element.ALIGN_LEFT);
cell = new PdfPCell();
cell.setBorder(Rectangle.NO_BORDER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setColspan(8);
cell.addElement(para5);
table.addCell(cell);
table.setSpacingAfter(30.0f);
cell = new PdfPCell(new Phrase("Asstt.", font1));
cell.setColspan(2);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Dm(A/CS)", font1));
cell.setColspan(2);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Dy.Finance Manager", font1));
cell.setColspan(4);
cell.setBorder(Rectangle.NO_BORDER);
table.addCell(cell);
return table;
}

Java iText: text below image

in a PdfPCell i'd like to put multiple images, and a text below each image.
i tried with this code:
private PdfPTable tabellaRighe() throws BadElementException, MalformedURLException, IOException, DocumentException {
int[] cellWidth = {500, 95};
PdfPTable table = new PdfPTable(2);
table.setWidths(cellWidth);
table.setTotalWidth(PageSize.A4.getWidth() - 45);
table.setLockedWidth(true);
PdfPCell cell;
cell = new PdfPCell();
cell.setBorderWidth(0);
Paragraph p = new Paragraph();
for (int i = 0; i < 4; i++) {
Image image = Image.getInstance(imgNd);
image.scaleToFit(300, 135);
Phrase ph = new Phrase();
ph.add(new Chunk(image, 0, 0, true));
ph.add("CIAO");
p.add(ph);
}
cell.addElement(p);
table.addCell(cell);
cell = new PdfPCell();
cell.setBorderWidthBottom(1);
cell.setBorderWidthLeft(1);
cell.setBorderWidthRight(1);
cell.setBorderWidthTop(1);
table.addCell(cell);
}
but the text is not below the image, but shifted to the right.
how can I put the text below each image?
Add \n before and after text.
ph.add("\nCIAO\n");
\n produce new line.

Categories

Resources