Hi,
I am using Aspose image library for converting image to pdf.
here i want to know few things.
How find the image is landscape or portrait
How to keep image in centre of the page.
How to get image horizontal and vertical resolution
Thank you in advance.
Regards
V Ramesh
@velpukondaramesh
Can you please share the details in the form of snapshot and Image so that we investigate that and try to help you with a possible sample code.
Hi @mudassir.fayyaz ,
I will add source file and output pdf document. I want that image in center of the page instead top center.
Source code:
Document doc = null;
try {
doc = new Document();
// Add a page to pages collection of document
Page page = doc.getPages().add();
// Load the source image file to Stream object
java.io.FileInputStream fs = new java.io.FileInputStream("D:\\download.png");
// Set margins so image will fit, etc.
page.getPageInfo().getMargin().setBottom(5);
page.getPageInfo().getMargin().setTop(5);
page.getPageInfo().getMargin().setLeft(5);
page.getPageInfo().getMargin().setRight(5);
// Create an image object
Image image1 = new Image();
// Add the image into paragraphs collection of the section
page.getParagraphs().add(image1);
image1.setHorizontalAlignment(HorizontalAlignment.Center);
image1.setVerticalAlignment(VerticalAlignment.Center);
image1.setImageStream(fs);
// Save resultant PDF file
doc.save("D:\\download.pdf");
} catch (Exception e) {
readImage = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (doc != null) {
doc.dispose();
doc.close();
}
}
download.png (1.7 KB)
download.pdf (17.7 KB)
Thank you in advance.
Regard’s
Ramesh
@velpukondaramesh
You can compare the height and width of the loaded image. If the height is greater than width then its a portrait image otherwise its landscape. I suggest you to please visit the follow
@ast3
Can you please try using latest Aspose.Imaging for .NET 21.2. Actually, the second image (input_new.jpg) is stored as image with dimensions 3648x2736, but it also has EXIF tag ExifOrientation.LeftBottom, which says that we must rotate image by 90 degrees.
By default we load the image as is, without any changes. If you wish to apply EXIT information to image, you should use following code:
using (var img = Aspose.Imaging.Image.Load("input.jpg"))
{
// if image has EXIF orientation inf…
Pease visit the following thread link for your convenience for information about getting the horizontal or vertical resolution of the image.
@Awadta
In order to drop in quality, the dpi is not a physical characteristic of the image, any image is always stored exclusively in pixels without taking into account dpi, and the file size depends on the size of the image in pixels. As for the task itself, you can see that no graphics editor changes the resolution in pixels when the dpi changes. This is a general approach and is not an issue of Aspose.Imaging.
If you want to preserve the quality when changing the dpi, you can first call res…
velpukondaramesh:
I will add source file and output pdf document. I want that image in center of the page instead top center.
Source code:
Document doc = null;
try {
doc = new Document();
// Add a page to pages collection of document
Page page = doc.getPages().add();
// Load the source image file to Stream object
java.io.FileInputStream fs = new java.io.FileInputStream("D:\\download.png");
// Set margins so image will fit, etc.
page.getPageInfo().getMargin().setBottom(5);
page.getPageInfo().getMargin().setTop(5);
page.getPageInfo().getMargin().setLeft(5);
page.getPageInfo().getMargin().setRight(5);
// Create an image object
Image image1 = new Image();
// Add the image into paragraphs collection of the section
page.getParagraphs().add(image1);
image1.setHorizontalAlignment(HorizontalAlignment.Center);
image1.setVerticalAlignment(VerticalAlignment.Center);
image1.setImageStream(fs);
// Save resultant PDF file
doc.save("D:\\download.pdf");
} catch (Exception e) {
readImage = null;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (doc != null) {
doc.dispose();
doc.close();
}
}
download.png (1.7 KB)
download.pdf (17.7 KB)
Thank you in advance.
Regard’s
Ramesh
Can you please share which API you are using and what is desired output you are intending to have.
Hi,
I want keep the image in the center of the page. could you please help with sample code in java
Thanks
@velpukondaramesh
Please share this information so that I may help in addressing the issue of keeping the image in the center.
Hi,
I am using aspose-pdf-20.10 and aspose-image-20.10 version.
I would like the small image or logo converted to pdf and want the image center of the page.
I have tried image alignment horizontal and vertical but it is giving the top center of the page but it is not my desire output. i want to keep the center of the page.
I hope you to understand my concern.
Thank you
@velpukondaramesh
I request you to try the following code and share your feedback.
Document doc = null;
try {
doc = new Document();
com.aspose.pdf.Page page = doc.getPages().add();
page.getPageInfo().getMargin().setBottom(5);
page.getPageInfo().getMargin().setTop(5);
page.getPageInfo().getMargin().setLeft(5);
page.getPageInfo().getMargin().setRight(5);
BufferedImage buffImage = ImageIO.read(new File(dataDir + "download.png"));
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(buffImage, "png", os);
InputStream is = new ByteArrayInputStream(os.toByteArray());
// Create image stamp
com.aspose.pdf.ImageStamp imageStamp = new com.aspose.pdf.ImageStamp(is);
imageStamp.setXIndent((page.getPageInfo().getWidth()/2) - (buffImage.getWidth()/2));
imageStamp.setYIndent((page.getPageInfo().getHeight()/2) - (buffImage.getHeight()/2));
// Add stamp to particular page
doc.getPages().get_Item(1).addStamp(imageStamp);
// Save resultant PDF file
doc.save(dataDir + "download.pdf");
} catch (Exception e) {
//readImage = null;
}
HI @mudassir.fayyaz
Thank you for your support. it is working now.