I am trying to add an image and page no in my PDF footer. My problem is i'm not able to show the page number on the image. Below is my code.
public void onEndPage(PdfWriter writer, Document document) {
int pageNo=writer.getPageNumber()-this.pageNumber+1;
Integer dummy=pageNo;
String pageNoString=dummy.toString();
PdfContentByte cbb = writer.getDirectContent();
try {
ColumnText column = new ColumnText(cbb);
PdfPTable newtable = new PdfPTable(1);
newtable.setTotalWidth(530);
newtable.setLockedWidth(true);
Image img = Image.getInstance("C:/Users/sathesh/Desktop/Warfiles/PDFFiles/Footer.png");
PdfPCell imageCell=new PdfPCell();
PdfPCell textCell=new PdfPCell(new Paragraph("Disclaimer text",new Font(Font.FontFamily.COURIER, 6, Font.NORMAL)));
imageCell.setBorder(Rectangle.NO_BORDER);
textCell.setBorder(Rectangle.NO_BORDER);
imageCell.setImage(img);
ColumnText.showTextAligned(cbb,
Element.ALIGN_LEFT, new Phrase(pageNoString,FontFactory.getFont(FontFactory.COURIER,12,new BaseColor(0xFF, 0x00, 0x00))), 50, 95, 0);
newtable.addCell(imageCell);
newtable.addCell(textCell);
column.addElement(newtable);
column.setSimpleColumn(30, 130, 570, 45,5f, Element.ALIGN_RIGHT | Element.ALIGN_BOTTOM |Element.ALIGN_JUSTIFIED_ALL);
}
column.go();
catch(Exception e)
{
e.printStackTrace();
}
}
You have:
ColumnText.showTextAligned(cbb, Element.ALIGN_LEFT, new Phrase(pageNoString,FontFactory.getFont(FontFactory.COURIER,12,new BaseColor(0xFF, 0x00, 0x00))), 50, 95, 0);
newtable.addCell(imageCell);
newtable.addCell(textCell);
column.addElement(newtable);
column.setSimpleColumn(30, 130, 570, 45,5f, Element.ALIGN_RIGHT | Element.ALIGN_BOTTOM |Element.ALIGN_JUSTIFIED_ALL);
column.go();
This add the text first, and then covers the text with the table containing the image, hence the image covers the text.
As I said in my comment, you should use elementary logic and try this:
newtable.addCell(imageCell);
newtable.addCell(textCell);
column.addElement(newtable);
column.setSimpleColumn(30, 130, 570, 45,5f, Element.ALIGN_RIGHT | Element.ALIGN_BOTTOM |Element.ALIGN_JUSTIFIED_ALL);
column.go();
ColumnText.showTextAligned(cbb, Element.ALIGN_LEFT, new Phrase(pageNoString,FontFactory.getFont(FontFactory.COURIER,12,new BaseColor(0xFF, 0x00, 0x00))), 50, 95, 0);
Now the table with the image is added first and the text is written on top of the image.
Related
I am trying to set the font size inside my table cell but it isn't working. I am using IText 2.0.8 because it is a legacy application and cannot upgrade to the later versions.
I tried using a phrase inside the paragraph and setting the font there as well but it isn't getting reflected in the generated PDF.
PdfPCell cell = new PdfPCell();
PdfPTable innerTable = new PdfPTable(TWO);
BaseFont bf1 = BaseFont.createFont(BaseFont.COURIER_BOLDOBLIQUE,
BaseFont.CP1252, BaseFont.EMBEDDED);
BaseFont bf2 = BaseFont.createFont(BaseFont.TIMES_BOLDITALIC,
BaseFont.CP1252, BaseFont.EMBEDDED);
Paragraph paragraph = new Paragraph(key + ": ", new Font(bf1, 30));
PdfPCell innerCell = new PdfPCell(paragraph);
innerCell.setBorder(Rectangle.NO_BORDER);
innerTable.addCell(innerCell);
paragraph = new Paragraph(value, new Font(bf2, 30));
innerCell = new PdfPCell(paragraph);
innerCell.setBorder(Rectangle.NO_BORDER);
innerTable.addCell(innerCell);
cell.setVerticalAlignment(Element.ALIGN_CENTER);
cell.addElement(innerTable);
cell.setFixedHeight(FIXED_HEIGHT);
cell.setBorder(Rectangle.NO_BORDER);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
Can you have a look,
Font font = new Font(BaseFont.createFont(BaseFont.COURIER_BOLDOBLIQUE,
BaseFont.CP1252, BaseFont.EMBEDDED), 30, Font.BOLD, new Color(0, 0, 0));
Paragraph paragraph = new Paragraph(key + ": ", font);
i want to generate a pdf label for CD using java itext. i have drawn the circle but i am unable to set image and multiple paragraphs inside the circle.
Below is the code snippet.code snippet
String printingPath = "CD_label.pdf";
Document document = new Document(new Rectangle(PageSize.A4));
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(printingPath));
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.setRGBColorFill(0xFF, 0xFF, 0xFF);
BaseColor colorval = new BaseColor(102,178,255);
cb.setColorStroke(colorval);
cb.circle(300.0f, 650.0f, 150.0f);
cb.circle(300.0f, 650.0f, 20.0f);
cb.stroke();
//cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA,BaseFont.CP1257,BaseFont.EMBEDDED), 10);
//cb.beginText();
//cb.resetRGBColorStroke();
//cb.setTextMatrix(320, 420);
//cb.showText("Text inside cd");
// ColumnText.showTextAligned(cb, Element.ALIGN_LEFT,new Phrase("Hello itext"),50, 700, 0); cb.endText();
Image img = Image.getInstance("Symbol.png");
img.setAbsolutePosition(270f, 740f);
img.scaleAbsolute(60, 34);
document.close();
Why don't you see your text?
You set the fill color to WHITE:
cb.setRGBColorFill(0xFF, 0xFF, 0xFF);
Text (usually) is drawn by filling glyph outlines defined in some font. Thus, your uncommented text drawing code
cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA,BaseFont.CP1257,BaseFont.EMBEDDED), 10);
cb.beginText();
cb.resetRGBColorStroke();
cb.setTextMatrix(320, 420);
cb.showText("Text inside cd");
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT,new Phrase("Hello itext"),50, 700, 0);
cb.endText();
does draw text... in WHITE on WHITE...
If you remove that cb.setRGBColorFill instruction (or select a clearly different fill color), you'll see your text:
(The point (320, 420) clearly is outside a circle with center (300, 650) and radius 150, consequentially so is your "Text inside cd" text...)
Another issue: ColumnText.showTextAligned starts its own text object, so to create a valid PDF you must move it after your cb.endText():
cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA,BaseFont.CP1257,BaseFont.EMBEDDED), 10);
cb.beginText();
cb.resetRGBColorStroke();
cb.setTextMatrix(320, 420);
cb.showText("Text inside cd");
cb.endText();
ColumnText.showTextAligned(cb, Element.ALIGN_LEFT,new Phrase("Hello itext"),50, 700, 0);
Why don't you see your image?
Because you don't add it!
If you add it to your PdfContentByte cb
Image img = Image.getInstance("Symbol.png");
img.setAbsolutePosition(270f, 740f);
img.scaleAbsolute(60, 34);
cb.addImage(img);
the result becomes like this:
(I obviously don't have your image, so I use a simple example image instead.)
using iText, I have to create a PDF with a big PdfPTable and, on the footer, the total pages number (something like 'page X of Y'). I took a look at this exemple but I really don't understant how it works. At the moment my code is something like this:
PdfPTable table = new PdfPTable(10);
//something to fill the table
baos = new ByteArrayOutputStream();
document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, baos);
document.open();
PdfTemplate totalPage = writer.getDirectContent().createTemplate(30, 16);
ColumnText columnTable = new ColumnText(writer.getDirectContent());
columnTable.addElement(table);
while(true) {
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, new Phrase("Pag. " + String.format("%d", writer.getPageNumber()),FONT_N), (document.right() - document.left())/ 2 + document.leftMargin(), document.bottom(), 0);
//I think something to insert totalPage in the document...
columnTable.setSimpleColumn(document.left(), document.top(), document.right(), Math.round(document.bottom()*1.5));
if(!ColumnText.hasMoreText(columnTable.go()))
break;
document.newPage();
}
//I think something to set totalPage...
document.close();
Can someone help me?
Replace while loop with below code:
ColumnText columnTable = new ColumnText(writer.getDirectContent());
columnTable.addElement(table);
PdfTemplate totalPage = writer.getDirectContent().createTemplate(30, 16);
try {
table.setWidths(new int[]{24, 24, 2 });
table.setTotalWidth(527);
table.setLockedWidth(true);
table.getDefaultCell().setFixedHeight(20);
table.getDefaultCell().setBorder(Rectangle.BOTTOM);
table.addCell("");
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(String.format("Page %d of", writer.getPageNumber()));
PdfPCell cell = new PdfPCell(Image.getInstance(totalPage));
cell.setBorder(Rectangle.BOTTOM);
table.addCell(cell);
table.writeSelectedRows(0, -1, 34, 803, writer.getDirectContent());
ColumnText.showTextAligned(totalPage, Element.ALIGN_LEFT,
new Phrase( String.valueOf(writer.getPageNumber())),
0, 0, 0);
}
catch(DocumentException de) {
throw new ExceptionConverter(de);
}
I'm using iText and create a dynamic table which has a a reoccurring header in the method createTabularHeader:
PdfPTable table = new PdfPTable(6);
// fill it with some basic information
table.setHeaderRows(1);
Yet on the first page I would like to display different information. (but the table structure/size remains the same)
Due to the dynamic content which is obtained in a different method I can't say when a new page starts.
I tried with the most primitive variant - just adding a white rectangle over the text and insert the different text. As it's just on the first page all I have to do is creating that rectangle between both methods.
But the white rectangle doesn't have any opacity and can' cover anything.
Yet by trying around I found the method writer.getDirectContent().setColorStroke(BaseColor.WHITE); which set the text to white. Later I just set the BaseColor of my cells manually to black. But the even though the new text is applied after the calling of my createTabularHeader-method its layer is under the layer of the original text and the letters are covering the new text partly.
Using the answer to How to insert invisible text into a PDF? brought me to the idea of using myPdfContentByte.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE); was not so helpful as it resets only on the 2nd page regardless what I do and the regular text on the first page stays invisible.
I'm unable to find a proper solution... How can the table-header be modified only on the first page?
The solution is not really nice, but works... and as some sort of bonus I want to add how you can modify the indentions on the first page.
public void createPdf() {
document = new Document();
try {
PdfWriter writer = PDFHead.getWriter(document);
//If it's a letter we have a different indention on the top
if (letterPDF) {
document.setMargins(36, 36, 100, 36);
} else {
document.setMargins(36, 36, 36, 36);
}
document.open();
document.add(createTabularContent());
document.close();
} catch (DocumentException | FileNotFoundException ex) {
try {
document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILENAME));
document.open();
document.add(new Phrase(ex.getLocalizedMessage()));
document.close();
Logger.getLogger(Etikette.class.getName()).log(Level.SEVERE, null, ex);
} catch (FileNotFoundException | DocumentException ex1) {
Logger.getLogger(Etikette.class.getName()).log(Level.SEVERE, null, ex1);
}
}
}
The PDFHead is used to create a regular header (the one which appears on every page, not only on pages with the table):
public static PdfWriter getWriter(Document document) throws FileNotFoundException, DocumentException {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
HeaderFooter event = new HeaderFooter("Ing. Mario J. Schwaiger", type + " " + DDMMYYYY.format(new java.util.Date()), 835, isLetterPDF(), customerNumber);
writer.setBoxSize("art", new Rectangle(36, 54, 559, 788));
writer.setPageEvent(event);
return writer;
}
And in that HeaderFooter-Event I use the fact the function is called after the PDF is basically created (for the page number for instance):
#Override
public void onEndPage(PdfWriter writer, Document document) {
if (isLetter) {
//That's only for the first page, apparently 1 is too late
//I'm open for improvements but that works fine for me
if (writer.getPageNumber() == 0) {
//If it's a letter we use the different margins
document.setMargins(36, 36, 100, 36);
}
if (writer.getPageNumber() == 1) {
PdfContentByte canvas = writer.getDirectContent();
float llx = 460;
float lly = 742;
float urx = 36;
float ury = 607;
//As I add the rectangle in the event here it's
//drawn over the table-header. Seems the tableheader
//is rendered afterwards
Rectangle rect1 = new Rectangle(llx, lly, urx, ury);
rect1.setBackgroundColor(BaseColor.WHITE);
rect1.setBorder(Rectangle.NO_BORDER);
rect1.setBorderWidth(1);
canvas.rectangle(rect1);
ColumnText ct = new ColumnText(canvas);
ct.setSimpleColumn(rect1);
PdfPTable minitable = new PdfPTable(1);
PdfPCell cell = PDFKopf.getKundenCol(PDFHeader.getCustomer(customerNumber));
cell.setBorder(Rectangle.NO_BORDER);
minitable.addCell(cell);
//A single cell is not accepted as an "Element"
//But a table including only a single cell is
ct.addElement(minitable);
try {
ct.go();
} catch (DocumentException ex) {
Logger.getLogger(HeaderFooter.class.getName()).log(Level.SEVERE, null, ex);
}
//In any other case we reset the margins back to normal
//This could be solved in a more intelligent way, feel free
} else {
document.setMargins(36, 36, 36, 36);
}
}
//The regular header of any page...
PdfPTable table = new PdfPTable(4);
try {
table.setWidths(new int[]{16, 16, 16, 2});
table.setWidthPercentage(100);
table.setTotalWidth(527);
table.setLockedWidth(true);
table.getDefaultCell().setFixedHeight(20);
table.getDefaultCell().setBorder(Rectangle.BOTTOM);
table.addCell(header);
PdfPCell cell;
cell = new PdfPCell(new Phrase(mittelteil));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBorder(Rectangle.BOTTOM);
table.addCell(cell);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(String.format("Page %d of ", writer.getPageNumber()));
cell = new PdfPCell(Image.getInstance(total));
cell.setBorder(Rectangle.BOTTOM);
table.addCell(cell);
table.writeSelectedRows(0, -1, 34, y, writer.getDirectContent());
} catch (DocumentException de) {
throw new ExceptionConverter(de);
}
}
I am trying to create a formatted PdfPCell where my text is on the left and an image(QRCode) is "floated" (in the css sense) to the right. My current code moves the image to the right but the text is on the next line not the same line as the image.
Ideas?
PdfPCell cell = new PdfPCell();
Paragraph p = new Paragraph();
p.add(new Paragraph("Ciao Baby",RESTNAME));
BarcodeQRCode qrcode = new BarcodeQRCode("http://www.tvfoodmaps.com", 72, 72, null);
Image img = qrcode.getImage();
img.scaleToFit(32,32);
img.setAlignment(Element.ALIGN_RIGHT);
cell.addElement(img);
cell.addElement(p);
You can try replacing
img.setAlignment(Element.ALIGN_RIGHT);
with
img.Alignment = Image.TEXTWRAP | Image.ALIGN_RIGHT;
Try this.
Phrase phrase = new Phrase("Ciao Baby",RESTNAME);
BarcodeQRCode qrcode = new BarcodeQRCode("http://www.tvfoodmaps.com", 72, 72, null);
Image img = qrcode.getImage();
img.scaleToFit(32,32);
phrase.add(new Phrase(new Chunk(img, 0, 0)));
cell.addElement(phrase);