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

Getting an error when trying to add page numbers.
https://forum.pdfsharp.net/viewtopic.php?f=2&t=551
Page 1 of 1

Author:  Kennyj [ Tue Nov 25, 2008 1:38 pm ]
Post subject:  Getting an error when trying to add page numbers.

I'm getting this error where is says
gfx = XGraphics.FromPdfPage(page);

can someone explain how I need to fix this?

Cannot create XGraphics for a page of a document that cannot be modified. Use PdfDocumentOpenMode.Modify.

Code:
if (checkBox1.Checked == true)
                {
                    MessageBox.Show("Printing Page Numbers.");
                    PdfDocument outputDocument = new PdfDocument();

                    XFont font = new XFont("Verdana", 10, XFontStyle.Bold);
                    XStringFormat format = new XStringFormat();
                    format.Alignment = XStringAlignment.Center;
                    format.LineAlignment = XLineAlignment.Far;
                    XGraphics gfx;
                    XRect box;

                    foreach (string file in pdfList.Items)
                    {

                        PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);
                        int count = inputDocument.PageCount;

                        for (int idx = 0; idx < count; idx++)
                        {
                            PdfPage page = inputDocument.PageCount > idx ?
                            inputDocument.Pages[idx] : new PdfPage();
                            gfx = XGraphics.FromPdfPage(page);
                            box = page.MediaBox.ToXRect();
                            box.Inflate(0, -10);
                            gfx.DrawString(String.Format("{0} • {1}", file, idx + 1),
                            font, XBrushes.Red, box, format);

                            outputDocument.AddPage(page);


                        }
                    }



Author:  Thomas Hoevel [ Wed Nov 26, 2008 8:46 am ]
Post subject: 

The page was opened in Import mode (for import only, i. e. read-only).

It's in the error message: use PdfDocumentOpenMode.Modify instead of PdfDocumentOpenMode.Import if you really want to modify the PDF file you opened.
Or create a new PDF file, copy the contents of the old file and add page numbers to the new file.

With PdfDocumentOpenMode.Modify the source file will not change if you don't call Save.

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