PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Exception on PdfDocument.close() call
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2256
Page 1 of 1

Author:  Nixeus [ Wed Dec 05, 2012 3:48 pm ]
Post subject:  Exception on PdfDocument.close() call

Hello,

I would like to create a function in order to merge some pdf's.

Here is my code

Code:
public class PDF_Merge
    {
        static string [] strTabPdfFiles;
       

        public static string SetPdfToMerge(string strPdfFilesInput)
        {
            strTabPdfFiles = strPdfFilesInput.Split(';');
            return "O";
        }

        public static string MergeToPdf(string strPdfFilesOutput)
        {
            try
            {
                PdfDocument objDocumentFinal = new PdfDocument(strPdfFilesOutput);

                foreach (string strDoc in strTabPdfFiles)
                {
                    PdfDocument objDocument = PdfReader.Open(strDoc, PdfDocumentOpenMode.Import);

                    foreach (PdfPage page in objDocument.Pages)
                    {
                        objDocumentFinal.AddPage(page);
                    }
                    objDocument.Close();----------> Exception : File cannot be modified
                }
                objDocumentFinal.Close();

                objDocumentFinal.Save(strPdfFilesOutput);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

            return "O";
        }

    }


My problem is that on the objDocument.Close() call, i have an exception : "The document cannot be modified".

Anyone could help me about that ?


Great thanks for this lib,

Best regards,

Nixeus

Author:  Thomas Hoevel [ Wed Dec 05, 2012 4:49 pm ]
Post subject:  Re: Exception on PdfDocument.close() call

Hi!

A PDF file opened with PdfDocumentOpenMode.Import is for import only and cannot be modified.
Try PdfDocumentOpenMode.Modify instead.

Or look at the Concatenate Documents sample:
http://www.pdfsharp.net/wiki/Concatenat ... ample.ashx

Author:  Nixeus [ Wed Dec 05, 2012 5:22 pm ]
Post subject:  Re: Exception on PdfDocument.close() call

Hello,

Thanks for your answer, but i don't want to modify the PDF, i would like to create a new. So why using "modify" ?

Nevertheless, i tried it and i have an other exception :

"A PDF document must be opened with PdfDocumentOpenMode.Import to import pages from it"

Author:  Thomas Hoevel [ Wed Dec 05, 2012 5:28 pm ]
Post subject:  Re: Exception on PdfDocument.close() call

Nixeus wrote:
i don't want to modify the PDF
Sorry, I missed that. But the sample works.
So maybe using for instead of foreach makes the difference - or something else.
I can't investigate this today, but would like to hear from you if you find something out.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/