|
Rank: Newbie Groups: Member
Joined: 3/14/2014 Posts: 4
|
Quote: public ACtionCOntroller(){ //blah blah blah get data for model blah blah
MVCToPDF.ResultAsDownload = false; MVCToPDF.RenderAsPDF();
//Add runtime license return View("SnapReportPDF",model); }
protected override void OnResultExecuted(ResultExecutedContext filterContext) { base.OnResultExecuted(filterContext);
EO.Pdf.HtmlToPdfResult result = MVCToPDF.Result; if (result != null) foreach (var page in result.PdfDocument.Pages) { EO.Pdf.HtmlToPdf.ConvertHtml("Overlay text", page); } result.PdfDocument.Save("snapReport.pdf"); }
I want to add a header and footer to my pdf that I am generating from a .NET view. I got the above overriding method from demos on this site but I do not understand how I can add a header or footer this seems to just add more text to the document body. Also don't get how to save the document after you edit it, the method I used above causes general access errors, is there something that I am missing?
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Hi, You are very close --- you just need to move your header/footer contents to the desired area. You would do this by setting HtmlToPdf.Options.OutputArea. For example, the following code output the header at 0.5 inch from the top: HtmlToPdf.Options.OutputArea = new RectangleF(0, 0.5f, 8.5f, 0.5f); EO.Pdf.HtmlToPdf.ConvertHtml("Overlay text", page); By default the OutputArea points to the main content area. That's why if you do not set it, it will just add more text to the main content area. You can find more details on setting output area here: http://www.essentialobjects.com/doc/pdf/htmltopdf/page_size.aspxHope this helps. Please feel free to let us know if you still have any more questions. Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 3/14/2014 Posts: 4
|
I added the segment of code so that my onResultExecutedMethod is now: protected override void OnResultExecuted(ResultExecutedContext filterContext) { base.OnResultExecuted(filterContext);
EO.Pdf.HtmlToPdfResult result = MVCToPDF.Result; if (result != null) { foreach (var page in result.PdfDocument.Pages) { EO.Pdf.HtmlToPdf.Options.OutputArea = new System.Drawing.RectangleF(0, 0.5f, 8.5f, 0.5f); EO.Pdf.HtmlToPdfResult header = EO.Pdf.HtmlToPdf.ConvertHtml("<div>Overlay text</div>", page); } } string fileName = Server.MapPath("\\") + "snapReport.pdf"; result.PdfDocument.Save(fileName); }
But it still isn't adding my html text, it displays the same pdf as if I didn't overload the method.
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Hi, If you wish to send the modified PDF file down to the client, you must use afterConvertHandler when you call RenderAsPDF: http://www.essentialobjects.com/doc/eo.pdf.mvc.mvctopdf.renderaspdf_overload_1.aspxThe reason is the PDF file has already been sent to the client browser when you call base.OnResultExecuted. So the changes you made after that would be too late to make it to the client side. afterConvertHandler is called by base.OnResultExecuted before the file is sent to the client. So that gives you the chance for last minute touch up. Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 3/14/2014 Posts: 4
|
Ok, but what would I do in the afterConvertHandler function? Is that where I would add the header?
Also I cannot seem to padd any variables into MVC.ToPDF.RenderAsPDF, it doesn't compile if I add a parameter to it
CODE: using EO.Pdf.Mvc; using EO.Pdf.Mvc5; using EO.Pdf;
MVCToPDF.ResultAsDownload = false;
EO.Pdf.Mvc.MVCToPDF.RenderAsPDF(Post_Handler);//This does not compile
EO.Pdf.Runtime.AddLicense(mykey);
return View("SnapReportPDF",model); }
private void Post_Handler(object sender, PdfDocumentEventArgs e)//This does not compile, type of PdfDocumentEventArgs cannot be found {
}
protected override void OnResultExecuted(ResultExecutedContext filterContext) { base.OnResultExecuted(filterContext);
EO.Pdf.HtmlToPdfResult result = MVCToPDF.Result; if (result != null) { foreach (var page in result.PdfDocument.Pages) { EO.Pdf.HtmlToPdf.Options.OutputArea = new System.Drawing.RectangleF(0, 0.1f, 8.5f, 9.5f); EO.Pdf.HtmlToPdfResult header = EO.Pdf.HtmlToPdf.ConvertHtml("<div>Overlay text</div>", page); } } string fileName = Server.MapPath("\\") + "snapReport.pdf"; result.PdfDocument.Save(fileName); }
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Hi,
As for the code, you would just move the code you have in your OnResultExecuted into Post_Handler.
If the code does not compile for you, then you need a newer version. This feature was added rather recently (this year), so if you do not see this function in your version, then you need to update to the current version first.
Thanks!
|
|
Rank: Newbie Groups: Member
Joined: 3/14/2014 Posts: 4
|
In the Post_handler function I want to merge the result pdf page with some other pdf, how do I do that because when I attempt to use PDFDocument.Merge it will not allow me to assign the new merged document to result.PDFDocument.
Code: private void Post_Handler(object sender, PdfDocumentEventArgs e) {
EO.Pdf.HtmlToPdfResult result = MVCToPDF.Result; PdfDocument merged = new PdfDocument(); if (result != null) { int i = 0;
WebClient client = new WebClient();
foreach (var url in _drawings) {
//create a pdf document with url byte[] bytes = client.DownloadData(url); MemoryStream ms = new MemoryStream(bytes); PdfDocument newPDF = new PdfDocument(ms); //merge and reasign to result.PdfDOcument merged = PdfDocument.Merge(merged, newPDF);
} result.PdfDocument = PdfDocument.Merge(result.PdfDocument, merged);//This wont compile because it is readonly
foreach (var page in result.PdfDocument.Pages) { i++; //Add header EO.Pdf.HtmlToPdf.Options.OutputArea = new System.Drawing.RectangleF(0.5f, 0.25f, 7.5f, 0.5f); EO.Pdf.HtmlToPdfResult header = EO.Pdf.HtmlToPdf.ConvertHtml("<div>" + _reportName + "</div><div style='float:right'>" + _clientName + "</div><div>" + _locationName + "</div>", page);
//Add footer EO.Pdf.HtmlToPdf.Options.OutputArea = new System.Drawing.RectangleF(0.5f, 10.25f, 7.5f, 0.5f); EO.Pdf.HtmlToPdfResult footer = EO.Pdf.HtmlToPdf.ConvertHtml("<table style='width:100%;'><tr><td style='width:34%'></td><td align='center' style='width:33%'>" + i + "</td><td align='right' style='width:33%'>Print Date: " + _date + "</td></tr><tr><td style='width:34%'></td><td style='width:33%'></td><td align='right' style='width:33%'>Building No. " + _locationNumber + "</td></tr></table>", page); } }
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Hi,
You can replace the document by setting e.Document to the merged document in your Post_Handler.
Thanks!
|
|