Open a document in PDFsharp
Asked Answered
A

1

6

I have a method called Save which saves a PDF document at a specified location.

PdfDocument Document=new PdfDocument();
public void Save(string pathWithFileName)
{
     Document.Save(pathWithFileName);
}

Now I draw some paragraphs, using XGrahics class. Then I save the document using Save method. It works perfectly.

Now I want to reopen document, add some stuff and again save the document. How can I do this?

Amuse answered 1/2, 2017 at 12:27 Comment(3)
what problems have you faced in opening have you tried opening itAcclaim
When i tried to add another paragraph in document it crashes.Amuse
I presume "crashes" means "throws an exception". If you disclose details about the exception we can help you understand the error message.Ganger
G
6

To open an existing document, use Open() with the correct pathname:

PdfDocument document = PdfReader.Open(filenameDest); 

Then make the changes. Finally save it as you already do:

document.Save(filenameDest); 

PDFsharp comes with several samples.
You can download the complete sample code here:
http://pdfsharp.codeplex.com/releases/view/618773

Sample snippets and explanations can be found here:
http://www.pdfsharp.net/wiki/PDFsharpSamples.ashx

Ganger answered 1/2, 2017 at 13:1 Comment(2)
Thanks a lot. One question more: When I open the document again for adding some text, I must to updated the XGraphics, isn't it ?Amuse
The XGraphics object is associated with a PdfPage object. After opening the PDF file you have a new PdfPage and you will need a fresh XGraphics object for that page.Ganger

© 2022 - 2024 — McMap. All rights reserved.