I want to add a table in PDF using iTextPDF 5.5 version at middle of the page or at some random place.
I have header on each page below which I have few lines of paragraph. Below which table should come.
HeaderFooterPageEvent event = new HeaderFooterPageEvent();
writer.setPageEvent(event);
document.open(); //Adding Header on first page
for(int i= 0 ; i< quotationDTOs.size(); i++) {
if(i != 0)
document.newPage(); // Adding header on new page
WriteReference(quotationDTOs.get(i), document, writer); //Adding para here
WriteModels(quotationDTOs.get(i), document, writer); // Adding table here
}
WriteModels(QuotationDTO quotationDTOTemp, Document document, PdfWriter writer)
{
PdfPTable table = new PdfPTable(1);
table.setTotalWidth(100);
table.setWidthPercentage(100);
PdfPCell cell = new PdfPCell(new Phrase("Some text here"));
cell.setFixedHeight(13);
cell.setBorder(Rectangle.NO_BORDER);
cell.setColspan(1);
cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
table.addCell(cell);
document.add(table);QuotationDTO quotationDTOTemp, Document document, PdfWriter writer
}
how to generate pdf for below content using itext5.
I am able to get simple table using itext. but for above image how to achieve this?
Below is my code. it is not showing image in the table.
Paragraph paragraph = new Paragraph();
PdfPCell cell = null;
// 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(1);
mainTable.setHorizontalAlignment(Element.ALIGN_LEFT);
Image img = Image.getInstance(appIconNameWithPath);
//img.setWidthPercentage(10);
cell = new PdfPCell(img, true);
cell.setBorder(PdfPCell.NO_BORDER);
firstTable.addCell(cell);
mainTable.addCell(firstTableCell);
// Second table
PdfPCell secondTableCell = new PdfPCell();
secondTableCell.setBorder(PdfPCell.NO_BORDER);
PdfPTable secondTable = new PdfPTable(2);
secondTable.setHorizontalAlignment(Element.ALIGN_RIGHT);
secondTableCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell = new PdfPCell(new Phrase(appName));
cell.setBorder(PdfPCell.NO_BORDER);
secondTable.addCell(cell);
cell = new PdfPCell(new Phrase(packageName));
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell.setBorder(PdfPCell.NO_BORDER);
secondTable.addCell(cell);
If the content of cell is bigger than height of cell in the height-fixed table, the content does not display.
This is probable a specification of iText library, but is there any way to make the content visible in iText7 in this case?
I'm migrating from iText5 to iText7.
Even though I set the same value(table and cell height, padding value, font size) as iText5, the content of cell does not display in iText7.
If I set the TopPadding and BottomPadding of Cell to 0, the content of cell does display.
I don't think that iText5 and iText have diffent coordinates and units.
How do I get the same result as iText5?
iText 5's code
PdfPTable ptable = new PdfPTable(1);
ptable.setTotalWidth(143f);
ptable.setWidthPercentage(100f);
PdfPCell pcell = new PdfPCell();
pcell.setPadding(2f);
pcell.setUseAscender(true);
pcell.setUseDescender(true);
pcell.setFixedHeight(16.05603f);
Paragraph p = new Paragraph();
p.setLeading(4.032f, 1.0f);
p.setSpacingBefore(0f);
p.setSpacingAfter(0f);
Chunk str = new Chunk("Sample");
Font font = FontFactory.getFont("batang", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
font.setSize(12f);
str.setFont(font);
p.add(str);
pcell.addElement(p);
ptable.addCell(pcell);
ptable.writeSelectedRows(0, -1, 300f, 150f, contentByte);
iText 7's code
Table table = new Table(new float[] {143f});
table.setWidthPercent(100f);
table.setHeight(16.05603f);
Cell cell = new Cell();
cell.setPadding(2f);
cell.setHeight(16.05603f);
Paragraph p = new Paragraph();
p.setFixedLeading(4.032f);
p.setMultipliedLeading(1.0f);
p.setMarginTop(0f);
p.setMarginBottom(0f);
PdfFont font = PdfFontFactory.createRegisteredFont("batang", PdfEncodings.IDENTITY_H, true);
Text str = new Text("Sample").setFontSize(12f).setFont(font);
p.add(str);
cell.add(p);
table.addCell(cell);
table.setFixedPosition(300f, 150f, 143f);
document.add(table);
I'm using itextpdf to create my pdf with tables. While creating table i need to align some column to right, but its now working properly , can you guys help me.
I tried googling too, but didt work out for me. im using itextpdf 5.4 version.
public void generateMonthlySubReport(String[][] StrArray,String dueMonth,int Amt){
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(MON_SUB_FILE));
PdfPTable pt = new PdfPTable(StrArray[0].length);
pt.setTotalWidth(new float[]{ 55,120,360,140});
pt.setLockedWidth(true);
PdfPCell pcell = new PdfPCell();
document.open();
addKvLogo(document);
Chunk glue = new Chunk(new VerticalPositionMark());
Paragraph p1 = new Paragraph("Monthly Subscription Report",catFont);
p1.setAlignment(Element.ALIGN_CENTER);
addEmptyLine(p1,2);
document.add(p1);
Paragraph p2 = new Paragraph("Month : "+dueMonth);
p2.add(new Chunk(glue));
p2.add("Per Member : Rs."+Amt);
addEmptyLine(p2,2);
document.add(p2);
for(int i=0;i<StrArray.length;i++){
for(int j=0;j<StrArray[i].length;j++){
pcell = new PdfPCell();
if(i==0){
pcell.setBackgroundColor(BaseColor.LIGHT_GRAY);
}else{
pcell.setBackgroundColor(BaseColor.WHITE);
}
pcell.setUseAscender(true);
pcell.setMinimumHeight(22);
pcell.setPaddingLeft(10);
pcell.setHorizontalAlignment(Element.ALIGN_RIGHT);
pcell.setVerticalAlignment(Element.ALIGN_MIDDLE);
pcell.addElement(new Phrase(StrArray[i][j]));
pt.addCell(pcell);
}
}
pt.setTotalWidth(PageSize.A4.getWidth()-(document.leftMargin()*2));
pt.setLockedWidth(true);
document.add(pt);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
} `
You are mixing text mode with composite mode.
This is text mode:
pcell = new PdfPCell(new Phrase(StrArray[i][j]));
pcell.setHorizontalAlignment(Element.ALIGN_RIGHT);
In this case, the alignment of the cell will be used for the alignment of the text.
This is composite mode:
pcell = new PdfPCell();
Paragraph p = new Parapgraph(StrArray[i][j])
p.setAlignment(Element.ALIGN_RIGHT);
pcell.addElement(p);
In this case, the alignment of the cell is ignored, in favor of the alignment of the element.
How to know the difference between text mode and composite mode?
iText automatically switches from text mode to composite mode in a PdfPCell the moment you use the addElement() method. As soon as you do this, some properties defined at the cell level are ignored. This explains why the content you are adding isn't right-aligned.
I have a JSP page which is simply getting parameters, querying database, generating invoice PDF, and sending silently to the default printer. I use itext library.
The invoice has to be printed on a dot-matrix printer with continuous paper.
Each invoice page size is sized a5 landscape.
If i choose the pagesize as a5, code produces a PDF as its seen on here
a5 portrait
when its printed, it prints one page and leave another page blank. User has to scroll the paper back manually.
If i choose the pagesize as a5 landscape (a5.rotate()), code produces a PDF as its seen on here, which is better.
a5 landscape
but when this is printed on paper, it starts to print the page vertically, treating the printer has a a4 paper tray.
Seems to me, I need to define my printer as a dotmatrix printer with continuous paper.
The code i am using is :
Document document = new Document(PageSize.A5,0,0,0,0);
try {
PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
document.open();
StringBuffer javascript = new StringBuffer();
javascript.append("this.print({bUI: false, bSilent: true, bShrinkToFit: true});");
PdfAction pdfAction= PdfAction.javaScript(javascript.toString(), writer);
writer.addJavaScript(pdfAction);
writer.addViewerPreference(PdfName.PRINTSCALING, PdfName.NONE);
PdfPTable table = new PdfPTable(3); // 3 columns.
table.setWidthPercentage(100);
PdfPCell cell1 = new PdfPCell(new Paragraph(""));
PdfPCell cell2 = new PdfPCell(new Paragraph(""));
PdfPCell cell3 = new PdfPCell(new Paragraph(MakbuzNo,FontFactory.getFont(FontFactory.COURIER,9)));
cell3.setLeading(16f, 0f);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1 = new PdfPCell(new Paragraph(""));
cell2 = new PdfPCell(new Paragraph(""));
cell3 = new PdfPCell(new Paragraph(Duzenleyen,FontFactory.getFont(FontFactory.COURIER,9)));
cell3.setLeading(16f, 0f);
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1 = new PdfPCell(new Paragraph(MSISDN,FontFactory.getFont(FontFactory.COURIER,9)));
cell2 = new PdfPCell(new Paragraph(""));
cell3 = new PdfPCell(new Paragraph(DuzenlemeSaati,FontFactory.getFont(FontFactory.COURIER,9)));
cell1.setLeading(16f, 0f);
cell3.setLeading(16f, 0f);
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1 = new PdfPCell(new Paragraph(""));
cell2 = new PdfPCell(new Paragraph(""));
cell3 = new PdfPCell(new Paragraph(DuzenlemeTarihi,FontFactory.getFont(FontFactory.COURIER,9)));
cell3.setLeading(16f, 0f);
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1 = new PdfPCell(new Paragraph(" "));
cell2 = new PdfPCell(new Paragraph(" "));
cell3 = new PdfPCell(new Paragraph(" "));
cell1.setLeading(45f, 0f);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1 = new PdfPCell(new Paragraph(izahat,FontFactory.getFont(FontFactory.COURIER,9)));
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell1.setColspan(3);
cell1.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
cell1 = new PdfPCell(new Paragraph(" "));
cell2 = new PdfPCell(new Paragraph(" "));
cell3 = new PdfPCell(new Paragraph(" "));
cell1.setLeading(75f, 0f);
cell1.setBorder(Rectangle.NO_BORDER);
cell2.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell2);
table.addCell(cell3);
cell1 = new PdfPCell(new Paragraph(kopyayazi,FontFactory.getFont(FontFactory.COURIER,9)));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setColspan(3);
cell1.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
cell1 = new PdfPCell(new Paragraph(TutarYazi,FontFactory.getFont(FontFactory.COURIER,9)));
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell1.setColspan(2);
cell3 = new PdfPCell(new Paragraph(ToplamTutar,FontFactory.getFont(FontFactory.COURIER,9)));
cell3.setHorizontalAlignment(Element.ALIGN_RIGHT);
cell1.setLeading(16f, 0f);
cell3.setLeading(16f, 0f);
cell1.setBorder(Rectangle.NO_BORDER);
cell3.setBorder(Rectangle.NO_BORDER);
table.addCell(cell1);
table.addCell(cell3);
document.add(table);
document.newPage();
} catch (DocumentException de) {
de.printStackTrace();
System.err.println("document: " + de.getMessage());
}
document.close();
So how to use dotmatrix printer with continous paper and stop paper scroller when character on page is already printed ?
Welcome to hell :-) Here are all the daemons that have their tiny claws in your process:
The PDF file itself has an orientation
The PDF viewer/printer might try to rotate the pages to fit the page
The printer driver might do it, too
If you had a laser printer, that might rotate the page as well.
The most "simple" solution is to render the PDF in a BufferedImage, save that as a pixel image (say, PNG) and print that. This will allow you to make sure the orientation is what you want.
Also check the settings of the printer driver: Some of them send a "Form Feed" character after printing the file which would result in an empty page if you fill the whole a5 page.
I have experienced the similar problem with the Jasper reports printing(with continuous paper), After fiddling with the code for days I found a solution I think will work for you.
I think you have to do is to generate a PDF file as output and simply get the AdobeReader exe to do the printing, you can use
AcroRd32.exe /N /T PdfFile PrinterName
parameters to achieve this.
After long researches, I could not find any way to print on clients' dot-matrix printer without any extra installation.
Most efficient way is to have a signed applet. Java installation required at clients' side.