Covert images into pdf

Thanks Alexy,

We have a method to covert images into pdf. I have updated to the code according to Aspose.PDF version 24.1. Providing old code and modified code below. Please let me know if we can optimize the code further in any other easiest way as you did in the above case.

Code with Aspose.Pdf version 7.3

private static Stream ConvertImageToPdf(string fileName)
{
    Aspose.Pdf.Generator.ImageInfo imageInfo = new Aspose.Pdf.Generator.ImageInfo();
    // Aspose.Pdf.ImageInfo imageInfo = new Aspose.Pdf.ImageInfo();
    switch (Path.GetExtension(fileName).ToLower())
    {
        case ".jpg":
        case ".jpeg":
            imageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Jpeg;
            break;
        case ".bmp":
            imageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Bmp;
            break;
        case ".png":
            imageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Png;
            break;
        case ".tiff":
        case ".tif":
            imageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Tiff;
            break;
        default:
            imageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Unknown;
            break;
    }

    Stream pdfOutPutStream = null;
    try
    {
        pdfOutPutStream = PDFUtilities.CreateOutPDFStream(fileName);
        imageInfo.File = fileName;

        Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
        Aspose.Pdf.Generator.Section section = pdf.Sections.Add();

        Aspose.Pdf.Generator.Image image = new Aspose.Pdf.Generator.Image(section);

        image.ImageInfo = imageInfo;
        section.Paragraphs.Add(image);
        pdf.Save(pdfOutPutStream);
    }

Modified code with Aspose.PDF version 24.1

private static Stream ConvertImageToPdf(string fileName)
{
    Stream pdfOutPutStream = null;
    try
    {
        pdfOutPutStream = PDFUtilities.CreateOutPDFStream(fileName);
        Aspose.Pdf.Document pdf = new Aspose.Pdf.Document();
        Page section = pdf.Pages.Add();
        Image image = new Image();
        image.File = fileName;
        section.Paragraphs.Add(image);
        pdf.Save(pdfOutPutStream);
    }

@Febinbabu Since your question is related to Aspose.PDF, I have moved it into the appropriate forum. My colleagues from Aspose.PDF team will help you shortly.

Hi Team,

Need one more clarification.

In Aspose.PDF version 7.3, both Aspose.Pdf.Generate and Aspose.Pdf.Facade cantatins PageSize property.

Aspose.Pdf.Facades.PageSize
Aspose.Pdf.Generator.PageSize

After upgrading to verion 23.12 , PageSize is not there in the above 2 places, instead it is moved to Aspose.Pdf.PageSize.

  1. In our existing code, both Aspose.Pdf.Facades.PageSize and Aspose.Pdf.Generator.PageSize in many places.Can we replace both with Aspose.Pdf.PageSize?

  2. In PdfPageEditor, a property named pages were available in the aspose,pdf version 7.3. It is not available in 23.12 version. How can we re write the code according to Aspose.Pdf vrsion 23.12.

Providing the existing code with aspose.pdf version 7.3 is given below

public ActionStatus Resize(int[] pages, Aspose.Pdf.Facades.PageSize newPageSize)
{
using (var pdfInfo = new PdfFileInfo(this._fileName))
{
foreach (int page in pages)
{
if (page < 1 || page > pdfInfo.NumberOfPages)
{
return new ActionStatus(ErrorCodes.Error,
new InvalidOperationException(string.Format(“page’s value must be between 1 and {0}”,
pdfInfo.NumberOfPages)));
}
}
}
string tempFile = string.Empty;
using (var p = new PdfPageEditor())
{
try
{
tempFile = this._fileName.Insert(this._fileName.LastIndexOf(“.”), “_temp”);
if (FileUtility.FileInUse(_fileName))
return new ActionStatus(ErrorCodes.Error,
new InvalidOperationException(string.Format(“File ‘{0}’ is in use by another process”,
_fileName)));
File.Copy(this._fileName, tempFile);
//p = new PdfPageEditor();
p.BindPdf(tempFile);
p.PageSize = newPageSize;
p.Pages = pages;
p.Save(this._fileName);
}
catch (Exception ex)
{
return new ActionStatus(ErrorCodes.Error, ex);
}
finally
{
//p = null;
File.Delete(tempFile);
}
}
return new ActionStatus(ErrorCodes.NoError);
}

Hi Team,

Could you please provide me an update on the queries posted.

@Febinbabu

We are checking the case at the moment and will be writing you back shortly.

@Febinbabu

We have gone through the inquiries in this forum thread. Yes, the code snippet to convert an image into PDF is correct and optimized. You can keep using it in your project using the latest version of the API.

Furthermore, yes, you can use Aspose.Pdf.PageSize in the latest version of the API. About pages, all the PDF pages can be now accessed at Document level as new Aspose.Pdf API implements DOM approach. Below is a sample code example to access PDF pages and its different properties:

Document doc = new Document("Input.pdf");
foreach (Page page in doc.Pages)
{
 // Access page number and other properties
 var number = page.Number;
}

Please also check Working with Pages section in the API documentation and feel free to let us know in case you need further assistance.

Hi Asad,
Please clarify my below queries.

1. We were using below code with Aspose.Pdf version 7.3. After upgrading to Aspose 23.12, Aspose.Pdf.PageSize is a sealed class and can’t be inherited.
How to implement the same with Aspose.PDF.23.12?
public class PageSize : Aspose.Pdf.Generator.PageSize
{
public float Height { get; set; }
public virtual bool IsLandscape { get; set; }
public float Width { get; set; }

        public static readonly PageSize LEGAL_Landscape = new PageSize() { Width = Aspose.Pdf.Generator.PageSize.LegalWidth, Height = Aspose.Pdf.Generator.PageSize.LegalHeight, IsLandscape = true };
        public static readonly PageSize LETTER_Landscape = new PageSize() { Width = Aspose.Pdf.Generator.PageSize.LetterWidth, Height = Aspose.Pdf.Generator.PageSize.LetterHeight, IsLandscape = true };

        public PageSize() : base() { }

    }
  1. Below code is working fine with Aspose.PDF version 7.3. How do we re write the code with Aspose.PDF version 23.12?

Aspose.Pdf.Generator.TextInfo tinfo = new Aspose.Pdf.Generator.TextInfo { FontSize = 8, FontName = “Tahoma” };

Are these both properties same?

PdfPageEditor.Pages in Apose.Pdf 7.3 and PdfPageEditor.ProcessPages in Aspose.PDF 23.1

@Febinbabu

You would need to change your approach here. Instead of using your own extensions, you can use PageSize class directly as it contains all page sizes. Furthermore, you can set IsLandscape property on page level by Page.PageInfo.IsLandscape.

These settings can be accessed through TextState Class of TextFragment. e.g.

// Create text fragment
TextFragment textFragment1 = new TextFragment("Paragraph Text");

// Create text fragment
textFragment1.TextState.FontSize = 12;
textFragment1.TextState.Font = FontRepository.FindFont("TimesNewRoman");
textFragment1.TextState.BackgroundColor = Aspose.Pdf.Color.LightGray;
textFragment1.TextState.ForegroundColor = Aspose.Pdf.Color.Blue;

We recommend that you use the latest (24.1) version of the API if you have already planned to upgrade. Furthermore, above method in the latest version is used to specify which pages should be processed by PdfPageEditor. If you do not specify them, all pages will be processed by default.