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