Updating Old Aspose.Pdf with latest 19.9 dll

As per your saying that ‘we do not provide support for older releases of Aspose APIs’.
Now we are updating old Aspose.Pdf with latest 19.9 dll but unable to convert code.

Can you please help me to convert the below code:

public byte[] ConvertImageToPdf(byte[] doc)
{
//Instantiate a Pdf object
MemoryStream ms = new MemoryStream(doc);
MemoryStream msPng = new MemoryStream();
MemoryStream convPdf = new MemoryStream();

        Pdf pdf1 = new Pdf();

        // Create a new section in the Pdf document
        Aspose.Pdf.Generator.Section sec1 = new Aspose.Pdf.Generator.Section(pdf1);

        //Add the section in the sections collection of the Pdf document
        pdf1.Sections.Add(sec1);

        //Create an image object
        Aspose.Pdf.Generator.Image image1 = new Aspose.Pdf.Generator.Image(sec1);

        //Add the image into paragraphs collection of the section
        sec1.Paragraphs.Add(image1);
        image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Png;
        //Set the ImageStream to a MemoryStream object
        image1.ImageInfo.ImageStream = msPng;

        //Set desired the image scale
        image1.ImageScale = 0.5F;
        pdf1.Save(convPdf);

        return convPdf.GetBuffer();
    }

@amanjainmccalla

You may kindly use following DOM based code snippet in order to add image inside PDF.

FileStream mystream = new FileStream(dataDir + "download.png", FileMode.Open);
Document doc = new Document();
Page page = doc.Pages.Add();
// Set margins so image will fit, etc.
page.PageInfo.Margin.Bottom = 10;
page.PageInfo.Margin.Top = 10;
page.PageInfo.Margin.Left = 10;
page.PageInfo.Margin.Right = 10;

// Create an image object
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
// Add the image into paragraphs collection of the section
page.Paragraphs.Add(image1);
// Set the image file stream
image1.ImageStream = mystream;
// Save resultant PDF file
doc.Save(dataDir + "PNG2PDF.pdf");
// Close memoryStream object
mystream.Close();

You may replace FileStream in above code snippet with MemoryStream as per your requirements. In case you need further assistance, please feel free to let us know.

1 Like

Thank you so much Asad ali. It worked. but here is one question that is
you did not updated the below mentioned line
image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Png;

Do we not needed in this version ? Please look into my code.

@amanjainmccalla

You do not need to specify the image type unless it is SVG or Base64. In case of any of these formats, you can specify it as follows:

image1.FileType = ImageFileType.Svg;
1 Like