Place image at bottom left corner of pdf with iText - image

I'm having some trouble placing an image in the bottom-left corner of a PDF document.
Here's my code:
PdfReader reader = new PdfReader("source.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(pdfTarget));
Image qrImg = Image.getInstance("qrcode.png");
qrImg.setAbsolutePosition(0,0);
// place the image at the i-th page
PdfContentByte content = stamper.getOverContent(i);
content.addImage(qrImg);
This works for almost every pdf document I tried unless a single one that you can find here: https://ufile.io/50016
For this document the bottom left corner starts at (50,50) so the absolute position should be (50,50) that is incorrect for all the others pdfs.
I can't find a way to place the image at (0,0) or any other fixed absolute position that results in placing it always at the bottom left corner.
Any advice?

Pdf documents describe the page with a key called MediaBox. This is the raw size of the page. There's another key called CropBox that defines the visible area of the page. In your document the cropbox start at 54,55.4 and that's the offset you'll have to apply to the image. Check PdfReader.getCropBox() to get the dimensions.

Related

Rotating an image after being added in a PDF using Reportlab

I'm trying to create a PDF document with an image that will be rotated.
I can successfully create the PDF document, add the image and save it, but as soon as I try rotating it, I'm having many issues.
One thing I'm trying to understand is where is the axe for the rotation, is it at 0,0 (bottom left for PDF) or somewhere else?
Here's my current running code:
output = BytesIO()
# create a new PDF with Reportlab
c = canvas.Canvas(output)
c.saveState()
c.translate(X?, Y?) # TODO find this !
c.rotate(45)
c.drawImage('path/to/image.png', position_left, position_top, width=img_width, height=img_height, mask='auto')
c.restoreState()
c.save()
(Since PDF documents (0,0) point is at bottom right, I have position_left and position_top that refers to the top left point of the document, where I want to place the image).
My issue here is that I don't know how what values to put on c.translate(X?, Y?) to make the image rotate on its center axis, i.e. stays at the same position on the document, but rotate on itself from its center point.
Is using c.translate(X?, Y?) would work or do I need to use advanced mechanisms to rotate "just" the image on the PDF document? If so, can you point me to the right track?
Thank you for your help.
You can use the technique mentioned in below SO Thread
A simple method for rotate images in reportlab
from reportlab.platypus.flowables import Image
class RotatedImage(Image):
def wrap(self,availWidth,availHeight):
h, w = Image.wrap(self,availHeight,availWidth)
return w, h
def draw(self):
self.canv.rotate(90)
Image.draw(self)
I = RotatedImage('../images/somelogo.gif')

iText: Image in PdfpCell setAbsolutePosition does NOT work

I have a PdfPTable with 6 PdfPCell. I want to put an image in each cell at an absolute position. I just can get it to work, what am I doing wrong? As shown in the pic, the image always show on the to left (default) location.
laImage.scaleAbsolute(50, 50);
laImage.setAbsolutePosition(0, 100);
laCell.addElement(laImage);
The setAbsolutePosition() method can be used to define the absolute position on a page (using the coordinates of the page) when adding an image to a document using document.add(). It is very surprising to discover that somebody would be using that method to define a relative position in a PdfPCell.
Depending on what you really want, you could either change the padding of the PdfPCell (to create a distance between one or more of the cell borders and its content) or you could define a border for the Image object. This will introduce relative offsets.

How to display invoice pdf logo as full size in magento?

I have image with dimension of 961*210. I need to display this images in invoice pdf generation in full page(like banner image) on the top of the logo area.
What are the values I need to set for parameters(x1,y1,x2,y2) in insertLogo() function?.
$page->drawImage($image, x1, y1, x2, y2);
I tried multiple values but I can't figured it out. please help me?. I need exact coordinates for full page image (like banner image).
Ah the joys of Zend_Pdf. The parameters for the drawImage() method are as follows:
$image = image as created using
$image = Zend_Pdf_Image::imageWithPath($path);
where path is the path to the desired image on the filesystem
$x1 = distance from the left of the page to the left of the image
$y1 = distance from the bottom of the page to the bottom of the image
$x2 = distance from the left of the page to the right of the image
$y2 = distance from the bottom of the page to the top of the image
Unfortunately working with PDF's is a very manual process, and you are just going to have to try different values until you get it right. Also remember print margins, you'll want to leave a bit of space for this.

Table, rectangle placed on top of an image is not coming properly in preview mode in SSRS

I wanted to draw a rectangle with rounded corners in SSRS. But, after lot of research i got to know that currently there is no property for that. So, i am trying to use an image of a rectangle with rounded corners and on top of that trying to align the table and other controls within the image. But, when i am previewing it or exporting it to a PDF file, first the image is getting displayed, then below that all other controls comes. Am i doing anything wrong. Please let me know.
Why don't you try to enclose your image rectangle and other controls etc. within an SSRS Rectangle with BorderStyle = None...
Try following:
Create an image (the rectangle with rounded corners) in your favourite image drawing program. I did mine in Microsoft Paint
and save it as JPEG.
In SSRS report design, in Report Data pane, right click on Images >> Add Image and point to the image that you created in Step 1. (Untitled, in my case see the fig. below)
In SSRS report design, add a Rectangle from the Toolbox to the design surface and in the Properties Pane:
a) look for the BackgroundImage property Click its + sign. then
b) Enter values for Source as Embedded, BackgroundRepeat as Clip and the Value property , when you click the dropdown for its values, should show you the name of the image that you embedded in Step-2, select this name
Resize your rectangle to fit the shape and add your items to this rectangle.
EDIT:
Regarding the question in the Comment, I don't think that the size of the image rectangle can be increased dynamically. If that's the case then you may need to find some other work-around
HTH

Change Image already on Stage as3

Okay this solution seems to not be findable.
I have a border type image on stage (circular) and I am dynamically loading an image from xml in my document class. I have a place holder image inside the border. Can I replace that image with the one that is dynamically loaded?
Or do I have to addChild and then scale and transform it all through code?
I would just remove the place holder image and add the new image into the same container and scale/position it. It is simple to do with code.
container.removeChild(placeHolderImage);
container.addChild(newLoadedImage);
newLoadedImage.scaleX = newLoadedImage.scaleY = 1.5;
newLoadedImage.x = 100;
newLoadedImage.y = 100;
You can possibly draw into the bitmap data of the image already on screen but I don't see any downsides with just removing the old one and adding the new image.

Resources