IText 7覆盖pdfHtml中的默认边距 [英] Itext 7 overriding default margin in pdfHtml

查看:550
本文介绍了IText 7覆盖pdfHtml中的默认边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有必须从页面的绝对左上角开始的html内容.但是,HtmlConverter会自动在pdf页面的顶部和左侧添加0.5英寸的间隙.如何覆盖此默认边距?

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?

推荐答案

您可以使用@page通过CSS设置页面的页边距.以下声明将所有页边距设置为0,并在段落周围绘制边框以供视觉参考:

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>

您将看到该段落的边框触及页面的左侧,但尚未触及页面的顶部.那是因为该段也有一些先天的空白作为阻碍.将此设置为0将达到目的:

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;
}

输出:

这篇关于IText 7覆盖pdfHtml中的默认边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆