We are using Aspose.Pdf 17.4.0 for .NET to take a series of JPEG images and place them one per page into a new PDF file. Strangely, using the Aspose.Pdf namespace creates a PDF approximately two or three times size as the obsolete Aspose.Pdf.Generator namespace (depending upon the number of images).
For one sample run, we had 5 JPEG files of sizes 68 KB, 68 KB, 73 KB, 73 KB, 74 KB for a total of 352 KB. The PDF created using Aspose.Pdf.Generator was 358 KB (for about 6 KB of overhead for the PDF which seems reasonable). The PDF created using Aspose.Pdf was 713 KB, nearly double the size!
We have tried things like calling Aspose.Pdf.Document.OptimizeResources using CompressImages = true and changing the ImageQuality parameter, but it does not seem to have any effect.
Below is a minimal sample for reproducing the problem:
using Aspose.Pdf;
using Aspose.Pdf.Generator;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
public string CreatePdf(IEnumerable files /* paths to .jpg files */)
{
var directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), Guid.NewGuid().ToString());
Directory.CreateDirectory(directory);
// Old way
var pdf = new Pdf();
// New way
using (var document = new Document())
{
foreach (var file in files)
{
File.Copy(file, Path.Combine(directory, Path.GetFileName(file)));
// Old way
var section = pdf.Sections.Add();
section.PageInfo.Margin.Bottom = 0;
section.PageInfo.Margin.Left = 0;
section.PageInfo.Margin.Right = 0;
section.PageInfo.Margin.Top = 0;
var image = new Aspose.Pdf.Generator.Image(section);
section.Paragraphs.Add(image);
image.ImageInfo.File = file;
image.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Jpeg;
// New way
var page = document.Pages.Add();
page.PageInfo.Margin.Bottom = 0;
page.PageInfo.Margin.Left = 0;
page.PageInfo.Margin.Right = 0;
page.PageInfo.Margin.Top = 0;
using (var bitmap = new Bitmap(file))
{
page.CropBox = new Aspose.Pdf.Rectangle(0, 0, bitmap.Width, bitmap.Height);
}
page.Paragraphs.Add(new Aspose.Pdf.Image()
{
File = file
});
}
pdf.Save(Path.Combine(directory, "Aspose.Generator.pdf"));
document.Save(Path.Combine(directory, "Aspose.pdf"));
}
// Directory on the desktop will now contain all the JPEG files and both PDFs for comparison.
}