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

Alter section for document thats been fully created
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3591
Page 1 of 1

Author:  nickie [ Wed May 24, 2017 10:06 am ]
Post subject:  Alter section for document thats been fully created

Using migra doc PDFsharp-MigraDocFoundation-1_50-beta3b with windows forms in visual studio 2015

In the Important Facts about MigraDoc, PDFsharp, and PDF it states "MigraDoc documents are highly dynamic, you can change anything at any time."

I've created a pdf document with headers and footers and tables based on the invoice from the samples, built it up fully and saved it. I'm looking to create 5 different versions of the pdf with a slight change to the header for each document, but i don't want to create the body of the document (which has many sections with tables) each time as the details are the same.

I have a table created in the headers.primary section and i'm looking to change a cell, save the document, use the same document alter the header table cell save the document x 4.

Is it possible to get or retrieve a section or headers section and alter the table contents of a document that has been built up already. If im looking for pointers to achieve this.

Thanks

Author:  Thomas Hoevel [ Wed May 24, 2017 11:59 am ]
Post subject:  Re: Alter section for document thats been fully created

Hi!

The general approach:
The Section has an Elements member with an index operator []. Use that to get each element.
Every document element has a Tag member for use by your application. You can use the tag to identify the element that needs modification later on.
Some elements have an Elements member of their own, so in some cases you have to use recursion (e.g. to find a Paragraph in the Cell of a Table).

The simple approach:
If you just need different headers/footer, then just set them new for the section before rendering the document. That would be simpler than finding the right cell in an existing header.

Usage hint: create the master document, then call "Clone()" to create a copy, modify the copy, create a PDF, dispose the copy - and go back to "Clone()" until you got all the PDFs you need.

Or maybe use a subroutine for the common part and call that during the creation of all documents. Cleaner code, but if the creation of the common body takes a long time (e.g. database or web calls) then the "Clone()" method may be much faster.

Author:  nickie [ Wed May 24, 2017 2:37 pm ]
Post subject:  Re: Alter section for document thats been fully created

Hi,

The clone worked. Creation of the common body i was thinking would be the step that would take the most time for each pdf document but further investigation i think its this step MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToFile(document, "MigraDoc.mdddl"); that is taking the longest, which i need to do for each document anyway i presume.

So there might not be much of a gain to clone the body for use each time. Maybe need some further tests on how long it takes.

Thanks

Nickie

Author:  Thomas Hoevel [ Wed May 24, 2017 2:45 pm ]
Post subject:  Re: Alter section for document thats been fully created

nickie wrote:
Creation of the common body i was thinking would be the step that would take the most time for each pdf document but further investigation i think its this step MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToFile(document, "MigraDoc.mdddl"); that is taking the longest, which i need to do for each document anyway i presume.
The file "MigraDoc.mdddl" usually is for debugging purposes only - just delete that line or comment it out.

Author:  nickie [ Wed May 24, 2017 3:27 pm ]
Post subject:  Re: Alter section for document thats been fully created

Fantastic, its producing documents much much much faster now.

Thanks v much.

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