iText: Image in PdfpCell setAbsolutePosition does NOT work - image

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.

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

Place image at bottom left corner of pdf with iText

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.

JavaFx image resizing

I have a borderPane with a menu top,a grid left and an image in the center.I want the image to have the same size as the center of the border because now the image goes over my grid.
I tried this:
imageView.fitWidthProperty().bind(box.widthProperty());
where imageView is the name for my image and box is the BorderPane object.Thank you.
See JavaFX Feature Request RT-21337 Add ImageViewPane and MediaViewPane controls which contains a code attachment for a sample ImageViewPane implementation which which will resize the ImageView it contains to the area available to the region. To get the behaviour required you might also need to implement computeMinWidth and computeMinHeight on the ImageViewPane so that they return zero rather than the minimum size of the Image.
now the image goes over my grid
This is because the minimum size of your image is currently larger than the available space of the center of your BorderPane:
BorderPane does not clip its content by default, so it is possible that childrens' bounds may extend outside its own bounds if a child's min size prevents it from being fit within it space.
Some potential alternatives to prevent the center content overflowing the border:
Manually set a clip on the center node to prevent it overflowing the borders.
Dynamically resize the ImageView as in the ImageViewPane sample linked above.
Place the ImageView in a ScrollPane.
Use the css -fx-background-image attributes to display and dynamcially resize your image rather than using an ImageView.
Set the center of the borderpane first before setting the border content, that way it will be rendered underneath the border content.
Use a different construct from a borderpane (e.g. a HBoxes, VBoxes, etc.) which don't overlap their nodes when the nodes are larger than the available display area.
I tried this: imageView.fitWidthProperty().bind(box.widthProperty());
My guess from the code provided is that this didn't work because the enclosing box of the image is a dynamically resizable pane of some sort, so the minimum width of the box is being determined by the width of the image and not vice versa.

Handling size of image after orientation change WP7

I've got an Image with an overlay Canvas with some rectangles whose position is relative to image's size. The problem comes when I change the phone orientation, because image size changes (or at least, the renderization) but I can't manage to re-distribute the rectangles according to the new size, and the OrientationChanged event doesn't help me because it's fired before rendering the image (so all the rectangles go to (0,0))
Anyone can help me finding the correct event?
I'd either not use percentage widths or save them as exact values when first rendered/measured for one orientation. You can then adjust the dimensions relative to the orientation.
Update
If you can't get the exact values passed to you then you coudl load the image where teh user can't see it to measure it.
If you're getting the image and positions from an external source which always assumes a portrait orientation then why not just always display it as such in your app.
Finally got it!
The event I was looking for was the SizeChanged, I can't imagine how I didn't see it before...

How do you programatically change the source of an image whose name is ambiguous?

How do you say or achieve container.child.source = image.png?
I have a hexagon map built by specifying width and height. I draw a wireframe and place a base image for each hex into a canvas. Next, the canvas listens for a mouse click. I then run a calculation to determine which hex the mouse click was closest to. My intent is to change the source of the image that the user clicked on.
I know that mapSlate.getChildByName(mapProperties[closestHex]['baseName']) is the intended hex but I can't quite get to the point of doing a .source as Flex doesn't know that the selected object is an image.
If u are sure that mapSlate.getChildByName(mapProperties[closestHex]['baseName']) is the intended hex and that it is in fact an Image, can't you cast it into Image and change the source like:
Image(mapSlate.getChildByName(
mapProperties[closestHex]['baseName'])).source = "image.png";
or
(mapSlate.getChildByName(
mapProperties[closestHex]['baseName']) as Image).source = "image.png";

Resources