How to "float" an image in a cell - iTextPDF - java

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);

Related

Font Size not reflecting in IText

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);

Changing Font on PDF Rotated Text

I'm using iText to create barcodes on a PDF with the same format as this one:
The problem is the the left number, the first zero digits must be smaller, while the rest of the numbers must also be bold. "T.T.C." also has to be even smaller (it doesn't have to be on another line).
I was able to rotate the number with the following code:
String price = "23000 T.T.C.";
PdfContentByte cb = docWriter.getDirectContent();
PdfTemplate textTemplate = cb.createTemplate(50, 50);
ColumnText columnText = new ColumnText(textTemplate);
columnText.setSimpleColumn(0, 0, 50, 50);
columnText.addElement(new Paragraph(price));
columnText.go();
Image image;
image = Image.getInstance(textTemplate);
image.setAlignment(Image.MIDDLE);
image.setRotationDegrees(90);
doc.add(image);
The problem is that I cannot find a way online to change the font of certain characters of the String price when it is printed on the PDF.
I have created a small Proof of Concept that results in a PDF that looks like this:
As you can see, it has text in different sizes and styles. It also has a bar code that is rotated.
Take a look at the RotatedText example:
public void createPdf(String dest) throws IOException, DocumentException {
// step 1
Document document = new Document(new Rectangle(60, 120), 5, 5, 5, 5);
// step 2
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
// step 3
document.open();
// step 4
PdfContentByte canvas = writer.getDirectContent();
Font big_bold = new Font(FontFamily.HELVETICA, 12, Font.BOLD);
Font small_bold = new Font(FontFamily.HELVETICA, 6, Font.BOLD);
Font regular = new Font(FontFamily.HELVETICA, 6);
Paragraph p1 = new Paragraph();
p1.add(new Chunk("23", big_bold));
p1.add(new Chunk("000", small_bold));
document.add(p1);
Paragraph p2 = new Paragraph("T.T.C.", regular);
p2.setAlignment(Element.ALIGN_RIGHT);
document.add(p2);
BarcodeEAN barcode = new BarcodeEAN();
barcode.setCodeType(Barcode.EAN8);
barcode.setCode("12345678");
Rectangle rect = barcode.getBarcodeSize();
PdfTemplate template = canvas.createTemplate(rect.getWidth(), rect.getHeight() + 10);
ColumnText.showTextAligned(template, Element.ALIGN_LEFT,
new Phrase("DARK GRAY", regular), 0, rect.getHeight() + 2, 0);
barcode.placeBarcode(template, BaseColor.BLACK, BaseColor.BLACK);
Image image = Image.getInstance(template);
image.setRotationDegrees(90);
document.add(image);
Paragraph p3 = new Paragraph("SMALL", regular);
p3.setAlignment(Element.ALIGN_CENTER);
document.add(p3);
// step 5
document.close();
}
This example solves all of your issues:
You want a Paragraph to use different fonts: compose a Paragraph using different Chunk objects.
You want to add extra text on top of a bar code: add the bar code to a PdfTemplate and add the extra text using ColumnText.showTextAligned() (not that you can also compose a Phrase using different Chunk objects if you need more than one font in that extra text).
You want to rotate the bar code: wrap the PdfTemplate inside an Image object and rotate the image.
You can check the result: rotated_text.pdf
I hope this helps.

iTextPDF - Unable to use getOverContent() as a paremeter for table.writeSelectedRows() when internal anchors have been added to the table

I've been trying to create a table of contents which dynamically links to other pages in the PDF using anchors. I've ran into an issue with using stamper.getOverContent() as the canvas parameter in the method table.writeSelectedRows().
table.writeSelectedRows(0, -1, 36, 700, stamper.getOverContent(4));
The example below will write a table to page 4 containing an anchor destination, and a table to page 8 containing an anchor hyperlink which links back to page 4. (Note that I'm using tables for this because it is just a part of what ultimately needs to be achieved).
String strFileName = "C:\\link\\to\\existing\\PDF\\document.pdf";
PdfReader reader = new PdfReader(strFileName);
FileOutputStream out = new FileOutputStream("results/tables/link_in_positioned_table.pdf");
PdfStamper stamper = new PdfStamper(reader, out);
stamper.setRotateContents(true);
//Page 4 table containing destination
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell();
Paragraph p = new Paragraph();
table.setTotalWidth(500);
Anchor target = new Anchor("page 4");
target.setName("page4");
p.add(target);
cell.addElement(p);
table.addCell(cell);
table.writeSelectedRows(0, -1, 36, 700, stamper.getOverContent(4));
//Page 8 table containing link
PdfPTable table1 = new PdfPTable(1);
PdfPCell cell1 = new PdfPCell();
Paragraph p1 = new Paragraph();
table1.setTotalWidth(500);
Anchor anchor = new Anchor("page4 link");
anchor.setReference("#page4");
p1.add(anchor);
cell1.addElement(p1);
table1.addCell(cell1);
table1.writeSelectedRows(0, -1, 36, 700, stamper.getOverContent(8));
stamper.close();
reader.close();
out.close();
This works fine if anchor.setReference(); is an external hyperlink, but if it is internal it returns with the following error:
at
com.itextpdf.text.pdf.internal.PdfAnnotationsImp.addPlainAnnotation(PdfAnnotationsImp.java:126)
at com.itextpdf.text.pdf.PdfDocument.localGoto(PdfDocument.java:2178)
at
com.itextpdf.text.pdf.PdfDocument.writeLineToContent(PdfDocument.java:1643)
at com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:1162) at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:994) at
com.itextpdf.text.pdf.ColumnText.goComposite(ColumnText.java:1541) at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:1000) at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:994) at
com.itextpdf.text.pdf.ColumnText.go(ColumnText.java:982) at
com.itextpdf.text.pdf.PdfPRow.writeCells(PdfPRow.java:583) at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:833)
at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:966)
at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:912)
at
com.itextpdf.text.pdf.PdfPTable.writeSelectedRows(PdfPTable.java:891)
at
com.ems.rendition.cts.plugin.StamperPDFPlugin.createPdf(StamperPDFPlugin.java:1727)
at
com.ems.rendition.cts.plugin.StamperPDFPlugin.main(StamperPDFPlugin.java:1684)
Am I doing something wrong? Or is this an issue?
Note: I asked a similar question here - iTextPDF - Cannot use writeSelectedRows() on a table where an anchor has been inserted which didn't identify that getOverContent() was the issue and instead focussed on the actual implementation of the anchors.

Barcode128 appears outside of the pdf page in IE

I am creating an image with Barcode128 and am trying to place it in PDf file, but it is going over the page in IE:
if the order number (ETIMS...) is around 20 characters it print normal size barcode.
I tried different values for:
code128.setBarHeight();
code128.setX();
Image.setWidthPercentage();
Image.scaleAbsolute();
Image.scaleToFit();
Image.scalePercent();
But nothing works, it's like something else sets width and height and nothing I do changes it.
parts of the code where this is happening:
float[] widths1 = { 10f, 4f };
PdfPTable tableReqNumberBarcode;
Paragraph p1 = new Paragraph();
PdfPCell cellReqNumberBarcode;
while (iteratorPDFReader.hasNext())
{
tableReqNumberBarcode = new PdfPTable(widths1);
cellReqNumberBarcode = new PdfPCell(p1);
cellReqNumberBarcode.setBorder(Rectangle.NO_BORDER);
}
page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
cb.addTemplate(page, 1.15f, 0, 0, 1.15f, 70, 0);
Barcode128 code128 = new Barcode128();
code128.setCode(orderNumber);
code128.setFont(null); //remove printed text under the barcode
code128.setBarHeight(80f);
code128.setX(1f);
Image myImage = code128.createImageWithBarcode(cb, null, null);
cellReqNumberBarcode.addElement(myImage);
tableReqNumberBarcode.addCell(cellReqNumberBarcode);
document.add(tableReqNumberBarcode);
The code128.setX(1f); sets the width of the narrowest bar. Set it to a lower value to shrink the barcode.
I solved it by adding setX and setBarHeight()
Barcode128 code128 = new Barcode128();
code128.setCode(GetReqNumByJobNumber(childJobNumbers[labelNumber], eb, ssb));
code128.setFont(null); //null removes the printed text under the barcode
code128.setX(0.6f);
code128.setBarHeight(22);
Image myImage = code128.createImageWithBarcode(cb, null, null);
myImage.setAbsolutePosition(10,700);
myImage.scalePercent(125);

Text on image in PDF footer using iText

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.

Categories

Resources