0

I want to build a container around an existing PDF page, so actually enlarge that document and put it in the middle so that I can write more things into.

What I tried so far :

                        // Open the file
                    PdfDocument inputDocument = PdfReader.Open(filename, PdfDocumentOpenMode.Import);// PdfReader.Open(filename, PdfDocumentOpenMode.Modify);

                    PdfDocument newContainerDocument = new PdfDocument();

                    // Create an empty page or load existing
                    for (int idx = 0; idx < inputDocument.PageCount; idx++)
                    {
                        PdfPage page = new PdfPage(); //this should be the actual container
                        page.Width = tempWidth;
                        page.Height = tempHeight;

                        //gfx.DrawImage(image, (page.Width / 2) - (width / 2), (page.Height / 2) - (height / 2), width, height); //put it in the middle of  the container, this would only work with IMAGES ... using "XGraphics"

                        // Add the page and save it
                        newContainerDocument.AddPage(inputDocument.Pages[idx]);

                    }

                    newContainerDocument.Save(String.Format("{0} - Page {1}_expandedFile.pdf", insertName,DateTime.Now.Ticks.ToString()));

                    inputDocument.Close();
                    newContainerDocument.Close();

But I get:

The document cannot be modified

What is wrong here ?

Thank you

jsanalytics
  • 13,058
  • 4
  • 22
  • 43
JoeTheRock
  • 13
  • 4
  • Not an answer to your question, but you don't need to copy a page to "enlarge" it. What you need to do is enlarge the mediabox of the page. This will give you a larger sheet while your content will remain the same size, hence providing you with whitespace you can fill with additional content afterwards. – David van Driessche Mar 13 '16 at 07:32

1 Answers1

0

PdfDocumentOpenMode.Import is for import, allowing you to copy pages to a new file. PdfDocumentOpenMode.Modify is for modifications.

I think the problem is that you do not copy pages, you add existing pages to a new document.

Your code might work with Clone():
newContainerDocument.AddPage((PdfPage)inputDocument.Pages[idx].Clone());

I would create a new page and draw the existing page onto that page, as this sample does:
http://pdfsharp.net/wiki/TwoPagesOnOne-sample.ashx