Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Nov 20, 2023 7:39 am

i have a stream contain PDF document and i want to compress it before save it to file. I'm using the code attached below. But after save the document to file and open it, it only contain 1 empty page. Does this method require the file already existed in the disk? Because my code create a PDF document and save it to a MemoryStream before save to file. So i want to Compress it when it still in the MemoryStream. Thanks for your support.
Code: Select all
        void CompressPDF(Stream pdfStream)
        {
            var compressor = new PdfCompressor(pdfStream);

            var textCompression = compressor.Options.TextCompressionOptions;
            textCompression.CompressFonts = true;
            textCompression.UnembedFonts = true;

            var imageCompression = compressor.Options.ImageCompressionOptions;
            imageCompression.ImageQuality = ImageQuality.High;
            imageCompression.ResizeImages = true;
            imageCompression.CompressImage = true;

            compressor.CompressToStream(pdfStream);
        }

PhamNguyen
 
Posts: 12
Joined: Tue Oct 24, 2023 9:03 am

Mon Nov 20, 2023 9:27 am

Hi,

Thank you for your inquiry.
Here I have attached my code for your reference:
Code: Select all
            // Open the PDF file for reading
            FileStream pdfStream = File.OpenRead(@"test.pdf");
           
            // Create a new PdfDocument object
            PdfDocument doc = new PdfDocument();
           
            // Load the PDF document from the stream
            doc.LoadFromStream(pdfStream);

            // Create a new memory stream to store the compressed PDF data
            var outputStream = new MemoryStream();

            using (FileStream fileStream = new FileStream(@"result.pdf", FileMode.Create))
            {
                // Create a new PdfCompressor object with the PDF stream
                var compressor = new PdfCompressor(pdfStream);

                // Get the text compression options for the compressor
                var textCompression = compressor.Options.TextCompressionOptions;

                // Compress the fonts in the PDF document
                textCompression.CompressFonts = true;

                // Unembed the fonts in the PDF document
                textCompression.UnembedFonts = true;

                // Get the image compression options for the compressor
                var imageCompression = compressor.Options.ImageCompressionOptions;

                // Set the image quality to high
                imageCompression.ImageQuality = ImageQuality.High;

                // Resize images in the PDF document
                imageCompression.ResizeImages = true;

                // Compress images in the PDF document
                imageCompression.CompressImage = true;

                // Compress the PDF document to the memory stream
                compressor.CompressToStream(outputStream);

                // Get the byte array of the compressed PDF data
                byte[] fileBytes = outputStream.ToArray();

                // Write the compressed PDF data to the file stream
                fileStream.Write(fileBytes, 0, fileBytes.Length);
               
                fileStream.Flush();
                fileStream.Close();
            }

If the issue still exist, please offer your pdf file to help us do further investigation, you can attach here or send it to us via email (support@e-iceblue.com). Thank you in advance.

Sincerely,
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Mon Nov 20, 2023 9:47 am

So the PdfCompressor constructor only can take input parameter as FileStream, which mean the Pdf document must be saved to file on drive right?

PhamNguyen
 
Posts: 12
Joined: Tue Oct 24, 2023 9:03 am

Tue Nov 21, 2023 5:58 am

Hi,

Thank you for your feedback.
There are two choices for input parameters of the PdfCompressor constructor:
1) Specify a path (string path = @"C:\\Libraries\\demo";)
2) Stream
Similarly, Pdf document can also be saved in two ways:
1) Save to a file on the drive (write to disk)
2) Save to a stream (save to stream is within the stream, you can also upload or save to disk with the stream)
If you have any issue just feel free to contact us.

Sincerely,
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Return to Spire.PDF