Reputation: 409
I have a problem using Winnovative Html to Pdf Converter v8.0.0.0, my pdf page number are not correct. Here is what I 've done. I set a footer on my converter
pdfConverter.PdfFooterOptions.AddTextArea(new TextArea(500, 0, 50, "&p; / &P;", new Font(new FontFamily("Arial"), 8)));
then I create a document from a url
pdfDocument = pdfConverter.GetPdfDocumentObjectFromUrl(FirstUrl);
then I append a document from a url
pdfDocument.AppendDocument(pdfConverter.GetPdfDocumentObjectFromUrl(SecondUrl));
all the pages resulting from the first url contain a correct page number, but page number on the appended document does not seems to be recalculate
example : FirstUrl result in 3 pages and second in 2 pages. Page Numbers are
1/5
2/5
3/5
1/2
2/2
I want them to be like :
1/5
2/5
3/5
4/5
5/5
I also try not to add the footer to the converter but to the pdfDocument after the conversion like this:
PdfFont pdfFont = pdfDocument.Fonts.Add(new Font(new FontFamily("Arial"), 8));
string strPageNum = "&p; / &P;";
TextElement footerPageNumber = new TextElement(500, 0, 50, strPageNum, pdfFont);
pdfDocument.FooterTemplate.AddElement(footerPageNumber);
But in this case. Only pages from the first Url get page number on them and are nowhere to be seen on page from appended document.
Thanks for your help.
Upvotes: 3
Views: 5224
Reputation: 118
In the latest version of Winovative HTML to PDF converter you can add now headers and footers in external PDF documents appeneded or prepended to the result of conversion. You can find a complete example with C# source code in Add Header and Footer In External PDF Documents demo
Here is the relevant code copied from that demo application:
protected void convertToPdfButton_Click(object sender, EventArgs e)
{
// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
// Set the PDF file to be inserted before conversion result
string pdfFileBefore = Server.MapPath("~/DemoAppFiles/Input/PDF_Files/Merge_Before_Conversion.pdf");
htmlToPdfConverter.PdfDocumentOptions.AddStartDocument(pdfFileBefore, addHeaderFooterInInsertedPdfCheckBox.Checked,
showHeaderInFirstPageCheckBox.Checked, showFooterInFirstPageCheckBox.Checked);
// Set the PDF file to be added after conversion result
string pdfFileAfter = Server.MapPath("~/DemoAppFiles/Input/PDF_Files/Merge_After_Conversion.pdf");
htmlToPdfConverter.PdfDocumentOptions.AddEndDocument(pdfFileAfter, addHeaderFooterInAppendedPdfCheckBox.Checked, true, true);
// Enable header in the generated PDF document
htmlToPdfConverter.PdfDocumentOptions.ShowHeader = true;
// Draw header elements
if (htmlToPdfConverter.PdfDocumentOptions.ShowHeader)
DrawHeader(htmlToPdfConverter, true);
// Enable footer in the generated PDF document
htmlToPdfConverter.PdfDocumentOptions.ShowFooter = true;
// Draw footer elements
if (htmlToPdfConverter.PdfDocumentOptions.ShowFooter)
DrawFooter(htmlToPdfConverter, true, true);
string url = urlTextBox.Text;
// Convert the HTML page to a PDF document and add the external PDF documents
byte[] outPdfBuffer = htmlToPdfConverter.ConvertUrl(url);
// Send the PDF as response to browser
// Set response content type
Response.AddHeader("Content-Type", "application/pdf");
// Instruct the browser to open the PDF file as an attachment or inline
Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Header_Footer_in_External_PDF.pdf; size={0}", outPdfBuffer.Length.ToString()));
// Write the PDF document buffer to HTTP response
Response.BinaryWrite(outPdfBuffer);
// End the HTTP response and stop the current page processing
Response.End();
}
/// <summary>
/// Draw the header elements
/// </summary>
/// <param name="htmlToPdfConverter">The HTML to PDF Converter object</param>
/// <param name="drawHeaderLine">A flag indicating if a line should be drawn at the bottom of the header</param>
private void DrawHeader(HtmlToPdfConverter htmlToPdfConverter, bool drawHeaderLine)
{
string headerHtmlUrl = Server.MapPath("~/DemoAppFiles/Input/HTML_Files/Header_HTML.html");
// Set the header height in points
htmlToPdfConverter.PdfHeaderOptions.HeaderHeight = 60;
// Set header background color
htmlToPdfConverter.PdfHeaderOptions.HeaderBackColor = Color.White;
// Create a HTML element to be added in header
HtmlToPdfElement headerHtml = new HtmlToPdfElement(headerHtmlUrl);
// Set the HTML element to fit the container height
headerHtml.FitHeight = true;
// Add HTML element to header
htmlToPdfConverter.PdfHeaderOptions.AddElement(headerHtml);
if (drawHeaderLine)
{
// Calculate the header width based on PDF page size and margins
float headerWidth = htmlToPdfConverter.PdfDocumentOptions.PdfPageSize.Width -
htmlToPdfConverter.PdfDocumentOptions.LeftMargin - htmlToPdfConverter.PdfDocumentOptions.RightMargin;
// Calculate header height
float headerHeight = htmlToPdfConverter.PdfHeaderOptions.HeaderHeight;
// Create a line element for the bottom of the header
LineElement headerLine = new LineElement(0, headerHeight - 1, headerWidth, headerHeight - 1);
// Set line color
headerLine.ForeColor = Color.Gray;
// Add line element to the bottom of the header
htmlToPdfConverter.PdfHeaderOptions.AddElement(headerLine);
}
}
/// <summary>
/// Draw the footer elements
/// </summary>
/// <param name="htmlToPdfConverter">The HTML to PDF Converter object</param>
/// <param name="addPageNumbers">A flag indicating if the page numbering is present in footer</param>
/// <param name="drawFooterLine">A flag indicating if a line should be drawn at the top of the footer</param>
private void DrawFooter(HtmlToPdfConverter htmlToPdfConverter, bool addPageNumbers, bool drawFooterLine)
{
string footerHtmlUrl = Server.MapPath("~/DemoAppFiles/Input/HTML_Files/Footer_HTML.html");
// Set the footer height in points
htmlToPdfConverter.PdfFooterOptions.FooterHeight = 60;
// Set footer background color
htmlToPdfConverter.PdfFooterOptions.FooterBackColor = Color.WhiteSmoke;
// Create a HTML element to be added in footer
HtmlToPdfElement footerHtml = new HtmlToPdfElement(footerHtmlUrl);
// Set the HTML element to fit the container height
footerHtml.FitHeight = true;
// Add HTML element to footer
htmlToPdfConverter.PdfFooterOptions.AddElement(footerHtml);
// Add page numbering
if (addPageNumbers)
{
// Create a text element with page numbering place holders &p; and & P;
TextElement footerText = new TextElement(0, 30, "Page &p; of &P; ",
new System.Drawing.Font(new System.Drawing.FontFamily("Times New Roman"), 10, System.Drawing.GraphicsUnit.Point));
// Align the text at the right of the footer
footerText.TextAlign = HorizontalTextAlign.Right;
// Set page numbering text color
footerText.ForeColor = Color.Navy;
// Embed the text element font in PDF
footerText.EmbedSysFont = true;
// Add the text element to footer
htmlToPdfConverter.PdfFooterOptions.AddElement(footerText);
}
if (drawFooterLine)
{
// Calculate the footer width based on PDF page size and margins
float footerWidth = htmlToPdfConverter.PdfDocumentOptions.PdfPageSize.Width -
htmlToPdfConverter.PdfDocumentOptions.LeftMargin - htmlToPdfConverter.PdfDocumentOptions.RightMargin;
// Create a line element for the top of the footer
LineElement footerLine = new LineElement(0, 0, footerWidth, 0);
// Set line color
footerLine.ForeColor = Color.Gray;
// Add line element to the bottom of the footer
htmlToPdfConverter.PdfFooterOptions.AddElement(footerLine);
}
}
Upvotes: -1
Reputation: 409
After encountering the same problem with another project. I finally find the correct way of creating a pdf from many Url.
I Still set the footer on the document like this:
pdfConverter.PdfFooterOptions.AddTextArea(new TextArea(500, 0, 50, "&p; / &P;", new Font(new FontFamily("Arial"), 8)));
then I create a document from a url just as before:
pdfDocument = pdfConverter.GetPdfDocumentObjectFromUrl(firstUrl);
but instead of appending another pdf I get the second url in a HtmlToPdfElement like this:
HtmlToPdfElement pdfBody = new HtmlToPdfElement(secoundUrl);
and finally add it to the pdf document using a new page.
PdfPage newPage = pdfDocument.Pages.AddNewPage();
newPage.AddElement(pdfBody);
Upvotes: 0