Sometimes we receive problematic PDFs that require rebuilding before we annotate them in our application. The root cause is generally that the PDF was created in some anomalous way that plays havoc with image rotation and the like. The quickest and easiest fix, within Acrobat, is to either use the Print to PDF option, and import the new PDF or, in extreme cases, export the PDF to an image and then create a new PDF from those images.
When doing this in Acrobat, the file sizes increase somewhat for certain PDFs, mostly ones that are scanned images of documents. However, when doing the same operation (PDF to TIFF to PDF) with Syncfusion, the file sizes dramatically increase in size, sometimes by a factor of 6 or 10.
The only direction I can find in the documentation is to reduce the image quality of the source TIFF files, but this degradation of quality is not acceptable. Is there some other option that I am not seeing that we can use to rebuild these PDFs while maintaining some semblance of the original PDF file size?
This is the code that we are using to export the PDF and then build a new one:
public List<string> ExportPDFToTiff(string pathToPDF)
{
List<string> tiffPaths = new List<string>();
var tiffCreationThread = new Thread(() =>
{
var pdfViewerControl = new PdfViewerControl();
try
{
//Load the input PDF file
using (var loadedDocument = new PdfLoadedDocument(pathToPDF))
{
try
{
pdfViewerControl.Load(loadedDocument);
//Export all the pages as images
BitmapSource[] images = pdfViewerControl.ExportAsImage(0, loadedDocument.Pages.Count - 1);
//Set up the output path
string outputDirectory = Path.Combine(Path.GetTempPath(), "PdfConversion");
Directory.CreateDirectory(outputDirectory);
if (images != null)
{
for (int i = 0; i < images.Length; i++)
{
//Initialize the new Tiff bitmap encoder
var encoder = new TiffBitmapEncoder();
//Create the bitmap frame using the bitmap source and add it to the encoder
encoder.Frames.Add(BitmapFrame.Create(images[i]));
//Create the file stream for the output in the desired image format
string tiffPath = Path.Combine(outputDirectory, i.ToString() + ".tiff");
tiffPaths.Add(tiffPath);
using (FileStream stream = new FileStream(tiffPath, FileMode.Create))
{
//Save the stream, so that the image will be generated in the output location
encoder.Save(stream);
}
}
}
pdfViewerControl.Unload(true);
}
catch (Exception ex)
{
Debug.Write("Error: " + ex.Message);
}
}
}
finally
{
pdfViewerControl = null;
}
});
tiffCreationThread.SetApartmentState(ApartmentState.STA);
tiffCreationThread.Start();
tiffCreationThread.Join();
return tiffPaths;
}
public void CreatePDFFromTiffs(List<string> pathsToTiffs, PdfDocument newPdfDoc)
{
Debug.WriteLine("Begin Create PDF from Tiffs...");
var currentPage = 1;
foreach (var tiffPath in pathsToTiffs)
{
var image = new PdfBitmap(tiffPath);
// Create a new section and a new page with the dimensions of the TIFF image
PdfSection section = newPdfDoc.Sections.Add();
section.PageSettings.Size = new SizeF(image.Width, image.Height);
section.PageSettings.Margins.All = 0;
if (image.Width > image.Height)
{
section.PageSettings.Orientation = PdfPageOrientation.Landscape;
}
else
{
section.PageSettings.Orientation = PdfPageOrientation.Portrait;
}
PdfPage page = section.Pages.Add();
Debug.WriteLine("Added page " + currentPage);
currentPage++;
// Draw the image on the PDF page
page.Graphics.DrawImage(image, 0, 0, image.Width, image.Height);
}
}
We were able to reproduce the reported issue with the provided details on our end, and currently we are analyzing it. We will provide further details on May 16th, 2024.
For converting images to PDF, we do not compromise image quality. However, the file size may increase. The PDF document size is dependent on the image used. So, make sure for converting PDF to image have less/optimized size.
As we said before, we are not controlling the PDF size because of the size is dependent on the images which is used. In the provided sample, there are five images used and its size is 1.6 MB, so that the resulted document having that much of size.
We can reduce the output PDF file size by controlling the quality of the image during the image to PDF conversion using the following code, but it compromises the image quality in the resulted PDF file. You can refer to the following API documentation for further details.
We have confirmed the same using Adobe Acrobat Pro and attached the document for your reference. So, we could not proceed further on this from our end.