Free Support Forum - aspose.com

Image size problem-When converting it into pdf

hi Aspose Developement Team,
I followed the code give in documentation for converting an image into pdf. But the image width is more than the page width.So i am getting a only part of the image,which is not wished.I tried with fixheight and fixwidth.yet it is not solved.So please tell me how can I do it and provide me some code also.

Thanking You

Hello Surendrababu,

Thanks for using our products.

In order to set the image Height and Width information, you need to try using img.ImageInfo.FixHeight and img.ImageInfo.FixWidth properties. However, in order to set the image Height and Width information equal to page Height and Width, you may try using following code lines.

[C#]
// set the image Height equal to page height information
img.ImageInfo.FixHeight = sec1.PageInfo.PageHeight;
// set the image width equal to page width information
img.ImageInfo.FixWidth = sec1.PageInfo.PageWidth;

In case it does not resolve your problem, please share the image files so that we can test the scenario at our end. We apologize for your inconvenience.

I have used the following code to control the image height and width .Is there any wrong with this?I am providing you the image link also.
http://images2.wikia.nocookie.net/__cb20060928165651/censorship/images/8/8a/Wikinews-logo.png


Pdf pdf1 = new Pdf();
Section sec1 = pdf1.Sections.Add();
Aspose.Pdf.Image img1 = new Aspose.Pdf.Image(sec1);
sec1.Paragraphs.Add(img1);
if (img1.ImageInfo.FixHeight > sec1.PageInfo.PageHeight)
{
img1.ImageInfo.FixHeight = sec1.PageInfo.PageHeight;
}
if (img1.ImageInfo.FixWidth > sec1.PageInfo.PageHeight)
{
img1.ImageInfo.FixWidth = sec1.PageInfo.PageWidth;
}

//sec1.TextInfo.Alignment = AlignmentType.Center;
//img1.ImageInfo.Alignment = AlignmentType.Center;
img1.ImageInfo.File = @"C:\Documents and Settings\****\Desktop\testimage3.png";
img1.ImageInfo.ImageFileType = ImageFileType.Png;
if (img1.ImageInfo.FixHeight> sec1.PageInfo.PageHeight)
{
img1.ImageInfo.FixHeight = sec1.PageInfo.PageHeight;
}
if (img1.ImageInfo.FixWidth > sec1.PageInfo.PageHeight)
{
img1.ImageInfo.FixWidth = sec1.PageInfo.PageWidth;
}

//ImageFileType img = img1.ImageInfo.ImageFileType;
//img1.ImageInfo.Title = "testimage";
pdf1.Save(@"C:\Documents and Settings\*****\Desktop\test.pdf");

This didn't work for me.I wrote this code after I load the image.


Hello Surendrababu,

Thanks for sharing the information.

I have tested the scenario with Aspose.Pdf for .NET 5.0.0 using following code snippet and as per my observations, the image dimensions are properly being set. The resultant PDF that I have generated is also in attachment, please take a look.

[C#]

//Instantiate a Pdf object by calling its empty constructor
Pdf pdf = new Pdf();
String imagefile = http://images2.wikia.nocookie.net/__cb20060928165651/censorship/images/8/8a/Wikinews-logo.png;
// Create a section object
Aspose.Pdf.Section section1 = pdf.Sections.Add();

// creat an image object
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(section1);
image1.ImageInfo.File = imagefile;
image1.ImageInfo.ImageFileType = ImageFileType.Png;
// set the image width equal to page width
image1.ImageInfo.FixWidth = section1.PageInfo.PageWidth;
// set the image Height equal to page Height
image1.ImageInfo.FixHeight = section1.PageInfo.PageHeight;
// add the image to paragraphs collection of the PDF document
section1.Paragraphs.Add(image1);
pdf.Save(@"D:\pdftest\Image_DimensionsIssue.pdf");

However, if you don't specify the Width and Height information and set the left margin information as image1.Margin.Left = -90; the image is properly displayed. Please take a look over Image_DimensionsIssue-leftmargin.pdf file in attachment generated after following these steps.

Hi Aspose,

Still i got the same error regarding big images. ,here I am attaching the document,code,pdf file.Plese test this image and suggest me code.

Hello Surendrababu,

Thanks for sharing the resource files

As I have shared earlier, you may set the image width equal to page width. However, its more better to try using the following code lines to specify the image Height and Width. I have tested the scenario using Aspose.Pdf for .NET 5.0.1 (available for download over this link) and I am able to generate the PDF document properly. Please find attached the resultant PDF that I have generated.

[C#]

// set the page orientation to landscape

pdf.IsLandscape = true;

// set the image width equal to page width
image1.ImageInfo.FixWidth = section1.PageInfo.PageWidth-section1.PageInfo.Margin.Left-section1.PageInfo.Margin.Right;
// set the image Height equal to page Height
image1.ImageInfo.FixHeight = section1.PageInfo.PageHeight-section1.PageInfo.Margin.Top-section1.PageInfo.Margin.Bottom;

Hi Shabhajaz,
Sorry,I don’t want every image to be saved in landscape and dimensions ,when the image is bigger only i would like to validate.I am using the latest dll itself.
I have used the following code to convert an image into pdf
string url = @FilePath;

//Prepare the web page we will be asking for
Request = (HttpWebRequest)WebRequest.Create(url);
Request.Method = “GET”;
Request.ContentType = “application/image”;
Request.Credentials = CredentialCache.DefaultCredentials;

//Execute the request
Response = (HttpWebResponse)Request.GetResponse();
resStream = Response.GetResponseStream();
//Write content into the MemoryStream
resReader = new BinaryReader(resStream);
ImageStream = new MemoryStream(resReader.ReadBytes((int)Response.ContentLength));

//Declaring the Input and Output Memory Streams
OutStream = new MemoryStream();

//Converting and return the stream
Opdf = new Pdf();
Aspose.Pdf.Section section1 = Opdf.Sections.Add();

// creat an image object
Image image1 = new Image(section1);
image1.ImageInfo.ImageStream = ImageStream;
//image1.ImageInfo.ImageFileType = ImageFileType.Jpeg;
// add the image to paragraphs collection of the PDF document
section1.Paragraphs.Add(image1);
// set the image width equal to page width
//Now i would like compate image dimensions with page dimension.
//But I am unable to get image width and height ,they are always zero(why?).So How can I //compare

if (image1.ImageInfo.FixWidth > section1.PageInfo.PageWidth)/
{
image1.ImageInfo.FixWidth = section1.PageInfo.PageWidth;
}
if (image1.ImageInfo.FixHeight > section1.PageInfo.PageHeight)
{
// set the image Height equal to page Height
image1.ImageInfo.FixHeight = section1.PageInfo.PageHeight;
}

Opdf.Save(OutStream);

Please respond me in this way?
Thanking you.
sbj

Hello Surendrababu,

In case you don't need to set the page orientation as Landscape for every image, you may try using the following code snippet in which first I am getting the image dimensions and in case the image width is greater than page width, I have specified the page orientation as Landscape and in case the image width is less than page width, the page orientation is kept as Portrait. Please take a look over the following code snippet and in case it does not satisfy your requirements or you have any further query, please feel free to contact. We apologize for your inconvenience.

[C#]

// Retrieve names of all the Pdf files in a particular Directory
string[] fileEntries = Directory.GetFiles(@"D:\pdftest\TempImages\", "*.gif");
//Instantiate a Pdf object by calling its empty constructor
Pdf pdf = new Pdf();

int counter;
for (counter = 0; counter < fileEntries.Length ; counter++)
{
// Create a section object
Aspose.Pdf.Section section1 = pdf.Sections.Add();
// creat an image object
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(section1);
image1.ImageInfo.File = fileEntries[counter];
image1.ImageInfo.ImageFileType = ImageFileType.Gif;
// Create a BitMap object in order to get the information of image file
Bitmap myimage = new Bitmap(fileEntries[counter]);
// check if the width of the image file is greater than Page width or not
if (myimage.Width > section1.PageInfo.PageWidth)
{
// if the Image width is greater than page width, then set the page orientation to Landscape
section1.IsLandscape = true;
// set the image width equal to page width
image1.ImageInfo.FixWidth = section1.PageInfo.PageWidth - section1.PageInfo.Margin.Left - section1.PageInfo.Margin.Right;
// set the image Height equal to page Height
image1.ImageInfo.FixHeight = section1.PageInfo.PageHeight - section1.PageInfo.Margin.Top - section1.PageInfo.Margin.Bottom;
}
else
// if the Image width is less than page width, then set the page orientation to Portrait
section1.IsLandscape = false;
// add the image to paragraphs collection of the PDF document
section1.Paragraphs.Add(image1);
}

// save the PDF document
pdf.Save(@"D:\pdftest\TempImages\Image_DimensionsIssue.pdf");

is there a java version example?

I can’t find the Bitmap class in Java version!

Hi,


I am working over this query and will get back to you soon. We apologize for this inconvenience.