Free Support Forum - aspose.com

How to set PDF page matching the image size?

Hello.


I need to convert an image to PDF and I’d like to set page dimensions exactly matching the image size, so the PDF won’t contain any margins. The solution should work for any image size.

I checked several suggestions appearing in the forum but did not find anything working for me.
Here is the code:
var pdf = new Pdf();
var section = pdf.Sections.Add();
var image = new Image(section);
image.ImageInfo.ImageFileType = ImageFileType.Jpeg;
                    image.ImageInfo.ImageStream = msOriginalData;
image.ImageInfo.Title = “”;
section.PageInfo.Margin.Left = 0;
section.PageInfo.Margin.Right = 0;
section.PageInfo.Margin.Top = 0;
section.PageInfo.Margin.Bottom = 0;
		    var bitmap = new Bitmap(msOriginalData);
section.PageInfo.PageHeight = bitmap.Height + 1;
section.PageInfo.PageWidth = bitmap.Width;
section.Paragraphs.Add(image);
		    pdf.Save(msPdfData);

I’m attaching the image and the resulting PDF, it contains white margins on right and bottom.
Rgds, Leonid

Hi Lenoid,


Thanks for your inquiry. Please use DOM approach to convert image to PDF. Please check following code snippet, hopefully it will help you to achieve your requirement.


// Instantiate Document Object<o:p></o:p>

Document doc = new Document();

// Add a page to pages collection of document

Page page = doc.Pages.Add();

// Load the source image file to Stream object

FileStream fs = new FileStream(myDir + "Image01.jpg", FileMode.Open, FileAccess.Read);

byte[] tmpBytes = new byte[fs.Length];

fs.Read(tmpBytes, 0, Convert.ToInt32(fs.Length));

MemoryStream mystream = new MemoryStream(tmpBytes);

// Instantiate BitMap object with loaded image stream

Bitmap b = new Bitmap(mystream);

// Set margins so image will fit, etc.

page.PageInfo.Margin.Bottom = 0;

page.PageInfo.Margin.Top = 0;

page.PageInfo.Margin.Left = 0;

page.PageInfo.Margin.Right = 0;

page.PageInfo.Width = b.Width;

page.PageInfo.Height = b.Height;

//page.CropBox = new Aspose.Pdf.Rectangle(0, 0, b.Width, b.Height);

// Create an image object

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

// Add the image into paragraphs collection of the section

page.Paragraphs.Add(image);

// Set the image file stream

image.ImageStream = mystream;

// Save resultant PDF file

doc.Save(myDir+"Image2PDF_DOM.pdf");

// Close memoryStream object

mystream.Close();

Please feel free to contact us for any further assistance.


Best Regards,

Thanks, this works fine for the image I sent before, however on another image it still leaves white space on the page below the image.


I’m attaching the problematic image and the corresponding PDF.

Do not see option to attach additional files to the post.

Here are the links:
https://drive.google.com/folderview?id=0B9ZcuDVKA5LSYWY3QWhGcWtiV2M&usp=sharing

Actually the image is distorted in PDF, it does not keep original ratio of width/height.

I added:

page.PageInfo.Width = bitmap.Width;
page.PageInfo.Height = bitmap.Height;

Now it looks fine, no white margins and the ration is OK.

Hi Leonid,


Thanks for your feedback. Yes you need to set page width/height as per image. It is good to know that you have managed to accomplish the requirements.

Please feel free to ask any question or concern, we will be more than happy to extend our support.

Best Regards,