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.
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,
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
// 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?
Hi,