First of all: when adding text and images to the page, iText sometimes changes the order of text content and image. You can avoid this by using:
writer.setStrictImageSequence(true);
If you want to know the current position of the cursor, you can use the getVerticalPosition() method. Unfortunately, this method is not very elegant: it requires a Boolean parameter that will add a new line (if true ) or give you a position in the current line (if false ).
I donβt understand why you want to get upright. This is because you want to have a title followed by an image, and want the caption and image to be on the same page?
In this case, you can put your text and images inside the table cell and instruct iText not to split the lines. In this case, iText will forward both the text and the image in the correct order to the next page if the content does not match the current page.
Update:
Based on the additional information added in the comments, it is now clear that the OP wants to add watermarked images.
There are two approaches to achieve this, depending on the actual requirement.
Approach 1:
The first approach is explained in the WatermarkedImages1 example. In this example, we create a PdfTemplate to which we add an image, as well as some text written on top of this image. Then we can wrap this PdfTemplate inside the image and add this image along with its watermark using a single document.add() statement.
This is the method that does all the magic:
public Image getWatermarkedImage(PdfContentByte cb, Image img, String watermark) throws DocumentException { float width = img.getScaledWidth(); float height = img.getScaledHeight(); PdfTemplate template = cb.createTemplate(width, height); template.addImage(img, width, 0, 0, height, 0, 0); ColumnText.showTextAligned(template, Element.ALIGN_CENTER, new Phrase(watermark, FONT), width / 2, height / 2, 30); return Image.getInstance(template); }
This is how we add images:
PdfContentByte cb = writer.getDirectContentUnder(); document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE1), "Bruno")); document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE2), "Dog")); document.add(getWatermarkedImage(cb, Image.getInstance(IMAGE3), "Fox")); Image img = Image.getInstance(IMAGE4); img.scaleToFit(400, 700); document.add(getWatermarkedImage(cb, img, "Bruno and Ingeborg"));
As you can see, we have a very large image (photo of my wife and me). We need to scale this image to fit the page. If you want to avoid this, take a look at the second approach.
Approach 2:
The second approach is explained in the WatermarkedImages2 example. In this case, we add each image to PdfPCell . This PdfPCell scales the image to fit the width of the page. To add a watermark, we use the cell event:
class WatermarkedCell implements PdfPCellEvent { String watermark; public WatermarkedCell(String watermark) { this.watermark = watermark; } public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS]; ColumnText.showTextAligned(canvas, Element.ALIGN_CENTER, new Phrase(watermark, FONT), (position.getLeft() + position.getRight()) / 2, (position.getBottom() + position.getTop()) / 2, 30); } }
This cell event can be used as follows:
PdfPCell cell; cell = new PdfPCell(Image.getInstance(IMAGE1), true); cell.setCellEvent(new WatermarkedCell("Bruno")); table.addCell(cell); cell = new PdfPCell(Image.getInstance(IMAGE2), true); cell.setCellEvent(new WatermarkedCell("Dog")); table.addCell(cell); cell = new PdfPCell(Image.getInstance(IMAGE3), true); cell.setCellEvent(new WatermarkedCell("Fox")); table.addCell(cell); cell = new PdfPCell(Image.getInstance(IMAGE4), true); cell.setCellEvent(new WatermarkedCell("Bruno and Ingeborg")); table.addCell(cell);
You will use this approach if all images are more or less the same size, and if you do not want to worry about installing images on the page.
Consideration:
Obviously, both approaches have a different result due to the choice of design. Please compare the resulting PDF files to see the difference: watermark_template.pdf compared to watermark_table.pdf