How to move location of svg on iText PDF? - java

I tried to insert svg image to pdf file.
But I don't know how to move svg position on iText PDF.
The image is always located in (0,0).
How to move svg image position on iText PDF?
For example, I want to locate on the right side more than this sample.
http://itextpdf.com/examples/iia.php?id=263

Taken from the example you mention:
PdfContentByte cb = writer.getDirectContent();
PdfTemplate map = cb.createTemplate(6000, 6000);
drawSvg(map, CITY);
cb.addTemplate(map, 0, 0);
The map object is a canvas that in this case measures 6000 by 6000 user units (by default 1 user unit = 1 point).
This canvas can be used as a Form XObject inside the PDF. In iText language, a Form XObject is known as a PdfTemplate object. You draw the SVG to this PdfTemplate, in which case the coordinates defined in the SVG are used.
Once you have drawn the SVG to the canvas, you can add the Form XObject (or PdfTemplate) to the PDF using the addTemplate() method.
The first parameter is the object itself (in the snippet the map object), the two other parameters are the coordinates (in this case (0, 0) as you already mentioned in your question).
In short, you almost answered your own question: you refer to an example with the line:
cb.addTemplate(map, 0, 0);
And you notice that The image is always located in (0,0).
Change the 0, 0 in the addTemplate() method and you change the location of the SVG image in your PDF.

Related

Add form fields to a rotated page in pdfBox

I have a requirement to add adobe form fields to an existing pdf.
The problem I encounter is when adding fields to a rotated page, the resulting form field text orientation is incorrect.
e.g. A page that is rotated 90 degrees clockwise, results in form field where the text is "vertical".
Is there a workaround to get form fields created with the correct orientation?
The appearance characteristics dictionary (/MK entry) of the widget has an /R entry where the rotation can be set. See e.g. this file.
PDAppearanceCharacteristicsDictionary fieldAppearance
= new PDAppearanceCharacteristicsDictionary(new COSDictionary());
fieldAppearance.setRotation(90);
widget.setAppearanceCharacteristics(fieldAppearance);
You may have to adjust your coordinates. To find the best coordinates, use PDFDebugger and hover at the place you want your field to be.
Update:
For checkmarks (and radio buttons) where the appearance stream is created by the user and not by PDFBox (as seen here or in the PDFBox example) you need to set the matrix yourself like this (for 90°):
yesAP.setMatrix(AffineTransform.getQuadrantRotateInstance(1, rect.getWidth(), 0));
The "1" here is for 90°. The translation needs to be adjusted for the other rotations.

Converting PDFBox coordinates to pixel coordinates of PDPage::convertToImage

I'm using PDFBox's PDPage::convertToImage to display PDF pages in Java. I'm trying to create click-able areas on the PDF page's image based on COSObjects in the page (namely, AcroForm fields). The problem is the PDF seems to use a completely different coordinate system:
System.out.println(field.getDictionary().getItem(COSName.RECT));
yields
COSArray{[COSFloat{149.04}, COSFloat{678.24}, COSInt{252}, COSFloat{697.68}]}
If I were to estimate the actual dimensions of the field's rectangle on the image, it would be 40,40,50,10 (x,y,width,height). There's no obvious correlation between the two and I can't seem to find any information about this with Google.
How can I determine the pixel position of a PDPage's COSObjects?
The pdf coordinate system is not that different from the coordinate system used in images. The only differences are:
the y-axis points up, not down
the scale is most likely different.
You can convert from pdf coordinates to image coordinates using these formulae:
x_image = x_pdf * width_image / width_page
y_image = (height_pdf - y_pdf) * height_image / height_pdf
To get the page size, simply use the mediabox size of the page that contains the annotation:
PDRectangle pageBounds = page.getMediaBox();
You may have missed the correlation between the array from the pdf and your image coordinate estimates, since a rectangle in pdf is represented as array [x_left, y_bottom, x_right, y_top].
Fortunately PDFBox provides classes that operate on a higher level than the cos structure. Use this to your advantage and use e.g. PDRectangle you get from the PDAnnotation using getRectangle() instead of accessing the COSArray you extract from the field's dictionary.

Erase content in pdf page with itext

Shortly, I want to erase the content of pdf page by background color without changing its page size. Here is more detail:
Says pdf page size is A4 paper, content can be texts or images, and the erased content is 1 cm spacing around (blue part)
I wonder is there any way to do this?
Update: my try with clipping path
// render text and image
//...
// then erase
PdfContentByte clipCB = pdfWriter.getDirectContent();
clipCB.saveState();
clipCB.setColorStroke(Color.WHITE);
clipCB.rectangle(100,100,600, 600);
clipCB.clip();
clipCB.newPath();
clipCB.restoreState();
There are a few options. The easiest thing to do would be to draw 4 rectangles in the background color around the edges. A more elegant approach would be to set a clipping path prior to rendering the page's contents.
Here's an example using a clipping path: http://www.java2s.com/Tutorial/Java/0419__PDF/Cliparegion.htm

global positioning an image with itext

Anybody knows if there are any special coordinates in iText to global positioning an image at the bottom right of the document?
I'm not sure it exists...
First we need to know if you're talking about a Document that is created from scratch or about adding an Image to an existing document.
If you create a document from scratch, then the coordinate of the bottom-right depends on the page size you've used when creating the Document object. For instance: for an A4 page, the bottom right corner has x = 595 ; y = 0 (the measurements are done in 'user unit' which correspond with points by default). So if you want to position an image in the bottom right corner, you need to use img.setAbsolutePosition(595 - img.getScaledWidth(), 0); and then just use document.add(img); to add the image.
DISCLAIMER: if you use a page size that differs from the default, or if you define a CropBox, you'll need to adapt the coordinates accordingly.
If you want to add an image to an existing document, you need to inspect the page size, and you need to check if there's a CropBox. You need to calculate the offset of the image based on these values. Again you can use setAbsolutePosition(), but you need to add the image to a PdfContentByte object obtained using the getOverContent() or getUnderContent() method (assuming that you're using PdfStamper).

Browsing PDF page content. Problem with color space and DPI

I am writing a program that would validate PDF file. I am using iText java library to get content of a file, but I have some problems with parsing it. I need to get info about color space and DPI of each image. How can I get info about position and dimensions of image in PDF? I tried to browse each XObject of PDF but I stuck, I cannot find any information about width and height of file in PDF.
Are there any other libraries which can help me?
Thank You for all answers and tips.
The image object in the PDF file stores only the Width and the Height of the image in pixels. In order to know the position and size of the image on the page, in PDF points, you have to execute the page content stream, to create a virtual rendering. The image is painted on the page using the 'Do' operator and its position and size are given by the current transformation matrix that is in place when the 'Do' operator is executed.
The DPI for a specific drawing instance is computed as 72*imageSizeInPixels/imageSizeInPoints, imageSizeInPoints being computed as described above.

Categories

Resources