"Can not to be linearized"

Hi

This program code

new License().SetLicense(“Aspose.Total.lic”);

FileStream stream = new FileStream(“C:\Temp\Output.pdf”, FileMode.OpenOrCreate);

Pdf pdfDocument = new Pdf();

Aspose.Pdf.Generator.Pdf pdf1 = new Aspose.Pdf.Generator.Pdf(stream);
foreach (string file in Directory.GetFiles(“C:\Temp\PNG”))
{
Aspose.Pdf.Generator.Section sec1 = pdf1.Sections.Add();
Aspose.Pdf.Generator.Image image1 = new Aspose.Pdf.Generator.Image(sec1);
sec1.Paragraphs.Add(image1);
image1.ImageInfo.File = file;
image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Png;
}
pdf1.Linearized = true;
pdf1.Close();

Generates this exception:

An unhandled exception of type ‘System.Exception’ occurred in Aspose.Pdf.dll
The number of pages less than 3 , document can not to be linearized !

Why?!

The number of pages are 358, Aspose.Pdf is Version 10.7.0.0



Regards

Christian

Hi Christian,


Thanks for your inquiry. We will appreciate it if you please share your source images here, so we will look into it and guide you accordingly.

We are sorry for the inconvenience caused.

Best Regards,

It does not depend on the Input File!

But, I’ve created one for you anyway.
Just try to run the code in a loop with at least 4 iterations
with the attached PNG.

Regards

Christian

Hi Christian,


Thanks for your inquiry. You are using old generator for linearization(optimization), please use new DOM approach for the purpose, it will help you to accomplish the task without any issue.

Moreover, please note we are not fixing any issues or enhancement in old generator, as it will be obsolete in near future. So it is recommended to use new generator, it is more efficient and improved. It can be used for both creating a PDF document from scratch or manipulating existing PDF document.

FileStream stream = new
FileStream(“C:\Temp\images_Output.pdf”,
FileMode.OpenOrCreate);<o:p></o:p>

Pdf pdfDocument = new Pdf();

Aspose.Pdf.Document pdf1 = new Aspose.Pdf.Document();

foreach (string file in Directory.GetFiles("C:\\Temp\\images"))

{

Aspose.Pdf.Page sec1 = pdf1.Pages.Add();

Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();

sec1.Paragraphs.Add(image1);

image1.File = file;

}

pdf1.OptimizeSize=true;

// Optimzie the file size by removing unused objects

pdf1.OptimizeResources(new Document.OptimizationOptions()

{

LinkDuplcateStreams = true,

RemoveUnusedObjects = true,

RemoveUnusedStreams = true,

CompressImages = true,

ImageQuality = 10

});

pdf1.Save(stream);

Please feel free to contact us for any further assistance.


Best Regards,

How many ways are there to accomplish this task?!

System.NullReferenceException on Pages.Add() Method

- The output from the code you gave me is not linearized!
- The size of the PDF is just too big: 100 * 41 K = 15 MB, but should be 4 MB!!
- The Image is not fullsize on the PDF
- Additional it must be PDF/A-1b


Regards

Christian

Hi Christian,


Thanks for your feedback. It is recommended to use OptimzeResources() object for the purpose.

Please check following code snippet, hopefully it will help you to accomplish the task. We have rendered PDF structure using ProccessParagraphs() before optimizing the PDF document. PDF structure is rendered using either Save() method or ProcessParagraphs(). While converting image to PDF, you can either set page size according to image size (width/height) or image size as per page size. Moreover, you can convert document to PDFA using convert() method as following.

Please note now size of resultant PDFA with 100 images is 233KB, If you reduce image quality then size will be further decreased accordingly.


Aspose.Pdf.Document pdf1 = new Aspose.Pdf.Document();

foreach (string file in Directory.GetFiles("C:\\Temp\\images"))

{

Aspose.Pdf.Page sec1 = pdf1.Pages.Add();

// Set margins so image will fit, etc.

sec1.PageInfo.Margin.Bottom = 0;

sec1.PageInfo.Margin.Top = 0;

sec1.PageInfo.Margin.Left = 0;

sec1.PageInfo.Margin.Right = 0;

Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();

// to fit image as per page size

image1.FixHeight = sec1.PageInfo.Height;

image1.FixWidth = sec1.PageInfo.Width;

sec1.Paragraphs.Add(image1);

image1.File = file;

}

// render PDF document structure

pdf1.ProcessParagraphs();

//optimize PDF file

pdf1.OptimizeSize = true;

pdf1.OptimizeResources(new Document.OptimizationOptions()

{

AllowReusePageContent=true,

LinkDuplcateStreams = true,

RemoveUnusedObjects = true,

RemoveUnusedStreams = true,

CompressImages = true,

ImageQuality = 80

});

//PDFA conversion

pdf1.Convert(new MemoryStream(), PdfFormat.PDF_A_1B, ConvertErrorAction.Delete);

pdf1.Save("E:/data/images_Output.pdf");

Please feel free to contact us for any further assistance.


Best Regards,

Hi

This code does:

- not produce linearized pdf
- only fit the first page
- consume about 1.5GB of memory, which probably could generate a OutOfMemoryException

Regards

Christian

Hi Christian,

Thanks for your feedback.

chstn:

This code does:

- not produce linearized pdf


Please use Optimize() to linearize the PDF as follwoing.

....

//optimize PDF file

pdf1.Optimize();

pdf1.OptimizeResources(new Document.OptimizationOptions()

....

....


chstn:


This code does:

- only fit the first page

I have noticed that only first page is honoring page margin and image width/height, so I have logged a ticket PDFNEWNET-39456 for further investigation and resolution. We will notify you as soon as it is resolved.

chstn:

This code does:

- consume about 1.5GB of memory, which probably could generate a OutOfMemoryException


Please elaborate how you are calculating memory usage. I have checked memory consumption in Task Manager and it remains 24-400 MB.

Best Regards,