We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Vertically align image?

Hi -



Is there a way to align an image vertically within a page beside me calculating the top margin?



Thanks!

Hello Dave,


Thanks for using our products

I am afraid currently we don not have any method in Image or ImageInfo class which can vertically align the image inside PDF page but for the sake of implementation, I have logged this requirement as PDFJAVA-32919 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of correction. We are really sorry for this inconvenience.

However I would request you to please share some more details regarding your requirement i.e. do you need some method similar to ImageInfo.setAlignment method which takes value of AlignmentType enumeration as an argument to Left, Justify, FullJustify, Center and Right align the image inside PDF page. Please note that this method horizontally aligns the image inside PDF page. Or you need a method which can take some integer value to specify the vertical alignment of the image inside PDF.

As a workaround, you may consider using FloatingBox to place image inside PDF document. All the objects are placed in Flow-layout (Top-Left to Bottom-Right) but FloatingBox is placed in Absolute position.

The issues you have found earlier (filed as PDFJAVA-32919) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Hello Dave,


Thanks for your patience.


In order to accomplish your requirement, you may consider using setVerticalAlignment() method of ImageInfo class. Please take a look over the following code snippet.


[Java] Pdf pdf = new Pdf();
// Create a new section in the Pdf object
Section sec1 = pdf.getSections().add();
aspose.pdf.Image image1 = new aspose.pdf.Image();
image1.getImageInfo().setFile("d:/pdftest/_thumb_8498.png");
image1.getImageInfo().setImageFileType(ImageFileType.Png);
image1.getImageInfo().setVerticalAlignment(aspose.pdf.VerticalAlignmentType.Center);
image1.getImageInfo().setAlignment(AlignmentType.Center);
// Add the table in paragraphs collection of the desired section
sec1.getParagraphs().add(image1);
pdf.save("d:/pdftest/checkBug-32919.pdf");

Hi,


Is this still the way of vertically centering images in Aspose.Pdf 9.1.0? There doesn’t seem to be a ImageInfo.setVerticalAlignment() method, at least on Java.

Hi Edu,


Thanks for contacting support.

The approach of using aspose.pdf package is legacy approach and we recommend our customers to try using the new Document Object Model (DOM) of com.aspose.pdf package. In order to vertically aligned the image inside PDF page, please get the page dimensions and specify the image position using appropriate coordinates. In can you need to display the image at center of page (Vertically and Horizontally center aligned), then you may consider getting page height/width information, divide the value by 2 to get the center point of page and then use same values to specify the image position.

[Java]

//Open a document<o:p></o:p>

com.aspose.pdf.Document pdfDocument1 = new com.aspose.pdf.Document();<o:p></o:p>

pdfDocument1.getPages().add();<o:p></o:p>

<o:p> </o:p>

int PageHeight = (int)pdfDocument1.getPages().get_Item(1).getPageInfo().getHeight();<o:p></o:p>

int PageWidth = (int)pdfDocument1.getPages().get_Item(1).getPageInfo().getWidth();<o:p></o:p>

System.out.println("H = " +PageHeight + " W = "+PageWidth);<o:p></o:p>

//Set coordinates<o:p></o:p>

int lowerLeftX = PageWidth/2;<o:p></o:p>

int lowerLeftY = PageHeight/2;<o:p></o:p>

int upperRightX = (PageWidth/2)+200;<o:p></o:p>

int upperRightY = (PageHeight/2) + 200;<o:p></o:p>

<o:p> </o:p>

//Get the page you want to add the image to<o:p></o:p>

com.aspose.pdf.Page page = pdfDocument1.getPages().get_Item(1);<o:p></o:p>

<o:p> </o:p>

//Load image into stream<o:p></o:p>

java.io.FileInputStream imageStream = new java.io.FileInputStream(new java.io.File(“c:/pdftest/Pizza.JPG”));<o:p></o:p>

<o:p> </o:p>

//Add an image to the Images collection of the page resources<o:p></o:p>

page.getResources().getImages().add(imageStream);<o:p></o:p>

<o:p> </o:p>

//Using the GSave operator: this operator saves current graphics state<o:p></o:p>

page.getContents().add(new com.aspose.pdf.Operator.GSave());<o:p></o:p>

<o:p> </o:p>

//Create Rectangle and Matrix objects<o:p></o:p>

com.aspose.pdf.Rectangle rectangle = new com.aspose.pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);<o:p></o:p>

com.aspose.pdf.Matrix matrix = new com.aspose.pdf.Matrix(new double[] { rectangle.getURX() - rectangle.getLLX(), 0, 0, rectangle.getURY()- rectangle.getLLY(), rectangle.getLLX(), rectangle.getLLY() });<o:p></o:p>

<o:p> </o:p>

//Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed<o:p></o:p>

page.getContents().add(new com.aspose.pdf.Operator.ConcatenateMatrix(matrix));<o:p></o:p>

com.aspose.pdf.XImage ximage = page.getResources().getImages().get_Item(page.getResources().getImages().size());<o:p></o:p>

<o:p> </o:p>

System.out.println("Height = "+ximage.getHeight());<o:p></o:p>

System.out.println("Width = "+ximage.getWidth());<o:p></o:p>

<o:p> </o:p>

//Using Do operator: this operator draws image<o:p></o:p>

page.getContents().add(new com.aspose.pdf.Operator.Do(ximage.getName()));<o:p></o:p>

<o:p> </o:p>

//Using GRestore operator: this operator restores graphics state<o:p></o:p>

page.getContents().add(new com.aspose.pdf.Operator.GRestore());<o:p></o:p>

<o:p> </o:p>

//Save the new PDF<o:p></o:p>

pdfDocument1.save(“c:/pdftest/ImageAdded_document.pdf”);<o:p></o:p>

<o:p> </o:p>

//Close image stream<o:p></o:p>

imageStream.close();

atlassian:
Hi,

Is this still the way of vertically centering images in Aspose.Pdf 9.1.0? There doesn't seem to be a ImageInfo.setVerticalAlignment() method, at least on Java.
Hi Edu,

As the current autoported MergedAPI of Aspose.Pdf for Java is ported from Aspose.Pdf for .NET, so the method ImageInfo.setVerticalAlignment() does not exist in new API because the respective method does not exist in Aspose.Pdf.Generator namespace. Therefore as a solution, please try using DOM approach shared above.

Hi Nayyar,


Thank you for your response.

Unfortunately, due to PDFNEWJAVA-34178, it seems this approach is not going to work for existing documents. Is there any other way of doing this in that case?

Thank you.

Hi Edu,


As a workaround, please try using following code lines to get page dimensions.

[Java]

com.aspose.pdf.facades.PdfFileInfo
info =
new
com.aspose.pdf.facades.PdfFileInfo();
<o:p></o:p>

info.bindPdf("c:/pdftest/square.pdf");

System.out.println("Height = "+ info.getPageHeight(1));

System.out.println("Width = "+ info.getPageWidth(1));