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

Copying document contents for adding new section
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3678
Page 1 of 1

Author:  MwBakker [ Thu Oct 26, 2017 12:07 pm ]
Post subject:  Copying document contents for adding new section

Good day all,

I have a document that is building and viewing correctly in PDF form. However, when I want to add an extra commentary section to this PDF document I am unable to copy the document contents.

Code:
 //
        //  adds extra commentary to the document
        //
        public Document genCommentary(string commentary, string author, string function)
        {
            Document docWithCommentary = new Document();
            // docWithCommentary = this.doc;
            docWithCommentary = doc.Clone();
            Section sectionCommentary = docWithCommentary.AddSection();

            Paragraph paragraph = new Paragraph();
            paragraph = sectionCommentary.AddParagraph("Adviesrapport", "Adviesrapport");
            paragraph = sectionCommentary.AddParagraph(commentary, "Normal");
            paragraph = sectionCommentary.AddParagraph(author + ',', "Normal");
            paragraph = sectionCommentary.AddParagraph(function, "writerFunction");

            return docWithCommentary;
        }

        //                                         
        //  render and stream PDF document
        //
        public MemoryStream renderandStreamPDF(Document docWithCommentary)
        {
            PdfDocumentRenderer renderer = new PdfDocumentRenderer();
           
            if (docWithCommentary == null)
            {
                renderer.Document = this.doc;
            } else
            {
                renderer.Document = docWithCommentary;
            }
         
            renderer.RenderDocument();

            MemoryStream stream = new MemoryStream();
            renderer.PdfDocument.Save(stream, false);
            return stream;
        }


So a new document is being created, based on the contents of the old document. It is a new object, yet I get the following error message:
Quote:
The document is already bound to another renderer.

But why? I create a new renderer and my docWithCommentary to be rendered is also a new object.

SIDENOTE: when I completely rebuild the content instead of copying I do not get this error message, but it costs the machine extra resources while it just needs to copy the doc rather than entirely rebuild it from scratch

Author:  Thomas Hoevel [ Thu Oct 26, 2017 12:36 pm ]
Post subject:  Re: Copying document contents for adding new section

Hi!

Since you show just a code snippet, I can only speculate.

I think "doc.Clone()" is the correct approach. Make sure to create the clone before rendering the first document.
Add the extra content to the clone at any time.

Author:  MwBakker [ Thu Oct 26, 2017 12:40 pm ]
Post subject:  Re: Copying document contents for adding new section

Hi Thomas, thank you for your quick reply. What you guys do and achieve with PDFSharp and its support is very valuable it should be mentioned more so hereby.

Unfortunatly the doc.Clone() did not seem to work in any way.. What I did to solve this is visible in the following code.

Code:
 //
        //  adds extra commentary to the document
        //
        public Document genCommentary(string commentary, string author, string function)
        {
            Document docWithCommentary = new Document();
            // copy contents of previous document
            foreach (Section section in this.doc.Sections)
            {
                Section copiedSection = section.Clone();
                docWithCommentary.Add(copiedSection);
            }
            // clear old doc after copy
            doc = null;

            Section sectionCommentary = docWithCommentary.AddSection();

            Paragraph paragraph = new Paragraph();
            paragraph = sectionCommentary.AddParagraph("Adviesrapport", "InvolvedSystemsHeader");
            paragraph = sectionCommentary.AddParagraph(commentary, "Normal");
            paragraph = sectionCommentary.AddParagraph(author + ',', "Normal");
            paragraph = sectionCommentary.AddParagraph(function, "writerFunction");

            return docWithCommentary;
        }

        //                                         
        //  saves the PDF as a file to the system
        //
        public MemoryStream renderandStreamPDF(Document docWithCommentary)
        {
            renderer = new PdfDocumentRenderer();
           
            if (docWithCommentary == null)
            {
                renderer.Document = this.doc;
            } else
            {
                renderer.Document = docWithCommentary;
            }
         
            renderer.RenderDocument();

            MemoryStream stream = new MemoryStream();
            renderer.PdfDocument.Save(stream, false);
            return stream;
        }



I just hope my system's resources are not used a lot more in for-eaching every section of the old document, compared to doing a doc.Clone().

Author:  Thomas Hoevel [ Thu Oct 26, 2017 12:47 pm ]
Post subject:  Re: Copying document contents for adding new section

Version 1.32 has a bug with the Clone() method that was fixed with version 1.50.
I hope you use the latest version of PDFsharp/MigraDoc 1.50.

Author:  MwBakker [ Thu Oct 26, 2017 1:00 pm ]
Post subject:  Re: Copying document contents for adding new section

Thomas Hoevel wrote:
Version 1.32 has a bug with the Clone() method that was fixed with version 1.50.
I hope you use the latest version of PDFsharp/MigraDoc 1.50.


I did not, because I did want to use beta packages into this program. Should I update or do I have instability risks?

Also: how much of a difference this would make in performance for me? A lot or negligible?

Author:  Thomas Hoevel [ Thu Oct 26, 2017 2:31 pm ]
Post subject:  Re: Copying document contents for adding new section

1.50 is faster and has fewer known bugs than 1.32. The "stable" label is overrated, better get the beta. :lol:

Author:  MwBakker [ Thu Oct 26, 2017 3:18 pm ]
Post subject:  Re: Copying document contents for adding new section

Thomas Hoevel wrote:
1.50 is faster and has fewer known bugs than 1.32. The "stable" label is overrated, better get the beta. :lol:


Deleted PDFSharp and MigraDoc, reinstalled both again (PDFSharp 1.50 beta) and it conflicted while running, so I am afraid to stick with this.

Gotta use the for-each through sections anyway because if a new commentary section is generated, the old commentary should not be taken along in copy (otherwise I will have 2 commentary sections)

I just hope the rendering is not very much slower as with 1.50 because at 30 pages or so it takes up to 7 seconds with 1.32.

Not complaining though, this tool is free and rock-solid so as a student I am actually very happy with the general experience of PDFSharp and Migradoc in building PDF's etc. and that rendering large documents takes a few moments is logical.

Author:  TH-Soft [ Thu Oct 26, 2017 4:41 pm ]
Post subject:  Re: Copying document contents for adding new section

Hi!
MwBakker wrote:
Deleted PDFSharp and MigraDoc, reinstalled both again (PDFSharp 1.50 beta) and it conflicted while running, so I am afraid to stick with this.
Did you try the GDI build or the WPF build?
We can't help you if you do not provide information about the conflict.

With respect to speed: my testcode went from 24.4 s down to 1.6 s. The biggest improvement is with tables.
See also:
viewtopic.php?p=9380#p9380

Author:  MwBakker [ Fri Oct 27, 2017 8:14 am ]
Post subject:  Re: Copying document contents for adding new section

Currently I am including these two, working stable but a rendering of 7 sec appr for 32 pages containting tables on each section.

I read in your speed measurements that I need Migradoc GDI+ ? to get along with PDFSharp Beta 2 ?

Attachments:
Capture.PNG
Capture.PNG [ 14.89 KiB | Viewed 11119 times ]

Author:  TH-Soft [ Sat Oct 28, 2017 1:01 pm ]
Post subject:  Re: Copying document contents for adding new section

The second package includes the first package, bus since they have different versions you are getting conflicts.

You only need one package. And the latest package using GDI+ is this:
https://www.nuget.org/packages/PDFsharp ... 619-beta4c

The speed was tested with beta 2, but you should use the latest beta.

Author:  MwBakker [ Mon Oct 30, 2017 8:32 am ]
Post subject:  Re: Copying document contents for adding new section

TH-Soft wrote:
The second package includes the first package, bus since they have different versions you are getting conflicts.

You only need one package. And the latest package using GDI+ is this:
https://www.nuget.org/packages/PDFsharp ... 619-beta4c

The speed was tested with beta 2, but you should use the latest beta.



Ok so now my rendering takes maximum 2 seconds for a 32 paged PDF document.
For each page including header with logo + footer, table with icons per row and several larger icons on page itself. That speed is amazing

Is there any place I can donate or help you guys? I may just be a student but I would love to help out a little.

Author:  TH-Soft [ Mon Oct 30, 2017 8:37 am ]
Post subject:  Re: Copying document contents for adding new section

MwBakker wrote:
Is there any place I can donate or help you guys?
The official donations page:
http://pdfsharp.net/Donate.ashx?HL=donate

Author:  MwBakker [ Mon Oct 30, 2017 9:16 am ]
Post subject:  Re: Copying document contents for adding new section

TH-Soft wrote:
MwBakker wrote:
Is there any place I can donate or help you guys?
The official donations page:
http://pdfsharp.net/Donate.ashx?HL=donate


Alright, after my next payment at my student sidejobs I'll do this

Author:  MwBakker [ Fri Dec 22, 2017 3:26 pm ]
Post subject:  Re: Copying document contents for adding new section

TH-Soft wrote:
MwBakker wrote:
Is there any place I can donate or help you guys?
The official donations page:
http://pdfsharp.net/Donate.ashx?HL=donate


So it's been a while but I got financially a little under the radar. I have made a donation to the wonderfull jobs you guys are doing. I am still a student myself so I would give more if I had the opportunity

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