Itext 7 overriding default margin in pdfHtml

后端 未结 1 1201
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-15 05:32

I have html content that has to start at the absolute top left corner of the page. However, the HtmlConverter automatically adds a 0.5in gap from the top and left side of th

相关标签:
1条回答
  • 2021-01-15 06:23

    You can set the margins of a page through CSS using @page. The following declaration sets all the page-margins to 0, as well as draws a border around paragraphs for visual reference:

    @page{
        margin:0pt;
    }
    p{
        border-left: solid 2pt blue;
        border-top: solid 1pt blue;
        border-bottom: solid 1pt blue;
    }
    

    Use the following as input:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>margin example</title>
        <link href="margin.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div>
            <p>This page should have the margins set at 0</p>
        </div>
    </body>
    </html>
    

    And you'll see that the paragraph's border is touching the left side of the page, but not yet the top side. That's because the paragraph has some innate margin as a blockelement as well. Setting this to 0 will do the trick:

    p{
        border-left: solid 2pt blue;
        border-top: solid 1pt blue;
        border-bottom: solid 1pt blue;
        margin-top: 0pt;
    }
    
    @page{
        margin:0pt;
    }
    

    And the output:

    0 讨论(0)
提交回复
热议问题