PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 6:36 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 14 posts ] 
Author Message
PostPosted: Thu Oct 26, 2017 12:07 pm 
Offline

Joined: Thu Oct 26, 2017 12:00 pm
Posts: 13
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


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 26, 2017 12:36 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 26, 2017 12:40 pm 
Offline

Joined: Thu Oct 26, 2017 12:00 pm
Posts: 13
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().


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 26, 2017 12:47 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 26, 2017 1:00 pm 
Offline

Joined: Thu Oct 26, 2017 12:00 pm
Posts: 13
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?


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 26, 2017 2:31 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3095
Location: Cologne, Germany
1.50 is faster and has fewer known bugs than 1.32. The "stable" label is overrated, better get the beta. :lol:

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 26, 2017 3:18 pm 
Offline

Joined: Thu Oct 26, 2017 12:00 pm
Posts: 13
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.


Top
 Profile  
Reply with quote  
PostPosted: Thu Oct 26, 2017 4:41 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 27, 2017 8:14 am 
Offline

Joined: Thu Oct 26, 2017 12:00 pm
Posts: 13
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 10855 times ]
Top
 Profile  
Reply with quote  
PostPosted: Sat Oct 28, 2017 1:01 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 30, 2017 8:32 am 
Offline

Joined: Thu Oct 26, 2017 12:00 pm
Posts: 13
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.


Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 30, 2017 8:37 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 909
Location: CCAA
MwBakker wrote:
Is there any place I can donate or help you guys?
The official donations page:
http://pdfsharp.net/Donate.ashx?HL=donate

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Mon Oct 30, 2017 9:16 am 
Offline

Joined: Thu Oct 26, 2017 12:00 pm
Posts: 13
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


Top
 Profile  
Reply with quote  
PostPosted: Fri Dec 22, 2017 3:26 pm 
Offline

Joined: Thu Oct 26, 2017 12:00 pm
Posts: 13
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 122 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group