Hi -
Is there a way to align an image vertically within a page beside me calculating the top margin?
Thanks!
Hello Dave,
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.
// 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,
Hi Edu,
//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 Nayyar,
Hi Edu,
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));