Image to Pdf - Image Cuts off


Hello,

I am using Aspose.pdf version 9.9.0.0
Before setting the section's width - my images gets cut in the pdf
After setting the section's width: image fits, but there is also an equal amount of white space by the side.
How can I make sure given an image, it fits well in the converted pdf?

Here is my sample code:

Aspose.Pdf.Generator.Pdf pdfImage = new Aspose.Pdf.Generator.Pdf();
Aspose.Pdf.Generator.Section pdfImageSection = pdfImage.Sections.Add();
Aspose.Pdf.Generator.Image img = new Aspose.Pdf.Generator.Image(pdfImageSection);
img.ImageInfo.ImageStream = new MemoryStream(buffer);

Bitmap bmp = new Bitmap(new MemoryStream(buffer));
pdfImageSection.PageInfo.PageWidth = bmp .Width;
pdfImageSection.PageInfo.PageHeight = bmp.Height;
pdfImageSection.Paragraphs.Add(img);
MemoryStream outputPDFImage = new MemoryStream();
pdfImage.Save(outputPDFImage);

Thank you !!

Hi John,


Thanks for contacting support.

As per my understanding, you are using the legacy Aspose.Pdf.Generator approach for Image to PDF conversion. However we recommend you to please try using new Document Object Model for Converting an Image to PDF. Please take a look over following code snippet which is similar to the one which you have shared above.

Please note that when you do not specify page dimensions (Width/Height), the image will be placed in center of page and page orientation will be Portrait by default. However when placing images with larger width as compared to Height, there will be some issues with image visibility. So in order to resolve such issues, its better to specify the page dimensions equal to Image dimensions.

Furthermore, in order to remove the white space around page border, you can update/set the Page margin information. Please take a look over following code snippet and in case you still face any issue, please share your image files, so that we can test the scenario in our environment.

[C#]

Aspose.Pdf.Document pdf = new Aspose.Pdf.Document();<o:p></o:p>

Aspose.Pdf.Page pdfImageSection = pdf.Pages.Add();

Aspose.Pdf.Image img = new Aspose.Pdf.Image();

img.File = "c:/pdftest/Obfuscated_Method names.png";

Bitmap bmp = new Bitmap("c:/pdftest/Obfuscated_Method names.png");

pdfImageSection.PageInfo.Width = bmp.Width;

pdfImageSection.PageInfo.Height = bmp.Height;

pdfImageSection.Paragraphs.Add(img);

// remove extra white space in four corners by setting page margin information

pdfImageSection.PageInfo.Margin.Left = pdfImageSection.PageInfo.Margin.Right = pdfImageSection.PageInfo.Margin.Top = pdfImageSection.PageInfo.Margin.Bottom = 5f;

// save resultant PDF

pdf.Save(“c:/pdftest/ImageConversion_withDOM.pdf”);

Hi Nayyer,


It did the work, thank you.

Regards

Hi John,


Thanks for your feedback. It is good to know that you have managed to accomplish your requirement.

Please keep using our API and feel free to contact us for any query or concern, we will be more than happy to extend our support.

Best Regards,

I was also having the same issue (Image cutting off in Pdf)


Now it works fine.

Thanks,
-Surendra

Hi Surendra,


Thanks for your feedback. It is good to know that above suggestion also worked for you.

Please keep using our API and feel free to contact us for any assistance.

Best Regards,