Print Page | Close Window

Library doesn't open large files and damage them.

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: I need help - I can help
Forum Description: Problems and solutions while programming with the Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=3158
Printed Date: 29 Sep 24 at 12:32AM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Library doesn't open large files and damage them.
Posted By: Harier2008
Subject: Library doesn't open large files and damage them.
Date Posted: 11 Aug 15 at 1:33AM
Hello

In my application i need to merge many jpeg files into a one PDF file. After merging i need to embed one PDF to this file. 

My application convert all jpeg files in folder to PDFs and then merge PDFs in one file. Sometimes after merging i've got big files (about 260 - 320 MB) and library don't open this files after merging (the function LoadFromFile() returns 0). After restarting application i choose this file separately and library works fine with it (embed is ok, but only one time. The second time also will be error.).

Also, sometimes library damages my files when i choose files about 320-350 mb and try to embed: I use LoadFromFile() to open original file and AddEmbeddedFile() to embed file.  After embedding i use function .SaveToFile() to save changes, but after this function i've got the blank file (with one blank page) with embedded file. 

Link to some problem files: https://cloud.mail.ru/public/7BXR/whxWogJwv
Link to my test project for this issue: https://cloud.mail.ru/public/4sZj/dzwzr1j7P

Version of OS: Windows 7 x64, but project compiled in x32
Version Quick PDF: 11.14

What could be the reason for this error?
This is a bug of library?

Regards, Dmitry.



Replies:
Posted By: Ingo
Date Posted: 11 Aug 15 at 7:07AM
Hi Dimitry,
 
seems to be more a coding problem than a lib problem?
You should post the relevant code snippets (from try up to finally/end) - so someone here can a have a look on it.
 
Cheers and welcome here,
Ingo
 


-------------
Cheers,
Ingo



Posted By: Harier2008
Date Posted: 11 Aug 15 at 7:48AM
Hello, Ingo.
I posted my test project in VS 2012 (C#): https://cloud.mail.ru/public/4sZj/dzwzr1j7P
Also i can post code snippets: 
This is a code for convert jpeg files to PDF and merge them into a one PDF file: 
public string ConvertFolderFilesToPDF(FileInfo[] files, string pageFolderPath, string filenameprefix = "blockBinder_", bool removeTempFolder = false)
        {
            List<string> allPages = new List<string> { };
            string destPdfsDirectory = Path.Combine(pageFolderPath, "pdfs");
            if (!Directory.Exists(destPdfsDirectory)) Directory.CreateDirectory(destPdfsDirectory);

            for (int i = 0; i <= files.Length - 1; i++)
            {
                string curFile = Path.Combine(pageFolderPath, files.Name.ToString());
                string pageFileName = curFile;

                string PathToSave = Path.Combine(pageFolderPath, files.ToString());
                if (!Path.GetExtension(files.ToString()).Contains("pdf"))
                {
                    // if jpeg then convert it to pdf

                    PathToSave = Path.Combine(destPdfsDirectory, Path.GetFileName(pageFileName) + ".pdf");
                    int docid = qp.NewDocument();
                    float imgRes = 0;
                    try
                    {
                        // Get dpi of jpeg

                        using (FileStream fs = new FileStream(curFile, FileMode.Open, FileAccess.Read))
                        {
                            using (Image original = Image.FromStream(fs))
                            {
                                imgRes = original.HorizontalResolution;
                            }
                        }

                    }
                    catch (Exception ex)
                    {

                        qp.RemoveDocument(docid);
                        throw new Exception(ex.Message, ex.InnerException);
                    }

                    int imgId = qp.AddImageFromFile(curFile, 0);
                    try
                    {
                        qp.SelectImage(imgId);
                        int imgHeightPx = qp.ImageHeight();
                        int imgWidthPx = qp.ImageWidth();

                        int imgVRes = qp.ImageVerticalResolution();

                        // Get sizes in mm

                        float imgRealHeight = (((imgHeightPx / imgRes) * 2.54f) * 10) / 0.352777f;
                        float imgRealWidth = (((imgWidthPx / imgRes) * 2.54f) * 10) / 0.352777f;
                        qp.SetPageDimensions(imgRealWidth, imgRealHeight);
                        qp.DrawImage(0, imgRealHeight, imgRealWidth, imgRealHeight);
                        qp.SaveToFile(PathToSave);
                        qp.ClearImage(imgId);
                        qp.RemoveDocument(docid);
                    }
                    catch (Exception ex)
                    {
                        qp.ClearImage(imgId);
                        qp.RemoveDocument(docid);
                        throw new Exception(ex.Message, ex.InnerException);
                    }
                }
                allPages.Add(PathToSave);
            }

            // merge in one file
            int binderDocid = qp.NewDocument();
            foreach (string pagefn in allPages)
            {
                int res = qp.AddToFileList("binder", pagefn);
            }
            string fileInfoName = Path.GetFileNameWithoutExtension(allPages[0]);
            fileInfoName = fileInfoName.Replace(".jpg", "");
            fileInfoName = fileInfoName.Replace(".jpeg", "");
            fileInfoName = fileInfoName.Replace(".", "_");

            string destblockfilname = filenameprefix + fileInfoName + DateTime.Now.ToString("yyyyMMddHHmm") + ".pdf";
            int res1 = qp.MergeFileList("binder", Path.Combine(pageFolderPath, destblockfilname));

            qp.ClearFileList("binder");
            qp.RemoveDocument(binderDocid);

            return destblockfilname;
        }

And also this is a code for embed file: 

            int blockH = qp.LoadFromFile(BlockFilePath, "");
            if (blockH == 0)
            {
                MessageBox.Show("ERROR!"); return;
            }
            // remove all embedded files from target pdf: 
            int embeddedFilesCount = qp.EmbeddedFileCount();
            for (int ef = 1; ef <= embeddedFilesCount; ef++)
            {
                qp.RemoveEmbeddedFile(ef);
            }
// embed PDF file to target PDF
            int embeddedFileId = qp.AddEmbeddedFile(EmbedFilePath, @"PDF");
            int attachedFiles = qp.AddFileAttachment("Oblogka " + Path.GetFileName(EmbedFilePath), embeddedFileId);
            qp.SaveToFile(BlockFilePath);
            qp.RemoveDocument(blockH);
            MessageBox.Show("DONE!");


Posted By: Michael_67
Date Posted: 11 Aug 15 at 9:30PM
Harier2008,
You do not need to create and save each jpg file to separate PDF file.
It works better with:

qp.SelectImage(imgId);
......
qp.DrawImage(0, imgRealHeight, imgRealWidth, imgRealHeight); qp.NewPage();
......

(You do not need qp.NewPage(); for the first image)


Posted By: Harier2008
Date Posted: 13 Aug 15 at 11:04AM
Thank you. 
It's a good approach! It works very fast. 
But i still have other problem: 
1. I open PDF file (my sample file 324 mb) by LoadFromFile
2. I embed another PDF file to opened file (AddEmbeddedFile and AddFileAttachment). 
3. I save it and close (SaveToFile and RemoveDocument) and all works fine.
4. Next i open same file or another big file (more then 300 mb) and try to Embed File to it. Library doesn't open it. Function LoadFromFile return 0.

Link to my sample file: https://cloud.mail.ru/public/4nqv/tqNFM4tGf


Posted By: Michael_67
Date Posted: 13 Aug 15 at 1:06PM
You files are very big.
It may be that the memory is cleaned too slow. I would unload QuickPDF Library after each document e.G. with Release Library () and force garbage collection with

GC.Collect();
GC.WaitForPendingFinalizers();

Best Regards
Михаил aka Michael :)



Posted By: Harier2008
Date Posted: 17 Aug 15 at 5:16AM
Hello Michael. Thank you for your answer. 

324 mb are very big for this library? It's strange for me... I have 8 GB RAM and also SSD Disk. Why 324 mb  are too large for shared library? Think this is a question to developers of this library... 

Michael, I try this: 

System.Runtime.InteropServices.Marshal.FinalReleaseComObject(qp);
qp = null;
GC.Collect();
GC.WaitForPendingFinalizers();

And some times it's work, but some times it doesn't work... Most it didn't work... 

Also if library have some restrictions to file size why developers can't describe them? 

Дмитрий aka Dmitry :) 


Posted By: Michael_67
Date Posted: 17 Aug 15 at 10:49AM
I use the DLL variant of of Quick PDF, not a COM.

Have you ever considered what is the Error Code (LastErrorCode), if

LoadFromFile  returns 0?

If the memory is not released, two Files can be to big for the 32-bit application.
How much memory is used from your application after saving and removing of document?


Posted By: Harier2008
Date Posted: 18 Aug 15 at 3:16PM
Last error code is 401: can't open input file. That's all information :)
Now i use DLL version in my project. After any LoadFromFile i use ReleaseLibrary(). Also i add x64 version of library. With x64 all works fine. Files above 350 mb opened successfuly, embedding also successfully (i try three times). 
I think the reason of all my problems was a memory leaks when i use LoadFromFile in x32 version. But i still think that it's a bug of library (i use it from 2011). I don't think that 325 mb or above it's a big file. 

Also i found why i've got the blank file after embedding. It happens when LoadFromFile failed. I didn't check this fail and save the blank file with embedded file. Now i check the result of LoadFromLibrary before start to embed file. I dont test x64 version to open big files (above 350 mb). Maybe somewhen i found the way to refuse of embedding and LoadFromFile function. 




Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk