DPI Issue


#1

Dear Aspsoe Team,
i have issue in image (jpg/jpeg/tiff/tif/png/) to pdf conversion.During the conversion, it changes the image dpi value in output pdf file.
Issue Scenario:
Capture.JPG (42.6 KB)

Aspose(Java) library Detail:
i use below Aspose Library for Conversion.
Aspose.Imaging V19.7
Aspose.pdf V19.7

i attached input and actual output
fx1.zip (26.2 KB)
gr18.zip (1.4 MB)

Code for check dpi value in pdf file

com.aspose.pdf.Document document = new com.aspose.pdf.Document(“C:\Users\Java\Desktop\Doc1.docxopts.pdf”);

ImagePlacementAbsorber abs = new ImagePlacementAbsorber();
document.getPages().accept(abs);
for (Object imagePlacement : abs.getImagePlacements())
{
ImagePlacement placement = (ImagePlacement) imagePlacement;
System.out.println(“image width:” + placement.getRectangle().getWidth());
System.out.println(“image height:” + placement.getRectangle().getHeight());
System.out.println(“image horizontal resolution:” + placement.getResolution().getX());
System.out.println(“image vertical resolution:” + placement.getResolution().getY());
}

Code for image to pdf conversion

String dataDir=“C:\Users\java\Desktop\Doc1.tif”;
String extension = FilenameUtils.getExtension(dataDir);
String fileNameWithOutExt=FilenameUtils.removeExtension(dataDir);
Image image = (Image)Image.load(dataDir);
PdfOptions pdfOptions = new PdfOptions();
dataDir = fileNameWithOutExt+".pdf";
image.save(dataDir,pdfOptions);


#2

@rjnick,

I have observed your comments. Can you please visit this release notes link. Also i have shared code snippet with you to set DPI values. Please check.

// string outFileName = inputFileName + “t.pdf”;
Image image = Image.load(path+“fx1.tif”);
{
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setPageSize(new SizeF(612, 792));
image.save(path+“fx1test.pdf”, pdfOptions);
}


#3

Dear @Adnan.Ahmad,
I tried above code snippet, it changes the image height and width only. DPI value is automatically set as per height and width, but i want to set original image Dpi value to output Pdf file

i used this code

Image image = Image.load(dir);
try
{
PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setPageSize(new SizeF(445, 443));
image.save(dir+".pdf", pdfOptions);
}
finally
{
image.close();
}

output pdf file detail
image width:445.0
image height:443.0
image horizontal resolution:72
image vertical resolution:72

Input image detail
physicalWidthDpi :96
physicalHeightDpi : 96
h : 443
w : 445


#4

@rjnick,

I like to inform that i have worked with your issue in detail. Can you please share more sample files along with generated result for testing purpose. Also please explain your issue in more details.


#5

@rjnick

I have worked with source file shared by you. Can you please try to use following sample code on your end. This will help you to achieve your requirements. Please share feedback with us if there is still an issue.

Image image = Image.load(getAbsolutePath(“fx1.tif”));
try
{
System.out.println(image.getWidth());
System.out.println(image.getHeight());
System.out.println(((RasterImage)image).getHorizontalResolution());
System.out.println(((RasterImage)image).getVerticalResolution());

  	PdfOptions pdfOptions = new PdfOptions();
  	double width=image.getWidth()*(72/((RasterImage)image).getHorizontalResolution());
  	double height=image.getHeight()*(72/((RasterImage)image).getVerticalResolution());
  	System.out.println(width);
  	System.out.println(height);
  	pdfOptions.setPageSize(new SizeF((int)width, (int)height));			
  	image.save(getAbsolutePath("fx1.pdf"), pdfOptions);
  }
  finally
  {
    image.close();
  }
  
  Document document = new Document(getAbsolutePath("fx1.pdf"));
  ImagePlacementAbsorber abs = new ImagePlacementAbsorber();		
  document.getPages().accept(abs);
  
  for (Object imagePlacement : abs.getImagePlacements())
  {
  	ImagePlacement placement = (ImagePlacement) imagePlacement;
  	System.out.println("image width:" + placement.getRectangle().getWidth());
  	System.out.println("image height:" + placement.getRectangle().getHeight());
  	System.out.println("image horizontal resolution:" + placement.getResolution().getX());
  	System.out.println("image vertical resolution:" + placement.getResolution().getY());
  }

Change of custom DPI value issue
#6

Dear @Adnan.Ahmad
i tried the code but i got some error in RasterImage.

Error
Untitled.jpg (109.7 KB)


#7

@rjnick,

Please add imports for raster images and share feedback with us if there is still an issue. It is working fine on my end, only you need to add imports.


#8

Dear @Adnan.Ahmad
Above code work fine to me, Thank you for your Response


#9

@rjnick,

You are very welcome.