PDFsharp & MigraDoc Forum

PDFsharp - A .NET library for processing PDF & MigraDoc - Creating documents on the fly
It is currently Sat Oct 11, 2025 4:28 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules

Also see our new Tailored Support & Services site.



Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Using templates
PostPosted: Sun Apr 22, 2012 3:56 pm 
Offline
User avatar

Joined: Sun Apr 22, 2012 3:47 pm
Posts: 26
Hi, this project caught my eye and I was wonder if PDFSharp or MigraDoc could be used for creating pdf's out of an HTML template of an invoice for example. I checked out the invoice sample that was using an XML template, which would probably be better for what I need to do, but my concern with that would be the format of the document. Can you use an XSL file with the XML document with MigraDoc or can I read an HTML file, replace some fields with actual data and then convert it to PDF with MigraDoc or PDFSharp?

Thanks

_________________
Thanks,
George


Top
 Profile  
Reply with quote  
 Post subject: Re: Using templates
PostPosted: Mon Apr 23, 2012 11:43 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3136
Location: Cologne, Germany
Hi!

MigraDoc creates the document (PDF or Word) for you, but you have to provide the data and define the layout. It cannot import HTML.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: Using templates
PostPosted: Mon Apr 23, 2012 6:39 pm 
Offline
User avatar

Joined: Sun Apr 22, 2012 3:47 pm
Posts: 26
Is it possible to define the layout with specific details? I was checking out the invoice sample from the project and it made a generic looking invoice. I need to be able to specify more detail like shaded headers, etc.. Can I do that with MigraDoc? I've attached a sample screenshot of what I am talking about.

Thanks


Attachments:
File comment: Screen shot of a detailed invoice..
invoice sample.JPG
invoice sample.JPG [ 51.01 KiB | Viewed 21548 times ]

_________________
Thanks,
George
Top
 Profile  
Reply with quote  
 Post subject: Re: Using templates
PostPosted: Tue Apr 24, 2012 8:20 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3136
Location: Cologne, Germany
ghendric wrote:
Can I do that with MigraDoc?
You can draw a PDF page in the background. This page can contain your shaded logo (header), footer, even the table grid.

Then you can use MigraDoc to fill in the variable text having the table in the body of the document, using a textframe for the date.

If the invoice might have more than one page, use the technique shown in the Mix MigraDoc and PDFsharp sample. This also allows to use different background PDF pages.

It'll be simpler if the background page contains no table grid and you use MigraDoc to create both grid lines and table contents.
Then you would use the Invoice sample, but with a different logo and re-formatted tables (but without template).

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: Using templates
PostPosted: Fri May 04, 2012 1:50 pm 
Offline
User avatar

Joined: Sun Apr 22, 2012 3:47 pm
Posts: 26
I was able to create a PDF file but when I open the file with Adobe reader, the image is missing. I can see it if I use the Doc viewer control though. Is there a way to embed the image into the PDF file?

_________________
Thanks,
George


Top
 Profile  
Reply with quote  
 Post subject: Re: Using templates
PostPosted: Fri May 04, 2012 2:24 pm 
Offline
User avatar

Joined: Sun Apr 22, 2012 3:47 pm
Posts: 26
Also, I'm getting an error whenever I try to create more than 1 pdf file. The first 1 creates fine, the second one errors. The error that I am getting is
Quote:
The document is already bound to another renderer. A MigraDoc document can be rendered by only one renderer, because the rendering process modifies its internal structure. If you want to render a MigraDoc document on different renderers, you must create a copy of it using the Clone function.
I get the error when ever I try to set renderer.Document = doc. I've also listed the code where I'm getting it.

Code:
               
                Document doc = BankReferencePDF.CreateDocument();
                PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfFontEmbedding.Always);
                renderer.WorkingDirectory = @"C:\temp";
                renderer.Document = doc;
                renderer.RenderDocument();
                String filename = Guid.NewGuid().ToString("N").ToUpper() + ".pdf";
                renderer.PdfDocument.Save(filename);

                DocViewer frm = new DocViewer();
                frm.BankRefDocument = doc;
                frm.Show();


Attachments:
File comment: error that im getting
error.jpg
error.jpg [ 79.08 KiB | Viewed 21512 times ]

_________________
Thanks,
George
Top
 Profile  
Reply with quote  
 Post subject: Re: Using templates
PostPosted: Fri May 04, 2012 3:49 pm 
Offline
User avatar

Joined: Sun Apr 22, 2012 3:47 pm
Posts: 26
ghendric wrote:
I was able to create a PDF file but when I open the file with Adobe reader, the image is missing. I can see it if I use the Doc viewer control though. Is there a way to embed the image into the PDF file?


I figured this one out. Apparently supplying a WorkingDirectory path to the renderer loses the image for some reason.. I commented that out and now the image gets embedded.

Still having the problem with the error though...

_________________
Thanks,
George


Top
 Profile  
Reply with quote  
 Post subject: Re: Using templates
PostPosted: Sat May 05, 2012 3:13 pm 
Offline
PDFsharp Expert
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 354
Hi!

What is "BankReferencePDF.CreateDocument();" doing? Will it always create a new document (this should work) or does it return a reference to a previously created document (this does not work (and the error message indicates that this happens))

_________________
Öhmesh Volta ("() => true")
PDFsharp Team Holiday Substitute


Top
 Profile  
Reply with quote  
 Post subject: Re: Using templates
PostPosted: Sun May 06, 2012 3:09 am 
Offline
User avatar

Joined: Sun Apr 22, 2012 3:47 pm
Posts: 26
BankReferencePDF is a static class with a static constructor. The document is created when CreateDocument is called. I have a button on a form that when I click it, it creates the document from this class. If I click it a second time, then that's when I get the error from the renderer object..

Code:
       
        static BankReferencePDF() {
            _document = new Document();
            _document.Info.Title = "Bank Reference";
        }

        public static Document CreateDocument() {

            try {
                DefineStyles();
                CreatePage();

            } catch (Exception ex) {
                throw ex;
            }
            return _document;
        }

        private static void CreatePage() {
            Section section = _document.AddSection();

            PageSetup pgs = new PageSetup() {
                PageFormat = PageFormat.Letter,
                PageWidth = "8.5in",
                PageHeight = "11in",
                TopMargin = "0.5in",
                LeftMargin = "0.6in",
                RightMargin = "0.5in",
                BottomMargin = "0.5in"
            };
           
            section.PageSetup = pgs;

            CreatePageHeader(section);

            CreatePageBody(section);

            CreateCommentsSection(section);

            CreatePageSignatureLine(section);

        }

_________________
Thanks,
George


Top
 Profile  
Reply with quote  
 Post subject: Re: Using templates
PostPosted: Sun May 06, 2012 10:23 am 
Offline
PDFsharp Expert
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 354
You don't call "_document = new Document();" in CreateDocument, instead you always return the same document.
That's why it doesn't work, that's what the error message says.

Create a completely new document in CreateDocument and it'll work.

_________________
Öhmesh Volta ("() => true")
PDFsharp Team Holiday Substitute


Top
 Profile  
Reply with quote  
 Post subject: Re: Using templates
PostPosted: Sun May 06, 2012 2:34 pm 
Offline
User avatar

Joined: Sun Apr 22, 2012 3:47 pm
Posts: 26
Is it because I am using a static class? Or do I need to specify a document name or something?

Thanks

_________________
Thanks,
George


Top
 Profile  
Reply with quote  
 Post subject: Re: Using templates
PostPosted: Mon May 07, 2012 7:23 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3136
Location: Cologne, Germany
ghendric wrote:
Is it because I am using a static class?
Yes and no.
You call "_document = new Document();" in the static constructor, therefore it is called only once.
If you'd call it in "CreateDocument()" it would be called once for every new document and the problem would be solved.

You create the document once, but add the contents each time a call to "CreateDocument()" is made. That does not work.

You can keep the class static, but it would be more logical IMHO to remove "static" and create a new object when you need one.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: Using templates
PostPosted: Mon May 07, 2012 9:36 am 
Offline
User avatar

Joined: Sun Apr 22, 2012 3:47 pm
Posts: 26
Ok, I get it. I wound up creating a new object each time and it worked perfectly. Thanks for the help.

_________________
Thanks,
George


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 111 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