PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Apr 28, 2024 11:52 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed Dec 05, 2012 3:48 pm 
Offline

Joined: Wed Dec 05, 2012 3:45 pm
Posts: 2
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


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 05, 2012 4:49 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
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

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 05, 2012 5:22 pm 
Offline

Joined: Wed Dec 05, 2012 3:45 pm
Posts: 2
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"


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 05, 2012 5:28 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3096
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 348 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group