I am using OpenHtmlToPdf. Is it possible to add 1 page as last page of document. I have read all document but found noting.
The following is the same code to generate pdf document.
string html = Utils.ReadFile(#"C:\inetpub\wwwroot\pdf.html");
html = html.Replace("###BODY###", "DYNAMIC CONTENT");
var pdf = Pdf
.From(html)
.OfSize(PaperSize.A4)
.WithTitle("PDF Report With Logo")
.WithoutOutline()
.WithObjectSetting("header.htmlUrl", "http://localhost/header.htm")
.WithObjectSetting("footer.htmlUrl", "http://localhost/footer.htm")
.WithObjectSetting("web.defaultEncoding", "utf-8")
.Landscape()
.Comressed()
.Content();
If your last page is also based on html, you can add a page brake to your final html and then add page itself:
html += $"<div style=\"page-break-after: always;\"></div>{your_last_page}";
Related
I am getting html code in *.aspx.vb page. I want to display this content as HTML or web page, but instead it is getting displayed as html code using response.write()
str_ = Server.HtmlEncode(GetTemplateContents("email_customerActivation.htm", asz))
Response.ContentType = "text/html"
Response.Write(str_)
i think you are trying to hard ! you have a special control for this purpose:
asp-Literal = set it's innet html to what ever you need and you are good to go!
I have the text body of an email in html, and that email has a link to another webpage in it (so the url is in html). I would like to know if there's a way in C# to "click" on this link and then extract the html information from the page I would be redirected to if I clicked on the link normally.
You can use the System.Net.WebClient to download the html from the url.
var html = new System.Net.WebClient().DownloadString(url);
I am trying to use PDFsharp and MigraDoc to create a PDF report file from many other files.
Basically the workflow is as follows:
The user uploads 2 or more documents to concatenate
The user selects what pages they want to include in the TOC
The documents are concatenated together and a table of contents is generated
Now I am able to easily concatenate the documents and add bookmarks/outlines in PDFsharp. Where I have run into problems is when using MigraDoc to create the TOC referencing the PDFsharp created bookmarks/outlines as the targets of the hyperlinks.
Code to add bookmark/outline in PDFsharp (successfully adds the bookmarks):
// Current document is a PdfDocument and Title is the name of the bookmark
CurrentDocument.Outlines.Add(title, page, true, PdfOutlineStyle.Bold);
MigraDoc code to create the TOC page and render it to the current PDFsharp document:
// Create blank page
PdfPage page = (!hasTitlePage)
? AddPage(null, 0, TOC_BOOKMARK_TITLE) // Add to start
: AddPage(null, 1, TOC_BOOKMARK_TITLE); // Add after title page
// Get Graphics obj
XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.MUH = PdfFontEncoding.Unicode;
// Create MigraDoc document + Setup styles
Document document = new Document();
DefineStyles(document);
// Add header
Section section = document.AddSection();
Paragraph paragraph = section.AddParagraph("Table of Contents");
paragraph.Format.Font.Size = 14;
paragraph.Format.Font.Bold = true;
paragraph.Format.SpaceAfter = 24;
paragraph.Format.OutlineLevel = OutlineLevel.Level1;
// Add links - these are the PdfSharp outlines/bookmarks added previously when concatinating the pages
foreach (var bookmark in CurrentDocument.Outlines)
{
paragraph = section.AddParagraph();
paragraph.Style = "TOC";
Hyperlink hyperlink = paragraph.AddHyperlink(bookmark.Title);
hyperlink.AddText($"{bookmark.Title}\t");
hyperlink.AddPageRefField(bookmark.Title);
}
// Render document
DocumentRenderer docRenderer = new DocumentRenderer(document);
docRenderer.PrepareDocument();
docRenderer.RenderPage(gfx, 1);
return page;
Note - the MigraDoc code adds the TOC page but it doesn't recognize the PDFsharp bookmarks.
Screenshot of the TOC:
The TOC page is added at the start (although the TOC bookmark is listed at the end) but I cannot link to PDFsharp bookmarks
I am certain that the issue is with my understanding of MigraDoc and/or PDFsharp.
First prize would be to only use PDFsharp to add the TOC. I would appreciate any help in being pointed in the right direction.
MigraDoc is a world of its own - it uses PDFsharp to create PDF files, but cannot access internals of PDF files creates with PDFsharp (like Bookmarks in your case).
One approach: create the TOC with PDFsharp (as you already suggested).
Other approach: use MigraDoc to add pages from existing PDF files to a MigraDoc document to create the final PDF (you can add pages from PDF files like you add images).
With the second approach you can use the formatting capabilities of MigraDoc for the TOC and MigraDoc will insert the correct page numbers, provided you add MigraDoc Bookmarks for every image (imported PDF page).
With the first approach, you will have to do the formatting and insert the page numbers yourself, but you'll have more control over the final PDF.
Which is the "best" approach? Depends a bit on the extent of formatting you need for your TOC. With the second approach the TOC can have two or more pages and MigraDoc will take care of that automatically and entries in the TOC can have two or more lines and MigraDoc will also take care of that automatically. But I think a hack will be needed to add the Outlines (e.g. draw very small white text with the outline text somewhere on the page).
Update: For the PDFsharp only approach, you will add links with code like this:
PdfRectangle prect = new PdfRectangle(gfx.Transformer.WorldToDefaultPage(rect));
page.AddDocumentLink(prect, 1);
The second parameter to AddDocumentLink is the target page.
I have a variable inside a script tag in a web page. I want to use the HTML Agility Pack to extract content (e.g. all the css code) that appears before this variable is initialized. I'm able to extract all the style tags on the page using the following code.
string source = new WebClient().DownloadString(url);
var document = new HtmlDocument();
document.LoadHtml(source);
HtmlNode[] styleNodes = document.DocumentNode.SelectNodes("//style").ToArray();
Is there a way I can extract the style tags appearing before and after the variable separately?
Is there a way to get the XML presented in a XMLFormView? I'm trying to create a custom web part with a "Save as PDF" button for an InfoPath form. The idea is to combine the XML with the form's XSL and make the resulting HTML into PDF which is presented to the user as a popup.
Because it is to be presented as a popup, using Workflows is not an option.
http://msdn.microsoft.com/en-us/library/microsoft.office.infopath.server.controls.xmlformview.xmllocation.aspx
This property will give you the Url of the Base XML file. You can read the Stream reader to read this XML.
Take a look at my codeplex project http://ip2html.codeplex.com/
It allows you to generate HTML from the given (InfoPath) XML & (XMLFormView) XSLT.
We ended up using the XmlFormHostItem.NotifyHost method to send HTML to a custom web part in a button-clicked event, which converted the HTML to PDF using Winnovative HTML to PDF converter.
HTML generation from InfoPath code-behind:
var formData = new XmlDocument();
var xslt = new XslCompiledTransform(true);
// Load the form data
formData.LoadXml(MainDataSource.CreateNavigator().InnerXml);
// Extract the stylesheet from the package
xslt.Load(ExtractFromPackage("Print.xsl")); // (uses Template.OpenFileFromPackage(string fileName) to get xsl)
// Perform XSL-transformation
// [...]
// Send HTML to web part
this.NotifyHost(formData.InnerXml);
One drawback of this method is that the NotifyHost event only fires once per form, so if the user clicks 'Save as PDF' and then cancels, he must reload the form in order to be able to save as PDF.