Free Support Forum - aspose.com

Inserting big pictures on PDF leads to empty pages

Dear Aspose-Team,

I am facing another problem. I try to convert an image (.jpg) to a PDF-File.
It seems to work with smaller images, but larger images lead to empty pages with no image at all.
I am using the following code, oriented on the samples you supply within the documentation.
This code should scale the image to fit the page, doesn't it?
I also attached a sample image, which I cannot convert to PDF via this code.

Please give me a hint :)

Regards,
Michael Eberhardt

public String ConvertImageToPdf(string inputFileName, string outputFileName)
{
Pdf pdf = new Pdf();
// create a section and add it to pdf document
Aspose.Pdf.Generator.Section MainSection = pdf.Sections.Add();
//Add the radio form field to the paragraphs collection of the section
// create an image object
Aspose.Pdf.Generator.Image sample_image = new Aspose.Pdf.Generator.Image();
// specify the image file path information

sample_image.ImageInfo.File = inputFileName;
// specify the image file type

if (inputFileName.ToLower().Contains(".jpg")) sample_image.ImageInfo.ImageFileType = ImageFileType.Jpeg;
if (inputFileName.ToLower().Contains(".bmp")) sample_image.ImageInfo.ImageFileType = ImageFileType.Bmp;
if (inputFileName.ToLower().Contains(".png")) sample_image.ImageInfo.ImageFileType = ImageFileType.Png;
if (inputFileName.ToLower().Contains(".gif")) sample_image.ImageInfo.ImageFileType = ImageFileType.Gif;
if (inputFileName.ToLower().Contains(".tif")) sample_image.ImageInfo.ImageFileType = ImageFileType.Tiff;
if (inputFileName.ToLower().Contains(".tiff")) sample_image.ImageInfo.ImageFileType = ImageFileType.Tiff;
if (inputFileName.ToLower().Contains(".fax")) sample_image.ImageInfo.ImageFileType = ImageFileType.Tiff;
// specify the image width information equal to page width
sample_image.ImageInfo.FixWidth = MainSection.PageInfo.PageWidth - MainSection.PageInfo.Margin.Left - MainSection.PageInfo.Margin.Right;
// specify the image Height information equal to page Height
sample_image.ImageInfo.FixWidth = MainSection.PageInfo.PageHeight - MainSection.PageInfo.Margin.Top - MainSection.PageInfo.Margin.Bottom;

// create bitmap image object to load image information
Bitmap myimage = new Bitmap(inputFileName);
// check if the width of the image file is greater than Page width or not
if (myimage.Width > MainSection.PageInfo.PageWidth)
// if the Image width is greater than page width, then set the page orientation to Landscape
MainSection.IsLandscape = true;
else
// if the Image width is less than page width, then set the page orientation to Portrait
MainSection.IsLandscape = false;
// add image to paragraphs collection of section
MainSection.Paragraphs.Add(sample_image);
// save the resultant PDF
pdf.Save(outputFileName);
return outputFileName;
}public String ConvertImageToPdf(string inputFileName, string outputFileName)
{
Pdf pdf = new Pdf();
// create a section and add it to pdf document
Aspose.Pdf.Generator.Section MainSection = pdf.Sections.Add();
//Add the radio form field to the paragraphs collection of the section
// create an image object
Aspose.Pdf.Generator.Image sample_image = new Aspose.Pdf.Generator.Image();
// specify the image file path information

sample_image.ImageInfo.File = inputFileName;
// specify the image file type

if (inputFileName.ToLower().Contains(".jpg")) sample_image.ImageInfo.ImageFileType = ImageFileType.Jpeg;
if (inputFileName.ToLower().Contains(".bmp")) sample_image.ImageInfo.ImageFileType = ImageFileType.Bmp;
if (inputFileName.ToLower().Contains(".png")) sample_image.ImageInfo.ImageFileType = ImageFileType.Png;
if (inputFileName.ToLower().Contains(".gif")) sample_image.ImageInfo.ImageFileType = ImageFileType.Gif;
if (inputFileName.ToLower().Contains(".tif")) sample_image.ImageInfo.ImageFileType = ImageFileType.Tiff;
if (inputFileName.ToLower().Contains(".tiff")) sample_image.ImageInfo.ImageFileType = ImageFileType.Tiff;
if (inputFileName.ToLower().Contains(".fax")) sample_image.ImageInfo.ImageFileType = ImageFileType.Tiff;
// specify the image width information equal to page width
sample_image.ImageInfo.FixWidth = MainSection.PageInfo.PageWidth - MainSection.PageInfo.Margin.Left - MainSection.PageInfo.Margin.Right;
// specify the image Height information equal to page Height
sample_image.ImageInfo.FixWidth = MainSection.PageInfo.PageHeight - MainSection.PageInfo.Margin.Top - MainSection.PageInfo.Margin.Bottom;

// create bitmap image object to load image information
Bitmap myimage = new Bitmap(inputFileName);
// check if the width of the image file is greater than Page width or not
if (myimage.Width > MainSection.PageInfo.PageWidth)
// if the Image width is greater than page width, then set the page orientation to Landscape
MainSection.IsLandscape = true;
else
// if the Image width is less than page width, then set the page orientation to Portrait
MainSection.IsLandscape = false;
// add image to paragraphs collection of section
MainSection.Paragraphs.Add(sample_image);
// save the resultant PDF
pdf.Save(outputFileName);
return outputFileName;
}

Hi Johannes,

Thanks for using our products.

I have tested the scenario using Aspose.Pdf for .NET v6.2.0 and I am unable to notice any problem. The resultant PDF that I have generated is also in attachment. Please try using this version and in case you still face any issue, please share some details regarding your working environment. We apologize for your inconvenience.

I tried around a bit, but now I receive "invalid image or template handle".
Same Code as above.

What can I do?

Regards,
Michael Eberhardt

Hi Michael,

Can you please share which version of product you are using ? If you are using v6.2.0, please share some details regarding your working environment. We are sorry for your inconvenience.

I am using 6.2.0.0 on Win7 /x64.
It seems like the error occurs only when I try to save a PDF Document twice!
Got a workaround now, so it is not urgent anymore.

Thanks a lot!
Michael

Hi Michael,

Thanks for sharing the details. I have already noticed in your first post, same code snippet is pasted twice and I had an understanding that it happened by mistake. When I have tried executing both code snippets, I am able to notice the same problem. I am not entirely certain about your requirement of using the same image and generating two PDF documents. I think the viable solution would be to generate PDF document and create is copy in second step. In case you need any further assistance, please do let me know.