Welcome Guest Search | Active Topics | Sign In | Register

Header convering over content Options
Intellicom
Posted: Monday, February 17, 2014 9:50:25 PM
Rank: Member
Groups: Member

Joined: 2/10/2014
Posts: 11
I'm adding a header to my pages with the following code.


Code: Visual Basic.NET
Private Sub OnAfterRenderPage(sender As Object, e As PdfPageEventArgs)
        'Use ACM to output a page header on the
        'page. See ACM documentation for more
        'information on how to use ACM
        Dim render As New AcmRender(e.Page, 0.0F, New AcmPageLayout(New AcmPadding(1, 0.5F, 1, 0.5F)))
            'Set the output area to the top portion of the page. Note
            'this does not change the output area of the original
            'conversion from which we are called
            EO.Pdf.HtmlToPdf.Options.OutputArea = New RectangleF(0, 0, 8.5F, 2.0F)
            'Render an image and a horizontal line. Note the
            'second argument is the PdfPage object
            EO.Pdf.HtmlToPdf.ConvertHtml(vbCr & vbLf & _
                "    <img style='height: 100%;width: 100%;' src='https://roofspec.nenesinc.com/images/header.png' >" & vbCr & vbLf & _
                "    <br />", _
                 e.Page)
            render.Render(text)

    End Sub


The problem I have is that on all the pages, the content I'm rendering is getting covered up by the header. How do I push the content down on each page so it allows room for the header? Here's how I'm putting content in.

Code: Visual Basic.NET
Dim contents As String = File.ReadAllText(Server.MapPath("test.html"))
EO.Pdf.Runtime.AddLicense(Resources.Resources.EopdfKey)
        HtmlToPdf.Options.AfterRenderPage = New PdfPageEventHandler(AddressOf OnAfterRenderPage)
        
        'Zoom out slightly to fit more contents on one page
        HtmlToPdf.Options.FirstPageNumber = 1
        HtmlToPdf.Options.ZoomLevel = 0.8F
        HtmlToPdf.ConvertHtml(contents, fileName)

        Dim resp = System.Web.HttpContext.Current.Response
        resp.Clear()
        resp.ContentType = "text/pdf"
        resp.AddHeader("Content-Disposition", "attachment; filename=" & stamp & ";")
        resp.TransmitFile(fileName)
        resp.Flush()
        resp.End()
eo_support
Posted: Monday, February 17, 2014 9:58:31 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi,

You would push the contents down by setting HtmlToPdf.Options.OutputArea. Note that this is the same property you are setting inside your AfterRender handler. However these are two different values and they won't interfere with each other.

Thanks!
Intellicom
Posted: Tuesday, February 18, 2014 9:59:22 AM
Rank: Member
Groups: Member

Joined: 2/10/2014
Posts: 11
So I cannot, for the life of me, figure out how to get that content pushed down. Here is the code I have that generates the pdf.

Code: Visual Basic.NET
Private Sub OnAfterRenderPage(sender As Object, e As PdfPageEventArgs)
        'Use ACM to output a page header on the
        'page. See ACM documentation for more
        'information on how to use ACM
        Dim render As New AcmRender(e.Page, 0.0F, New AcmPageLayout(New AcmPadding(1, 0.5F, 1, 0.5F)))
        If (e.Page.Index > 0) Then
            Dim text As New AcmText(String.Format("Page {0}", e.Page.Index + 1))

            'Set the output area to the top portion of the page. Note
            'this does not change the output area of the original
            'conversion from which we are called
            EO.Pdf.HtmlToPdf.Options.OutputArea = New RectangleF(0, 0, 8.5F, 2.0F)
            'Render an image and a horizontal line. Note the
            'second argument is the PdfPage object
            EO.Pdf.HtmlToPdf.ConvertHtml(vbCr & vbLf & _
                "    <img style='height: 100%;width: 100%;' src='https://roofspec.nenesinc.com/images/header.png' >" & vbCr & vbLf & _
                "    <br />", _
                 e.Page)

            render.Render(text)
        Else

            Dim text As New AcmText("")

            'Set the output area to the top portion of the page. Note
            'this does not change the output area of the original
            'conversion from which we are called
            EO.Pdf.HtmlToPdf.Options.OutputArea = New RectangleF(0, 0, 8.5F, 2.0F)
            'Render an image and a horizontal line. Note the
            'second argument is the PdfPage object
            EO.Pdf.HtmlToPdf.ConvertHtml(vbCr & vbLf & _
                "    <img style='height: 100%;width: 100%;' src='https://roofspec.nenesinc.com/images/header.png' >" & vbCr & vbLf & _
                "    <br />", _
                 e.Page)
            render.Render(text)
        End If 'if page index > 0



    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim contents As String = File.ReadAllText(Server.MapPath("pdftest.html"))
        Dim stamp = Today.Year & "-" & Today.Month & "-" & Now.Day & "-" & Now.Hour & "-" & Now.Minute & ".pdf"

        Dim fileName = Server.MapPath("~/BidGeneration/Contracts/" & stamp)

        If (System.IO.File.Exists(fileName)) Then
            stamp = Today.Year & "-" & Today.Month & "-" & Now.Day & "-" & Now.Hour & "-" & Now.Minute & "-" & Now.Millisecond & ".pdf"
            fileName = Server.MapPath("~/BidGeneration/Contracts/" & stamp)
        End If

        EO.Pdf.Runtime.AddLicense(Resources.Resources.EopdfKey)
        HtmlToPdf.Options.AfterRenderPage = New PdfPageEventHandler(AddressOf OnAfterRenderPage)
        
        'Zoom out slightly to fit more contents on one page
        HtmlToPdf.Options.FirstPageNumber = 1
        'HtmlToPdf.Options.ZoomLevel = 0.8F
        HtmlToPdf.Options.OutputArea = New RectangleF(0.8F, 0.5F, HtmlToPdf.Options.PageSize.Width - 1.0F, HtmlToPdf.Options.PageSize.Height - 2.5F)
        HtmlToPdf.ConvertHtml(contents, fileName)
        Dim resp = System.Web.HttpContext.Current.Response
        resp.Clear()
        resp.ContentType = "text/pdf"
        resp.AddHeader("Content-Disposition", "attachment; filename=" & stamp & ";")
        resp.TransmitFile(fileName)
        resp.Flush()
        resp.End()
    End Sub


All I'm converting right now is just an ordered list, so I know where the content is. The pdftest.html page is located here
http://roofspec.nenesinc.com/pdftest.html and you can actually generate the pdf to look at it by going here http://roofspec.nenesinc.com/pdftest.aspx. Any help would really be appreciated. I just want that content to go under the header on every page.
eo_support
Posted: Tuesday, February 18, 2014 10:05:42 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi,

You have three pieces of output:

A. In your page load, you set HtmlToPdf.Options.OutputArea to (0.8, 0.5, ....), that means your main contents will start at 0.5 inch from the top edge of the paper (the first number is the left margin and the second number is the top margin.

In your after render event, you have rendered two pieces of information:

B. An image using ConvertUrl. Here you set HtmlToPdf.Options.OutputArea to (0.8, 0, ....), so this image starts from the top edge of the paper;
C. You also use AcmRender to render text. For the AcmRender, you set its padding to (1, 0.5, ...), so your text "Page x" will be rendered at 0.5 inch from the top edge of the paper;

Here A and C both starts at 0.5 inch from the top of the paper. So obviously they will overlap each other.

Thanks!
Intellicom
Posted: Tuesday, February 18, 2014 10:08:24 AM
Rank: Member
Groups: Member

Joined: 2/10/2014
Posts: 11
Yup, I figured that out. This did the trick. Thanks!
HtmlToPdf.Options.OutputArea = New RectangleF(0.5F, 2.2F, 8.0F, 7.5F)
BDM_1955
Posted: Friday, March 28, 2014 11:58:49 AM
Rank: Newbie
Groups: Member

Joined: 3/28/2014
Posts: 1
Hello,
I am having an issue with headers and footers overlapping content. Here is my setup:

- A user can customize a header, footer, and set the margins themselves (in inches)
- The user then selects which header, footer, and margins they would like to use (NOTE: the header and footer can be text or images)

The top of the header should start after the top margin, the bottom of the footer should end before the bottom margin, etc. I use the HtmlToPdfOptions.HeaderHtmlFormat to set the header html, and the .FooterHtmlFormat to set the footer html. My issue is that i need to figure out where to start the content of the PDF. I know I need to use the HtmlToPdfOptions.OutputArea to set the content area, but I need to know how tall the header and footer are. Right now I am creating separate documents for the header and the footer, placing the content for each into their respective object, and then figuring out the HtmlToPdfResult.LastPosition to figure out the height of each.

Code: Visual Basic.NET
'CREATE MARGIN OBJECTS
Dim topMargin As Double
Dim bottomMargin As Double
Dim leftMargin As Double
Dim rightMargin As Double

'CREATE HEADER AND FOOTER STRING OBJECTS
Dim headerHTML As String
Dim footerHTML As String

'CREATE PDF DOCUMENT OBJECTS
Dim doc As New EO.Pdf.PdfDocument
Dim headerDoc As New EO.Pdf.PdfDocument
Dim footerDoc As New EO.Pdf.PdfDocument

'ADD LICENSE
EO.Pdf.Runtime.AddLicense([LICENSE])

'CREATE OPTIONS OBJECT
Dim options As New EO.Pdf.HtmlToPdfOptions

'CREATE STRING OBJECTS WITH THE HEADER AND FOOTER HTML AS VALUES
Dim invisibleHeader As String = "<div style=""visibility:hidden;"">" & headerHTML  & "</div>"
Dim invisibleFooter As String = "<div style=""visibility:hidden;""><br>" & footerHTML & "</div>"

'CREATE HtmlToPdfResult OBJECTS FOR THE HEADER AND FOOTER SO WE CAN SEE HOW TALL THEY ARE ON PAPER
Dim headerResult As EO.Pdf.HtmlToPdfResult = EO.Pdf.HtmlToPdf.ConvertHtml(invisibleHeader, headerDoc, options)
Dim footerResult As EO.Pdf.HtmlToPdfResult = EO.Pdf.HtmlToPdf.ConvertHtml(invisibleFooter, footerDoc, options)

'SET THE HEADER AND FOOTER FOR THE REAL PDF DOCUMENT
options.HeaderHtmlFormat = headerHTML
options.FooterHtmlFormat = footerHTML

'THE HEADER SHOULD START AFTER THE TOP MARGIN
options.HeaderHtmlPosition = CSng(topMargin)

'THE FOOTER SHOULD END BEFORE THE BOTTOM MARGIN
options.FooterHtmlPosition = CSng(11 - bottomMargin  - footerResult.LastPosition)

'IF A LETTERHEAD IS NOT SELECTED, DEFAULT TO 1" MARGINS
If hdnLetterheadId.Value = "0" Or hdnLetterheadId.Value = "-1" Then 
     options.OutputArea = New System.Drawing.RectangleF(1, 1, 6.5, 9)
Else 'CALCULATE THE OUTPUT AREA BASED ON THE MARGINS, HEADER HEIGHT, AND FOOTER HEIGHT
     options.OutputArea = New System.Drawing.RectangleF(CSng(leftMargin), headerResult.LastPosition + CSng(topMargin) + (CSng(0.25) *      headerResult.LastPosition), CSng((8.5 - leftMargin - rightMargin)), CSng((11 - (headerResult.LastPosition + topMargin + (CSng(0.25) *    headerResult.LastPosition)) - (footerResult.LastPosition + bottomMargin))))
End If

Dim pdfResult As EO.Pdf.HtmlToPdfResult = EO.Pdf.HtmlToPdf.ConvertHtml(BodyContent, doc, options)


The problem I am encountering is that on paper the footer will be about 1", but the .LastPosition returns .54". The header has a similar problem to a lesser extent. I'm guessing there is an easier way to accomplish this but I cannot, for the life of me, figure it out. Is there something I am missing or incorrectly using?

I forgot to mention the header and footer need to repeat on every page. Ultimately, the user will have the same letter with a different salutation (i.e. name) on each page. For example, the user has customers and they want to send the same letter to each customer. The PDF will be the letter with a dynamic salutation field (kind of like a mail merge).

Thanks in advance
eo_support
Posted: Friday, March 28, 2014 11:11:55 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,196
Hi,

That's probably because of the auto scaling. See here for more details:

http://www.essentialobjects.com/doc/4/htmltopdf/auto_fit.aspx

As a test, you can try to set HtmlToPdf.Options.AutoFitX an AutoFitY to None and see what value you get.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.