PDFsharp & MigraDoc Forum https://forum.pdfsharp.net/ |
|
Using templates https://forum.pdfsharp.net/viewtopic.php?f=2&t=1982 |
Page 1 of 1 |
Author: | ghendric [ Sun Apr 22, 2012 3:56 pm ] |
Post subject: | Using templates |
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 |
Author: | Thomas Hoevel [ Mon Apr 23, 2012 11:43 am ] |
Post subject: | Re: Using templates |
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. |
Author: | ghendric [ Mon Apr 23, 2012 6:39 pm ] | ||
Post subject: | Re: Using templates | ||
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
|
Author: | Thomas Hoevel [ Tue Apr 24, 2012 8:20 am ] |
Post subject: | Re: Using templates |
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). |
Author: | ghendric [ Fri May 04, 2012 1:50 pm ] |
Post subject: | Re: Using templates |
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? |
Author: | ghendric [ Fri May 04, 2012 2:24 pm ] | ||
Post subject: | Re: Using templates | ||
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();
|
Author: | ghendric [ Fri May 04, 2012 3:49 pm ] |
Post subject: | Re: Using templates |
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... |
Author: | () => true [ Sat May 05, 2012 3:13 pm ] |
Post subject: | Re: Using templates |
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)) |
Author: | ghendric [ Sun May 06, 2012 3:09 am ] |
Post subject: | Re: Using templates |
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); } |
Author: | () => true [ Sun May 06, 2012 10:23 am ] |
Post subject: | Re: Using templates |
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. |
Author: | ghendric [ Sun May 06, 2012 2:34 pm ] |
Post subject: | Re: Using templates |
Is it because I am using a static class? Or do I need to specify a document name or something? Thanks |
Author: | Thomas Hoevel [ Mon May 07, 2012 7:23 am ] |
Post subject: | Re: Using templates |
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. |
Author: | ghendric [ Mon May 07, 2012 9:36 am ] |
Post subject: | Re: Using templates |
Ok, I get it. I wound up creating a new object each time and it worked perfectly. Thanks for the help. |
Page 1 of 1 | All times are UTC |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |