How to convert JPEG,GIF,PNG,TIF images to PDF?
Hi Laksh,
Thanks for using our products.
Kindly visit the following documentation links for more details and code snippets as per your requirement.
- Working with Images
- [Working with Images (Facades)]
(http://www.aspose.com/docs/display/pdfnet/Working+with+Images+%28Facades%29)
- Working with Images (Generator)
Please feel free to contact support in case you need any further assistance.
Thanks & Regards,
I couldn’t find any example of converting images into PDF. i found some that converts PDF into some other formats but thats not what im looking for.
You could try something I did in C#:
Well i thought PDF
Aspose.Pdf.Generator is old and obsolete and we have to use new Document class?
Hi Laxmikant,
Thanks for contacting support.
Please note that the link Working with Images (Generator) shared earlier by Rashid explains the steps on how to convert Image file into PDF format. However you might have been confused with other links that he shared and they mainly contain information related to converting PDF pages into Image or Inserting/Updating/Removing images form PDF file.
You may consider visiting the following link for required information on How to Convert an Image to PDF.
We are sorry for this confusion and inconvenience.
philip.betts:
You could try something I did in C#://Assuming you have some section....Section Chapter//Set the image to your image fileSystem.Drawing.Image img =//Create a temporary memorystreamMemoryStream ms = new MemoryStream();//Save the image into the memorystreamimageIn.Save(ms,img.RawFormat);//Create a Generator ImageAspose.Pdf.Generator.Image image1 = new Aspose.Pdf.Generator.Image(Chapter);//Add the image to the chapterChapter.Paragraphs.Add(image1);//Need some way to tell Aspose what kind of file you are dealing with, I wrote my own function for thisimage1.ImageInfo.ImageFileType = determineExtension(img.RawFormat);//Give Aspose's image the memory stream of the imageimage1.ImageInfo.ImageStream = ms;Hope that helps.
Laksh:
Well i thought PDF
Aspose.Pdf.Generator is old and obsolete and we have to use new Document class?
Thanks for quick reply and i really appreciate that.
Hi Laxmikant,
//open document<o:p></o:p>
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document();
pdfDocument.Pages.Add();
//set coordinates
int lowerLeftX = 100;
int lowerLeftY = 100;
int upperRightX = 200;
int upperRightY = 200;
//get the page where image needs to be added
Page page = pdfDocument.Pages[1];
//load image into stream
FileStream imageStream = new FileStream("D:\\pdftest\\Penguin.PNG", FileMode.Open);
//add image to Images collection of Page Resources
page.Resources.Images.Add(imageStream);
//using GSave operator: this operator saves current graphics state
page.Contents.Add(new Operator.GSave());
//create Rectangle and Matrix objects
Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
Aspose.Pdf.DOM.Matrix matrix = new Aspose.Pdf.DOM.Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });
//using ConcatenateMatrix operator: defines how image must be placed
page.Contents.Add(new Operator.ConcatenateMatrix(matrix));
XImage ximage = page.Resources.Images[page.Resources.Images.Count];
//using Do operator: this operator draws image
page.Contents.Add(new Operator.Do(ximage.Name));
//using GRestore operator: this operator restores graphics state
page.Contents.Add(new Operator.GRestore());
//save updated document
pdfDocument.Save("D:\\pdftest\\Image-In-PDF.pdf");
Well the code using Document class looks little bit complex than the code using PDF generator. But generator code does not work properly either. ( try the code with attached image, it crops the image on right), however Document class does work.
Can you please explain what are these different methods of Operator class does? is there documenation on this class?
Hi Laxmikant,
Pls see the attached image
Hi Laxmikant;
Thanks for sharing the template image file with us.
You can set the dimensions of image using Aspose.Pdf.Generator namespace as mentioned below. Kindly use the below code and check if it works fine for you.
[C#]
image1.ImageInfo.FixWidth = 100;
image1.ImageInfo.FixHeight = 100;
Please feel free to contact support in case you need any further assistance.
Thanks & Regards,
Thanks
float margin = 20;
//Instantiate a Pdf object by calling its empty constructor
Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();
pdf1.PageSetup.Margin.Top = margin;
pdf1.PageSetup.Margin.Left = margin;
pdf1.PageSetup.Margin.Right = margin;
pdf1.PageSetup.Margin.Bottom = margin;<span style="color:green;">//Create a section in the Pdf object</span> Aspose.Pdf.Generator.<span style="color:#2b91af;">Section</span> sec1 = pdf1.Sections.Add(); <span style="color:green;">//Create an image object in the section</span> Aspose.Pdf.Generator.<span style="color:#2b91af;">Image</span> image1 = <span style="color:blue;">new</span> Aspose.Pdf.Generator.<span style="color:#2b91af;">Image</span>(sec1); <span style="color:green;">//Add image object into the Paragraphs collection of the section</span> sec1.Paragraphs.Add(image1); <span style="color:green;">//Set the path of image file</span> image1.ImageInfo.ImageStream = imageStream; <span style="color:green;">//Set the type of image using ImageFileType enumeration</span> image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.<span style="color:#2b91af;">ImageFileType</span>.Jpeg; <span style="color:green;">//Set image title</span> image1.ImageInfo.Title = <span style="color:#a31515;">"JPEG image"</span>; image1.ImageInfo.FixWidth = pdf1.PageSetup.PageWidth - margin * 2; image1.ImageInfo.FixHeight = pdf1.PageSetup.PageHeight - margin * 2; <span style="color:green;">//Save the Pdf</span> pdf1.Save(<span style="color:blue;">string</span>.Format(<span style="color:#a31515;">"{0}.pdf"</span>, <span style="color:#2b91af;">Guid</span>.NewGuid()));</pre></div>
continuing my question above, i found the way to identify the width & height of the image. I’m using System.Drawing.Image class to identify the size but drawback of this approach is, i have to load the image twice, 1> to find the image size and 2>to convert the image into pdf. It would be really nice if i could find the image size while converting.
continuing my question above, i found the way to identify the width & height of the image. I’m using System.Drawing.Image class to identify the size but drawback of this approach is, i have to load the image twice, 1> to find the image size and 2>to convert the image into pdf. It would be really nice if i could find the image size while converting.
Hi,
I am working over this query and will get back to you soon. We are sorry for this delay and inconvenience.
Laksh:
1> Image class also has two properties ImageHeight and ImageWidth, How do they differ from Image.ImageInfo.FixHeight and Image.ImageInfo.FixWidth?
Hi Laxmikant,
The Image.ImageHeight and Image.ImageWidth properties are now obsolete so its better to use ImageInfo.FixHeight and ImageInfo.FixWidth properties to set the image dimensions inside PDF document.
Laksh:
3> What i would like to do; if image size is smaller than (PageSize - Margin * 2) then load the whole image but if its larger then resize or scale it downt so that it will fit on the page. ( I have not implemented this logic below because when i load image the ImageHeight, ImageWidth, FixHeight and FixWidth properties are all zero)
image1.ImageInfo.FixWidth = pdf1.PageSetup.PageWidth - margin * 2;
image1.ImageInfo.FixHeight = pdf1.PageSetup.PageHeight - margin * 2;
Laksh:
4>Also its adding extra page at the end
Laksh:
continuing my question above, i found the way to identify the width & height of the image. I’m using System.Drawing.Image class to identify the size but drawback of this approach is, i have to load the image twice, 1> to find the image size and 2>to convert the image into pdf. It would be really nice if i could find the image size while converting.Also, is ImageHeight, ImageWidth, FixHeight and FixWidth in Pixel??
Hi Laxmikant,
As I have shared in my earlier post, when using ImageInfo class, you can only get Image dimensions (Height x Width) once the PDF file is saved. Other than System.Drawing.Image class, you may also consider using Bitmap class to get image dimensions. However in order to save yourself from loading the image twice, you may consider loading the source Image in Bitmap object and pass the same object to SystemImage property of ImageInfo class. Please take a look over the following code snippet in which I have tried loading the image into Bitmap object and have used the same object to place inside PDF document. I have also used the approach to set the image dimensions based over page width/height. In case the Image is larger than page size, it will automatically be resized else it will be displayed in its original dimensions.
[C#]
float margin = 20;
//Instantiate a Pdf object by calling its empty constructor
Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf();
pdf1.PageSetup.Margin.Top = margin;
pdf1.PageSetup.Margin.Left = margin;
pdf1.PageSetup.Margin.Right = margin;
pdf1.PageSetup.Margin.Bottom = 5;
//Create a section in the Pdf object
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();
//Create an image object in the section
Aspose.Pdf.Generator.Image image1 = new Aspose.Pdf.Generator.Image(sec1);
//Add image object into the Paragraphs collection of the section
sec1.Paragraphs.Add(image1);
//Set the type of image using ImageFileType enumeration
image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Jpeg;
//Set image title
image1.ImageInfo.Title = "JPEG image";
// Create a BitMap object in order to get the information of image file
Bitmap myimage = new Bitmap("d:/pdftest/Penguins.jpg");
// check if the width of the image file is greater than Page width or not
if (myimage.Width > sec1.PageInfo.PageWidth)
// set image width equal to page width
image1.ImageInfo.FixWidth = sec1.PageInfo.PageWidth;
else if (myimage.Height > sec1.PageInfo.PageHeight)
// set Image height equal to page height
image1.ImageInfo.FixHeight= sec1.PageInfo.PageHeight;
// set bitmap as image to be placed in PDF file
image1.ImageInfo.SystemImage = myimage;
//Save the Pdf
pdf1.Save("d:/pdftest/Image_to_PDf.pdf");
In case you face any problem or you have any further query, please feel free to contact. We are sorry for your inconvenience.