Using the Generator api or the pdf document api we get a double in file size for the same output.
It uses one singe image no text and is 58KB in one file and 109 KB in the other …
Why the difference?
One that doubles in size:
private static void ImgTest2()
{
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
Page pg= doc.Pages.Add();
Aspose.Pdf.FloatingBox fb2 = new Aspose.Pdf.FloatingBox();
fb2.Left = 50F;
Aspose.Pdf.Image image2 = new Aspose.Pdf.Image();
image2.File = @“C:\temp\Sample1.jpg”;
image2.ImageScale = .5f;
fb2.Paragraphs.Add(image2);
pg.Paragraphs.Add(fb2);
foreach (Page page in doc.Pages)
{
int idx = 0;
foreach (Aspose.Pdf.XImage image in page.Resources.Images)
{
using (var imageStream = new MemoryStream())
{
image.Save(imageStream, System.Drawing.Imaging.ImageFormat.Png);
imageStream.Seek(0, SeekOrigin.Begin);
page.Resources.Images.Replace(idx, imageStream);
}
idx = idx + 1;
}
}
// optimzie the file size by removing unused objects
doc.OptimizeResources(new Document.OptimizationOptions()
{
LinkDuplcateStreams = true,
RemoveUnusedObjects = true,
RemoveUnusedStreams = true
});
doc.Save(@“C:\temp\images2.pdf”);
}
The 58KB output version
private static void ImgTest()
{
Aspose.Pdf.Generator.Pdf pdf = new Pdf();
Section section = pdf.Sections.Add();
section.PageInfo.PageWidth = Aspose.Pdf.Generator.PageSize.LetterWidth;
section.PageInfo.PageHeight = Aspose.Pdf.Generator.PageSize.LetterHeight;
Aspose.Pdf.Generator.MarginInfo margin = new Aspose.Pdf.Generator.MarginInfo();
margin.Left = 40;
margin.Right = 5f;
margin.Top = 110f;
section.PageInfo.Margin = margin;
Aspose.Pdf.Generator.FloatingBox fb2 = new Aspose.Pdf.Generator.FloatingBox();
fb2.Left = 50F;
//fb2.Top = 5F;
//fb2.BoxHorizontalPositioning = BoxHorizontalPositioningType.Margin;
//fb2.BoxVerticalPositioning = BoxVerticalPositioningType.Margin;
Aspose.Pdf.Generator.Image image2 = new Aspose.Pdf.Generator.Image();
image2.ImageInfo.File = @“C:\temp\Sample.jpg”;
image2.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Jpeg;
image2.ImageScale = .5f;
fb2.Paragraphs.Add(image2);
section.Paragraphs.Add(fb2);
pdf.Save(@“C:\temp\images.pdf”);
}
Hi Jim,
Thanks for contacting support.
I have tested the scenario and have observed that the output generated with Aspose.Pdf.Document class is greater in size as compare to output generated with Aspose.Pdf.Generator.Pdf class. For the sake of correction, I have logged this problem
as PDFNEWNET-36944 in our issue tracking system. We will further
look into the details of this problem and will keep you updated on the status
of correction. Please be patient and spare us little time. We are sorry for
this inconvenience.
Hi Jim,
In addition to Nayyer’s reply, please note that while generating a new PDF document using Aspose.Pdf.Document
(New DOM approach), the user cannot manipulate the page (document resources) before saving the PDF document. The PDF is created dynamically, and in order to get a real PDF document, we should render its programming/dynamic model.
So please save the document into a stream and then reopen it for other processing. Please check the following code; it will help to optimize the document and result in a smaller document size than Aspose.Pdf.Generator
.
MemoryStream ms = new MemoryStream();
Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
Page pg = doc.Pages.Add();
Aspose.Pdf.FloatingBox fb2 = new Aspose.Pdf.FloatingBox();
fb2.Left = 50F;
Aspose.Pdf.Image image2 = new Aspose.Pdf.Image();
image2.File = myDir + "logo_omega-style.jpg";
image2.ImageScale = 0.5f;
fb2.Paragraphs.Add(image2);
pg.Paragraphs.Add(fb2);
doc.Save(ms);
doc = new Aspose.Pdf.Document(ms);
foreach (Page page in doc.Pages)
{
int idx = 1;
foreach (Aspose.Pdf.XImage image in page.Resources.Images)
{
using (var imageStream = new MemoryStream())
{
// To compress images change the image type and resolution
image.Save(imageStream,
ImageFormat.Png, 72);
imageStream.Seek(0, SeekOrigin.Begin);
// Control image quality for better compression
page.Resources.Images.Replace(idx, imageStream, 80);
}
idx = idx + 1;
}
}
doc.OptimizeResources();
doc.Save(myDir + "testimagedom.pdf");
Best Regards,
@jimhelmbrecht
Thanks for your patience.
We are pleased to inform you that earlier reported issue PDFNET-36944, has been resolved in latest version Aspose.Pdf for .NET 17.12. Please try using the latest release version and in case you face any issue, please feel free to contact us.