How to get image horizontal and vertical resolution in java

Hi,

I am using Aspose image library for converting image to pdf.

here i want to know few things.

  1. How find the image is landscape or portrait
  2. How to keep image in centre of the page.
  3. 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

Pease visit the following thread link for your convenience for information about getting the horizontal or vertical resolution of the image.

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.