How to convert Images to PDF

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.

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.

Can you be more specific and provide some examples?

You could try something I did in C#:


//Assuming you have some section…
Section Chapter

//Set the image to your image file
System.Drawing.Image img =

//Create a temporary memorystream
MemoryStream ms = new MemoryStream();

//Save the image into the memorystream
imageIn.Save(ms,img.RawFormat);

//Create a Generator Image
Aspose.Pdf.Generator.Image image1 = new Aspose.Pdf.Generator.Image(Chapter);

//Add the image to the chapter
Chapter.Paragraphs.Add(image1);

//Need some way to tell Aspose what kind of file you are dealing with, I wrote my own function for this
image1.ImageInfo.ImageFileType = determineExtension(img.RawFormat);

//Give Aspose’s image the memory stream of the image
image1.ImageInfo.ImageStream = ms;

Hope that helps.

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 file
System.Drawing.Image img =

//Create a temporary memorystream
MemoryStream ms = new MemoryStream();

//Save the image into the memorystream
imageIn.Save(ms,img.RawFormat);

//Create a Generator Image
Aspose.Pdf.Generator.Image image1 = new Aspose.Pdf.Generator.Image(Chapter);

//Add the image to the chapter
Chapter.Paragraphs.Add(image1);

//Need some way to tell Aspose what kind of file you are dealing with, I wrote my own function for this
image1.ImageInfo.ImageFileType = determineExtension(img.RawFormat);

//Give Aspose's image the memory stream of the image
image1.ImageInfo.ImageStream = ms;

Hope that helps.

Hi Philip,

Thanks for sharing the code snippet. We really appreciate your cooperation in this regard.

Laksh:
Well i thought PDF
Aspose.Pdf.Generator is old and obsolete and we have to use new Document class?
Hi Laxmikant,

Please note that since the release of Aspose.Pdf for .NET 6.0.0, the Legacy Aspose.Pdf.Kit for .NET has been merged into Aspose.Pdf for .NET and all its feature are available under Aspose.Pdf.Facades namespace whereas all the Classes and enumerations of legacy Aspose.Pdf for .NET are now appearing under Aspose.Pdf.Generator namespace. The same code has been migrated/merged into this new product along with the introduction of new Document Object Model of Document class.

Please note that the code written in legacy products is still workable in new MergedAPI. Pleas visit the following link for further details on


In case of any further query, please feel free to contact.

Thanks for quick reply and i really appreciate that.

So is there anyway to convert image into pdf using new Document class, i would like to use new API over old API hoping there will be new functionality in new Document class.

If not then i’ll use generator code as you mentioned.

Thanks

Hi Laxmikant,


Please try using the following code snippet to create PDF document from scratch using Document class present in new API. In the following code snippet I have first created the Document object, then add a blank page to the document structure, added the image to images collection of PDF page and then placed the image of respective location over the page. For your reference, I have also attached the sample PDF document that I have generated with new API approach.

[C#]

//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,


I am afraid I am unable to find any attachment with your last post. Can you please again try attaching the image file so that we can test the scenario. We are sorry for your inconvenience.

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

using the generator code im trying to convert image into PDF so that it will appear on the whole page. ( minus the margin).

1> Image class also has two properties ImageHeight and ImageWidth, How do they differ from Image.ImageInfo.FixHeight and Image.ImageInfo.FixWidth?

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)

4>Also its adding extra page at the end

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.


Also, is ImageHeight, ImageWidth, FixHeight and FixWidth in Pixel??

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)
If you implement the following logic to place the image inside Section object, where we have already specified the MarginInfo for Section element, the image will automatically be resized to fit inside the PDF page (either its Smaller or Larger than page dimensions).
The reason you are getting zero against ImageInfo.FixHeight/FixWidth, is because image is loaded when Pdf document is being saved and I am afraid you might not be able to get image dimensions before saving the PDF file. However if you use the same properties after saving the PDF file, you will get the required information.

image1.ImageInfo.FixWidth = pdf1.PageSetup.PageWidth - margin * 2;
image1.ImageInfo.FixHeight = pdf1.PageSetup.PageHeight - margin * 2;

The above code will set the Image Height equal to Pdf page width minus Margin*2 and Image Height will be set to Page Height - Margin * 2.
Laksh:
4>Also its adding extra page at the end
The reason you are getting an extra page is because the Image title "JPEG image" is being displayed on consequent page and its appearing on next page is because the Image height is equal to Page height - margin and you have also specified the page bottom margin as 20, so no space is left to place Image title on first page. In order to resolve this problem, please set the page bottom margin as 5. Please take a look over the attached PDF document that I have generated using the code snippet that you have shared earlier and I have only changed the page bottom margin value to 5. I have used Aspose.Pdf for .NET 7.0.0 to test this scenario.

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.