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.

Wed Nov 01, 2023 7:34 am

I'm working with a function which merge multiple PDF into one. My problem is, the size of final PDF files is too large to save, so it will throw System.OutOfMemoryException at mentioned line below. I have tried using PdfDocument.MergeFiles, but it still thrown that exception. Can you suggest me any other solution to solve this problem?
Input data is 32 Pdf document, each of them have 250 page. So my final Pdf document after merge will have 8000 page, and it's size is approximately 1 GB.
Code: Select all
                    using (var pdfDoc = new PdfDocument())
                    {
                        listTempPDFFileNames.Where(printData => File.Exists(printData.FullPathFile))
                            .ToList()
                            .ForEach(x =>
                            {
                                var tempPdfDoc = new PdfDocument(x.FullPathFile);
                                pdfDoc.AppendPage(tempPdfDoc);
                            });

                        if (pdfDoc.Pages.Count > 0)
                        {
                            pdfDoc.SaveToFile(finalPDF); //This line cause System.OutOfMemoryException
                            pdfDoc.Close();
                        }
                    }
Last edited by PhamNguyen on Wed Nov 01, 2023 9:38 am, edited 1 time in total.

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

Wed Nov 01, 2023 9:32 am

Hi,

Thank you for your inquiry.
Did you use the latest commercial version of Spire.PDF for .Net 9.10.2? If not, please download the latest commercial version and retest through this link:https://www.e-iceblue.com/Download/download-pdf-for-net-now.html
You can refer to this link(https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Document-Operation/How-to-Compress-PDF-Document-in-C-VB.NET.html) to try compressing the PDF document before merging it.If the issue still exists, please offer the following information to help us do further investigation. Thank you in advance.
1)Your input test files,due to your test files are very big, you could upload all files to the DropBox or OneDrive and then share the download link with us.
2)Application type, such as Console App, .NET Framework 4.8
3)Your system information (E.g. Win7, 64 bit, RAM:8GB)

Sincerely
Ula
E-iceblue support team
User avatar

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

Tue Nov 14, 2023 3:48 am

Hi,
I just renew a license for Spire.Office for .NET at 2023-11-11. And i'm using Spire.Office for .NET version 8.10.2. After using compress function you suggest, my file's size is reduced significantly, so basically it meet my needs. But after compress all fonts embed into my file are gone. Is this normal? If it is, is there anyway to both reduced the file size, and keep the font still embed into file?
Thank you and have a good day.
Code: Select all
void CompressPDF(string pdfFilePath)
        {
            var compressor = new PdfCompressor(pdfFilePath);

            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.CompressToFile(pdfFilePath);
        }

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

Tue Nov 14, 2023 7:16 am

Hi,

Thank you for your inquiry.
Firstly, please confirm if you are using the updated license. Secondly, I suggest that you apply your license by license key (https://www.e-iceblue.com/Tutorials/Licensing/Licensing.html#Apply_by_Key), since the other methods would cause the unrecognition of the license file because of the lack of permission or other reasons in some cases. Finally, we have not found your purchase information. Please provide your email address or order number for purchase.
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

Tue Nov 14, 2023 7:29 am

Hi,
Thank you for your support. I have solved the problem with the license. Can you help me look into the problem I mentioned above? I edited it right before you reply =))

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

Tue Nov 14, 2023 8:36 am

Hi,

Thanks for your more inquiry.
I have carefully checked your code and you need to set the attribute of the following line of code to false. If you set it to true, it means to cancel font embedding. If you set it to false, it means you don’t cancel font embedding.
Code: Select all
textCompression.UnembedFonts = false;

If the issue still exists, please offer the following information to help us do further investigation. Thank you in advance.
1)Your input test files, due to your test files are very big, you could upload all files to the DropBox or OneDrive and then share the download link with us.
2)Application type, such as Console App, .NET Framework 4.8
3)Your system information (E.g. Win7, 64 bit, RAM:8GB)

Sincerely,
Ula
E-iceblue support team
User avatar

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

Tue Nov 14, 2023 9:35 am

Hi,
I get your point about set textCompression.UnembedFonts to false, but if i do this, the reduced file size will not be significant. What i want is a way to both reduce the file size significant like when set textCompression.UnembedFonts to true, and keep the font still embedded in the file at the same time. Is there any way Spire.Pdf library can meet my needs?
Thank you for your support, wish you have a nice day.

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

Wed Nov 15, 2023 6:24 am

Hi,

Thank you for your feedback.
Due to the relatively more space occupied by font files, unembedded font files is the key to compressing documents. When you set textCompression.UnembedFonts to true, it cannot keep the font embedded in the file. If you want to reduce the document size when embedding fonts, sorry, this it is very difficult. In addition, you can also try to reduce the file size by setting image quality to medium quality. I put the code below for your reference:
Code: Select all
imageCompression.ImageQuality = ImageQuality.Medium;

Of course, we have another compression method. I will put the code below for you to test.
Code: Select all
            // Create a PdfDocument object and load the PDF file
            PdfDocument doc = new PdfDocument(@"E:\35017\Leet.pdf");

            // Set the IncrementalUpdate property to false, indicating that we want to update the entire document
            doc.FileInfo.IncrementalUpdate = false;

            // Set the CompressionLevel property to Best, which means the PDF will be compressed with the highest level of compression
            doc.CompressionLevel = PdfCompressionLevel.Best;

            // Iterate through each page in the document
            foreach (PdfPageBase page in doc.Pages)
            {
                // Check if the page is not null
                if (page != null)
                {
                    // Check if the page has any images
                    if (page.ImagesInfo != null)
                    {
                        // Iterate through each image on the page
                        foreach (Spire.Pdf.Exporting.PdfImageInfo info in page.ImagesInfo)
                        {
                            // Try to compress the image at the specified index
                            page.TryCompressImage(info.Index);
                        }
                    }
                }
            }

            // Save the modified document to a new file
            doc.SaveToFile(@"E:\\35017\\Leet2.pdf");

            // Close the document
            doc.Close();


Sincerely,
Ula
E-iceblue support team
User avatar

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

Thu Dec 14, 2023 10:03 am

Hi,

I just encounter a new problem. I want to merge 2 PDF files into one final PDF file, each file is approximately 1GB size. Problem is i can't load it into memory using PdfDocument.LoadFromFile(string filePath) function. My system will throw OutOfMemoryException. Can you provide me code example to solve this problem? If you need to know any information to help me, please let me know.

Thanks for support.

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

Fri Dec 15, 2023 5:36 am

Hi,

Thank you for your inquiry.
Did you use the latest version of Spire. Office for .Net 8.11.2?If not, please use the latest version to test again. If the issue still exists, please offer the following information to help us do further investigation. Thank you in advance.
1)Your input test files, due to your test files are very big, you could upload all files to the DropBox or OneDrive and then share the download link with us.
2)Application type, such as Console App, .NET Framework 4.8
3)Your system information (E.g. Win7, 64 bit, RAM:8GB)

Sincerely,
Ula
E-iceblue support team
User avatar

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

Fri Dec 15, 2023 7:24 am

Here is link contain my test data: https://drive.google.com/drive/folders/1SLXofW94Y_OOWCKJAZCTxzG0VuTjpf9v?usp=sharing
My application using .NET framework 4.7.2. System spec is Win 10, 64 bit and 16GB RAM. I'm using the latest version of Spire.Office for .NET (8.11)
My app is merging all of this file into only 1 PDF files, and it running fine until it was 2 file left, each of them is 1BG size.

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

Fri Dec 15, 2023 9:30 am

Hi,

Thank you for your feedback.
In your latest reply, you mentioned that only two pdf files need to be merged, but your download link contains one hundred and twenty-five pdf files. Sorry, I am unable to determine which two pdf files you need to merge. Can you only upload two pdf files to the Google Drive and share the download link with us. 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 Dec 18, 2023 2:47 am

Hi

Sorry for misunderstanding. My requirement is merge all files in folder into only 1 final PDF file. The application runs very well until only the last 2 files remain, then system throw System.OutOfMemoryException. Can you provide me sample code to solve this problem?

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

Mon Dec 18, 2023 10:23 am

Hi,

Thanks for your feedback.
I tested your file and reproduced your issue, I need some time to do more investigation to check if there are solution to solve your issue. Once there are any updates, I'll inform you in time.

Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Tue Dec 19, 2023 9:07 am

Hi,

Thank you for your feedback.
Tested multiple methods to merge multiple pdf files into one pdf file, but all encountered issues. I have recorded your issue in our bug tracking system with the number "SPIREPDF-6453". Our Dev team will investigate and fix it in the future. If this issue is resolved, we will notify you as soon as possible. We deeply apologize for any inconvenience caused.

Sincerely,
Ula
E-iceblue support team
User avatar

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

Return to Spire.PDF

cron