| PDFsharp & MigraDoc Forum https://forum.pdfsharp.net/ |
|
| PDFsharp edit a pdf file https://forum.pdfsharp.net/viewtopic.php?f=2&t=2516 |
Page 1 of 1 |
| Author: | yuvi.dagar [ Mon Jul 15, 2013 7:31 am ] |
| Post subject: | PDFsharp edit a pdf file |
Environment - PDFsharp Library, Visual Studio 2012 and C# as the language. I am trying to:
2. add some text to it 3. save it as another file (Test2.pdf) I am able to do all the following. But when I open the file Test2.pdf the size of the page is getting reduced to Width = 11 inches, Height – 11 inches. These PDF files that I am using are Product Specification Sheets that I have downloaded from the internet. I believe this is happening on only certain types of file and I am not sure how to differentiate these files. Code given below: //File dimentions - Width = 17 inches, Height - 11 inches (Tabloid Format) PdfDocument pdfDocument = PdfReader.Open(@"D:\Test1.pdf", PdfDocumentOpenMode.Modify); PdfPage page = pdfDocument.Pages[0]; XGraphics gfx = XGraphics.FromPdfPage(page); XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic); gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center); //When the file is saved dimentions change to - Width = 11 inches, Height - 11 inches pdfDocument.Save(@"D:\Test2.pdf"); |
|
| Author: | Thomas Hoevel [ Mon Jul 15, 2013 7:56 am ] |
| Post subject: | Re: PDFsharp edit a pdf file |
PDF files come in many different "flavors". Maybe the workaround described here will work for you: viewtopic.php?p=2637#p2637 |
|
| Author: | yuvi.dagar [ Mon Jul 15, 2013 9:06 am ] |
| Post subject: | Re: PDFsharp edit a pdf file |
Thomas, thank you for the reply. I restructured the code as suggested by you to the following: Code: PdfDocument PDFDoc = PdfReader.Open(@"D:\Test1.pdf", PdfDocumentOpenMode.Import); PdfDocument PDFNewDoc = new PdfDocument(); for (int Pg = 0; Pg < PDFDoc.Pages.Count; Pg++) { PDFNewDoc.AddPage(PDFDoc.Pages[Pg]); XGraphics gfx = XGraphics.FromPdfPage(PDFDoc.Pages[Pg]); XFont font = new XFont("Arial", 10, XFontStyle.Regular); gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, PDFDoc.Pages[Pg].Width, PDFDoc.Pages[Pg].Height), XStringFormats.BottomCenter); } PDFNewDoc.Save(@"D:\Test2.pdf"); When I run this code I get the exception - Cannot create XGraphics for a page of a document that cannot be modified. Use PdfDocumentOpenMode.Modify. And when I change the code from PdfDocument PDFDoc = PdfReader.Open(@"D:\Test1.pdf", PdfDocumentOpenMode.Import); to PdfDocument PDFDoc = PdfReader.Open(@"D:\Test1.pdf", PdfDocumentOpenMode.Modify); I get the exception - A PDF document must be opened with PdfDocumentOpenMode.Import to import pages from it. How exactly should I achieve this ? |
|
| Author: | Thomas Hoevel [ Mon Jul 15, 2013 9:15 am ] |
| Post subject: | Re: PDFsharp edit a pdf file |
Use the page returned by AddPage or the page from PDFNewDoc.Pages, not the page from PDFDoc.Pages. See also: http://stackoverflow.com/a/17649357/162529 |
|
| Author: | yuvi.dagar [ Mon Jul 15, 2013 9:29 am ] |
| Post subject: | Re: PDFsharp edit a pdf file |
Thomas, thanks a lot !!! This solved my problem. |
|
| Page 1 of 1 | All times are UTC |
| Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |
|