How to add two images in one cell in iText? - java

I'm currently creating a system that generates a PDF. However, I cannot put two - three images in a single cell. I tried for-looping it but it has borders. What should i do?

Please take a look at the ImagesInCell example. It uses three images:
public static final String IMG1 = "resources/images/brasil.png";
public static final String IMG2 = "resources/images/dog.bmp";
public static final String IMG3 = "resources/images/fox.bmp";
These are the image instances:
Image img1 = Image.getInstance(IMG1);
Image img2 = Image.getInstance(IMG2);
Image img3 = Image.getInstance(IMG3);
The easiest way to add multiple images to a single cell is by using addElement multiple times:
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(50);
table.addCell("Different images, one after the other vertically:");
PdfPCell cell = new PdfPCell();
cell.addElement(img1);
cell.addElement(img2);
cell.addElement(img3);
table.addCell(cell);
document.add(table);
The result looks like this:
As you can see, the images were scaled automatically, to fit the width of the cell. If that isn't what you want, you have to improve your question, because you only claim that you can't add three images to the same cell, whereas this simple example proves the exact opposite.
Maybe you want something that looks like this:
In the first row with images, we use the same addElement() method as before, but we change the width percentage of the image to 20%:
cell = new PdfPCell();
img1.setWidthPercentage(20);
cell.addElement(img1);
img2.setWidthPercentage(20);
cell.addElement(img2);
img3.setWidthPercentage(20);
cell.addElement(img3);
table.addCell(cell);
In the second row with images, we use a different approach: we have wrapped the images inside Chunk objects, so that we can put them next to each other:
Paragraph p = new Paragraph();
img1.scalePercent(30);
p.add(new Chunk(img1, 0, 0, true));
p.add(new Chunk(img2, 0, 0, true));
p.add(new Chunk(img3, 0, 0, true));
cell = new PdfPCell();
cell.addElement(p);
table.addCell(cell);
Observe that I scaled the first image. The three images wouldn't fit next to each other if that image kept its original size.
Wrapping an image inside a Chunk has the advantage that we can mix images and text:
p = new Paragraph("The quick brown ");
p.add(new Chunk(img3, 0, 0, true));
p.add(" jumps over the lazy ");
p.add(new Chunk(img2, 0, 0, true));
cell = new PdfPCell();
cell.addElement(p);
table.addCell(cell);

Related

Image size are getting shrinked while printing in pdf using iText

Image image = Image.getInstance(filePath);
image.setSpacingAfter(10f);
image.scaleAbsolute(100f, 100f);
PdfPTable imageTabel = new PdfPTable(1);
imageTabel.setHorizontalAlignment(0);
image.setScaleToFitHeight(true);
PdfPCell cellOne = new PdfPCell(image, true);
cellOne.setBorder(Rectangle.NO_BORDER);
cellOne.setBackgroundColor(new BaseColor(255, 255, 255));
imageTabel.addCell(cellOne);`
columnText.addElement(imageTabel);
In PDF few images are not printing properly.
Output:

Add border to ImageView (JavaFX)

I'm trying to add a border to my Image/ImageView, but I don't know how to do it.
When I press the button three randomly generated images are added to the GridPane. I've tried adding -fx-border-color and -fx-border-width properties through an external CSS file and then applying it but that way hasn't worked.
Any help is appreciated!
btnLever.setOnAction((ActionEvent e) ->
{
// New array for loading in the images from 'generateImages' method
String[] fruits = new String[3];
fruits = generateImages();
// GAME WILL RUN IF THE CREDITS IS GREATER THAN 0
if(credits >0)
{
// IMAGES GENERATED
Image img1 = new Image(fruits[0]);
Image img2 = new Image(fruits[1]);
Image img3 = new Image(fruits[2]);
// ADDS IMAGES TO THE GRID
grid.add(new ImageView(img1), 0, 0);
grid.add(new ImageView(img2), 1, 0);
grid.add(new ImageView(img3), 2, 0);

iText 5.5.3 ColumnText doesn't wrap text correctly due to the way font size is measured

Question Setup
I am trying to accurately determine the width of a font (Ubuntu Italic), though iText appears to ignore the portion of the last glyph after the horizontal advance as shown in the image below.
The code I used to generate this example is as follows:
Document document = new Document(PageSize.LETTER);
FileOutputStream out = new FileOutputStream(new File("test.pdf"));
PdfWriter writer = PdfWriter.getInstance(document, out);
document.open();
String text = "ff";
Chunk chunk = new Chunk(text, FontFactory.getFont("Ubuntu-Italic.ttf", 72)
Phrase phrase = new Phrase(chunk);
float width = ColumnText.getWidth(phrase);
System.out.println(width + ", " + chunk.getWidthPoint());
PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100);
table.setTotalWidth(width);
PdfPCell cell = table.getDefaultCell();
cell.setPadding(0);
cell.setUseDescender(true);
cell.setUseAscender(true);
table.addCell(phrase);
float height = table.calculateHeights();
PdfContentByte canvas = writer.getDirectContent();
ColumnText columnText = new ColumnText(canvas);
columnText.setSimpleColumn(36, 756 - height, 36 + width, 756);
columnText.addElement(table);
columnText.go();
document.close();
out.close();
As demonstrated in the code I tried both ColumnText.getWidth(phrase) as well as chunk.getWidthPoint() which both return the same value, with a little bit of floating point difference.
Question
The code I wrote above simulates a situation in iText where text doesn't wrap correctly to the next line. The problem I'm having is that ColumnText, in the code I am using, is cropped. The issue is that because of the way iText measures text, the ColumnText thinks there is enough room for the f at the right edge when in fact there really isn't, so in my situation it is getting cut off. Is there a way to force ColumnText to measure the width of the font differently so that this doesn't happen?
Your observation
The right border that cuts through the f
corresponds with the definition of the Ubuntu-Italic letter f:
The width you get is the width of the letter on the base line, the horizontal advance, it is not the distance from the left-most to the right-most x coordinate.
I ended up solving this by adding padding on the left and right side of the PdfPCell and by increasing the width of the ColumnText equal to the padding I added. That way the text flows the same way and allows for the fact that iText only takes into account the horizontal advance values of the font.

Adding more text to a cell in table in itext

I am trying to add some text with a bar code in a table cell using itext as per the following code but it does not show in the pdf file. i tried adding chunks and paragraph. Any help on this would be appreciated.
Barcode128 barcode = new Barcode128();
//barcode.setCodeType(Barcode.EAN8);
barcode.setCode(code);
PdfPCell cell = new PdfPCell(barcode.createImageWithBarcode(writer.getDirectContent(), BaseColor.BLACK, BaseColor.GRAY), true);
Paragraph paragraph = new Paragraph("Hello World");
cell.addElement(paragraph);
cell.setPadding(10);
You are probably confused by text vs. composite mode.
When using the PdfPCell(Image) constructor, you create a cell in text mode. Any subsequent call to addElement(Element) will then switch the cell to composite mode, removing all content previously entered in the constructor.
You'll have to change your code that way:
PdfPCell cell = new PdfPCell();
Barcode128 barcode = new Barcode128();
barcode.setCode(code);
Image barcodeImage = barcode.createImageWithBarcode(writer.getDirectContent(), BaseColor.BLACK, BaseColor.GRAY);
cell.addElement(barcodeImage);
Paragraph paragraph = new Paragraph("Hello World");
cell.addElement(paragraph);

Text Positioning with IText for Java

I need to position different text before I generate the pdf using IText. I've been thinking of using Chunks, but I don't know how to position them separately. I also tried using PdfContentByte but it doesn't generate any PDF File.
Why don't you use tables combined with Chunks for your layout. ex:
PdfPTable headerTable = new PdfPTable(2);
float[] headerTableWidths = { 80f, 20f };
headerTable.setWidthPercentage(100f);
headerTable.setWidths(headerTableWidths);
headerTable.getDefaultCell().setBorderWidth(0);
headerTable.getDefaultCell().setPadding(2);
headerTable.getDefaultCell().setBorderColor(BaseColor.BLACK);
headerTable.getDefaultCell().setFixedHeight(90f);
PdfPCell infoCell = new PdfPCell();
infoCell.setHorizontalAlignment(Element.ALIGN_CENTER);
infoCell.setVerticalAlignment(Element.ALIGN_TOP);
infoCell.addElement("test");
infoCell.addElement("text");
table.addCell(infoCell);

Categories