merge PDFS

DynamicPDF Merger for .NET (v3 and older) Forum

 Feb 11 2010 4:46 PM

I get the following error

"There was end error while parsing the input PDF file. The PDF is likely corrupt or invalid"

when my page runs the following line of code more than once.
document.Draw(MapPath("~/PDFs/MergePDFs.pdf"));

It always works the first time. It seems like the file is still open or caught in memory. How do I go around this?
 Feb 11 2010 5:00 PM
Posted by a ceTe Software moderator
Hello,

The error you are getting is likely because of some stray HTML getting outputted along with the PDF stream. Please try adding the following line of code right after the DrawToWeb method of the document object and it will likely solve your problem.

    Visible = false;

Thanks,
ceTe Software Support Team
 Feb 12 2010 7:30 AM
I'm not using the DrawToWeb method, I'm using Draw. I tried adding visible=false with the Draw method. I still run into the same issue, plus my webpage disappears.

The reason I'm using the Draw method is b/c I want to open the file in a different window, so I save the file then open it. If there is a way to open the file in a different window using the DrawToWeb method then I can go that route.
 Feb 12 2010 7:51 AM
Posted by a ceTe Software moderator
Hello,

Can you please give us more details about your application and the error? In which line exactly you are getting this error? Is it when calling the Draw method or when opening the saved PDF to display in another page?

Also are you using any input PDF documents? If so can you please send over a small sample application along with the input PDF to our Support Team which can recreate the error so that they can troubleshoot the issue.

Thanks,
ceTe Software Support Team.
 Feb 12 2010 8:02 AM
First I loop thru a directory to merge all the pdf files:
            string strFileName;

            ceTe.DynamicPDF.Page page = new ceTe.DynamicPDF.Page(PageSize.Letter);
            int PageCnt = 0;
            int FileCnt = 0;

            string PDFDir = MapPath("~/PDFs/");
            MergeDocument document = new MergeDocument();
            string[] files = System.IO.Directory.GetFiles(PDFDir);
            //loop thru directory and merge all PDF documents
            foreach (string file in files)
            {
                if (System.IO.Path.GetExtension(file) == ".pdf")
                {

                    if (FileCnt == 0)
                    {
                        document = new MergeDocument(file);
                        // Add Bookmark
                        strFileName = System.IO.Path.GetFileNameWithoutExtension(file);
                        document.Pages[0].Elements.Add(new ceTe.DynamicPDF.PageElements.Bookmark(strFileName, 0, 0));
                        PageCnt = document.Pages.Count;
                    }
                    else
                    {
                        // Append additional PDF
                        document.Append(file);
                        // Add Bookmark
                        strFileName = System.IO.Path.GetFileNameWithoutExtension(file);
                        document.Pages[PageCnt].Elements.Add(new ceTe.DynamicPDF.PageElements.Bookmark(strFileName, 0, 0));
                        PageCnt = document.Pages.Count;
                    }
                    FileCnt += 1;
                }
            }

Then I merge them.
            try
            {
                //Save document to specific location
                document.Draw(MapPath("~/PDFs/MergePDFs.pdf"));
                

            }
            catch (Exception ex)
            {
                string msg = "<script>alert('" +
                "Process error, Contact support or try again. " +
                ex.Message.Replace("\r\n", "").Replace("'", "") + "');</script>";
                ScriptManager.RegisterClientScriptBlock(this, typeof(System.Web.UI.Page), "alert", msg, false);
                return;
            }

My code crashes on the document.Draw method, it only happens when I run the code twice, always works the first time around
 Feb 12 2010 9:00 AM
Posted by a ceTe Software moderator
Hello,

The reason for crashing of the application when running for the second time is because of the file name you are using to save it.

When running the code for the first time it creates a PDF using all the PDFs from the /PDFs/ folder with the name "MergePDFs.pdf" in the same folder.

When running the code for the second time it merges the above PDF ( "MergePDFs.pdf" as it is in the /PDFs/ folder) with the other PDFs again and tries to save with the same name in same folder. The original file is still in use and you are trying to save it with same name in same folder. This is the reason for the error you are getting.

Try to save it with different name every time and this will solve the problem.

Thanks,
ceTe Software Support Team.
 Feb 12 2010 9:13 AM
My assumption was that it would always over write it.

Thank you, I'm allset
 Aug 06 2024 6:44 AM
Looking for the best solution to merge PDFs online? Try the SameTools PDF Pro tool, which I believe is the greatest option for merging multiple PDF files into one, you are free to upload multiple PDF files as you like to merge them into a single PDF file with this tool, which can easily merge several PDF files into one. The software examines PDF files of any size without any problems users can freely merge all their PDF files. It can be used with a variety of Windows operating systems, including 11, 10, 8.1, 8, 7, Vista, XP, and so on. Users can enjoy a free demo version without the need to pay anything, and there are no file size limitations.

All times are US Eastern Standard time. The time now is 1:33 PM.