Itext 7 overriding default margin in pdfHtml

一个人想着一个人 提交于 2019-12-20 06:14:04

问题


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 the pdf page. How do I override this default margin?


回答1:


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:



来源:https://stackoverflow.com/questions/44936451/itext-7-overriding-default-margin-in-pdfhtml

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!