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;
}