Migrating code from Aspose.Pdf.Generator to new Document methods

Hi

I recently upgraded the PDF library from 11.3 to 17.6 and noticed that Aspose.Pdf.Generator no longer exists and could use some help migrating the following that simply merges a load of single page images into a single PDF. Just a basic search for something like CompressionLevel had me stuck finding the new equivalent?

Aspose.Pdf.Generator.Pdf pdf = new Aspose.Pdf.Generator.Pdf();
pdf.CompressionLevel = 9;

Aspose.Pdf.Generator.MarginInfo marginInfo = new Aspose.Pdf.Generator.MarginInfo();
marginInfo.Top = 0;
marginInfo.Bottom = 20;
marginInfo.Left = 0;
marginInfo.Right = 0;

Aspose.Pdf.Generator.Section sec1 = pdf.Sections.Add();
sec1.PageInfo.Margin = marginInfo;
sec1.PageInfo.PageWidth = Aspose.Pdf.PageSize.A4.Width;
sec1.PageInfo.PageHeight = Aspose.Pdf.PageSize.A4.Height;

foreach (var image in images)
{
	Aspose.Pdf.Generator.Image pageImage = new Aspose.Pdf.Generator.Image(sec1);

	sec1.Paragraphs.Add(pageImage);

	var tempfile = Path.GetTempFileName();

	using (FileStream tempFs = File.OpenWrite(tempfile))
	{
		using (var imageStream = new FileStream(image))
		{
			imageStream.CopyTo(tempFs);
		}
	}
	pageImage.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Png;
	pageImage.ImageInfo.Alignment = Aspose.Pdf.Generator.AlignmentType.Center;
	pageImage.ImageInfo.File = tempfile;
}

Any help appreciated as I hadn’t expected to do a load of re-coding when we updated the libraries.

Thanks

Si

Hi,

Thank you for contacting support. The old legacy Aspose.Pdf.Generator approach has been obsoleted from the Aspose.Pdf for .NET API. The latest version of the API follows DOM (Document Object Model) approach, please refer to following helping links to work with DOM model:

  1. Introduction to DOM API
  2. Working with Aspose.Pdf
  3. Convert an Image to PDF

Please let us know in case any further assistance or questions.

Thanks & Regards,
Imran Rafique

Looking through the documentation I can find no equivalent for the CompressionLevel setting that previously existed in the Generator namespace?

Hi Simon,

Thank you for the inquiry. You can compress an existing PDF file using OptimizeResources() method of Document class available under Aspose.Pdf namespace. This method removes, the resources which are not used in a document page, similar resources are joined into a single object and unused objects are removed. Please let us know in case of any further assistance or questions.

Thanks & Regards,
Imran Rafique

Hey, we have the same issue as Simon.

What is the equivalent to “Section” in the DOM approach? Our solution is built around sections.

Example:

Section pdfSection = pdf.Sections.Add();
pdfSection.PageInfo.PageWidth = PageSize.A4Width;
pdfSection.PageInfo.PageHeight = PageSize.A4Height;
pdfSection.PageInfo.Margin = new MarginInfo { Top = 10f, Bottom = 10f, Left = 40f, Right = 40f };
pdfSection.TextInfo.FontName = FontName;
pdfSection.TextInfo.FontSize = FontSizeText;

Br

Hi Grevling,

If it helps this is what my rewrite looked like with the new API:

        var pdf = new Aspose.Pdf.Document();

        foreach (var image in images)
        {
            var tempfile = Path.GetTempFileName();

            tempFiles.Add(tempfile);

			using (FileStream tempFs = File.OpenWrite(tempfile))
			{
				using (var imageStream = new FileStream(image))
				{
					imageStream.CopyTo(tempFs);
				}
			}

            var page = pdf.Pages.Add();

            // Set margins so image will fit, etc.
            page.PageInfo.Margin.Bottom = 20;
            page.PageInfo.Margin.Top = 0;
            page.PageInfo.Margin.Left = 0;
            page.PageInfo.Margin.Right = 0;

            Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
            image1.File = tempfile;
            page.Paragraphs.Add(image1);

            // Need to do this so the optimize works later
            pdf.ProcessParagraphs();
        }

        pdf.OptimizeResources(new Aspose.Pdf.Document.OptimizationOptions()
        {
            RemovePrivateInfo = true,
            AllowReusePageContent = true,
            LinkDuplcateStreams = true,
            RemoveUnusedObjects = true,
            RemoveUnusedStreams = true,
            CompressImages = true,
            ImageQuality = 50,
        });
        pdf.Optimize();

Cheers

Si

@Grevling,

There is no Section element in the new DOM (Document Object Model) approach, as the structure of a PDF file is hierarchical, Aspose.Pdf for .NET API also accesses the elements in the same way. We can use a Page instance, instead of the Section instance. In order to understand the new DOM approach, please go through this help topic: Introduction to the DOM API. This is the complete developers guide of the new DOM approach: Working with Aspose.Pdf

Thanks for your replies @imran.rafique and @simon.fairey. For now we’ll stick with upgrading to 17.3 and put the refactor in the backlog.

@Grevling,

Sure, please use the version 17.3 as per your convenience and let us know regarding any further assistance.

Note: we recommend our clients always use the latest version of the Aspose.Pdf for .NET API. We have released the latest version 17.11 whereas 17.3 is an old version.