[英]How can I set top margin of the first page of pdf document using iText7?
[英]Add an image at the top of the pdf with 0 margin, however the rest of the body has left and right margin/padding (Itext7, C#)
我想创建一个 pdf,其中包含第一页顶部的 0 边距的公告徽标/图像。 但是我想要正文中内容的空白/边距,如下所示:
我正在使用 IText7 和 C# (.net 4.6.1) 来尝试实现这一目标。 我将文档边距设置为 0,然后添加图像并遍历 html(来自我的所见即所得编辑器)将其分解为元素并将每个元素添加到文档中。 但是,当我这样做时,它也会将正文中内容的边距设置为 0。
有没有办法做到这一点,我可以为我在正文中添加的每个元素添加边距? 还是有什么其他方法? 先感谢您。
这是我的代码:
string bulletinpdf = Server.MapPath("~/generatedfiles/pdf/text.pdf");
PdfWriter writer = new PdfWriter(bulletinpdf);
PdfDocument pdfDoc = new PdfDocument(writer);
pdfDoc.SetDefaultPageSize(PageSize.LETTER);
Document document2 = new Document(pdfDoc);
document2.SetMargins(0, 0, 0, 0); //set document margins to 0 here
string outputpdf = Server.MapPath("~/Components/Images/bulletinheader.jpg");
ImageData imageData = ImageDataFactory.Create(outputpdf);
Image image2 = new Image(imageData);
//image2.SetMargins(0, 0, 0, 0); //i've tried commenting out the above and trying this. to no avail.
document2.Add(image2);
ConverterProperties properties = new ConverterProperties();
IList elements = (IList)HtmlConverter.ConvertToElements(createForm.BulletinText, properties);
foreach (IElement element in elements)
{
document2.Add((IBlockElement) element);
}
document2.Close();
这是 creatForm.BulletinText 的 html(从 CK 内容编辑器中提取的 html)我已经清空了图像元素,但出于复制目的附上了下面的图像:
<p><img alt="" src="" style="float:left; height:400px; width:400px">Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123 </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<table border="1" cellpadding="1" cellspacing="1" style="width:500px">
<tbody>
<tr>
<td>This is a test</td>
<td>This is a test</td>
</tr>
<tr>
<td>This is a test</td>
<td>This is a test</td>
</tr>
<tr>
<td>This is a test</td>
<td>This is a test</td>
</tr>
</tbody>
</table>
您可以通过稍微调整 HTML 来达到预期的目标,您必须编写的所有代码都是几行代码:
FileInfo htmlPath = new FileInfo("C:/Users/x/Desktop/html.html");
HtmlConverter.ConvertToPdf(new FileStream(htmlPath.FullName, FileMode.Open),
new FileStream("C:/Users/x/Desktop/out.pdf", FileMode.OpenOrCreate),
new ConverterProperties().SetBaseUri(htmlPath.Directory.FullName));
现在在您的 HTML 中,您需要添加将包含 header 的<img>
,为其设置 ID 并将其设置为(与 PDF 的宽度匹配):
<img id="header" style="width: 210mm" src="header.jpg"/>
然后我们将文档上的所有左右边距设置为 0(我们将通过将边距设置为<body>
元素来补偿它),并将第一页的上边距设置为 0。我们将徽标图像标记为运行元素并将其包含在第一页的边距框区域。 全部用 CSS 声明方式!
<style>
@page {
margin-left: 0;
margin-right: 0;
}
@page:first {
margin-top: 138pt;
@top-left {
content: element(header);
}
}
#header {
position: running(header);
}
</style>
完整的 HTML(我将表格放在下一页只是为了验证第二页看起来也符合预期):
<head>
<style>
@page {
margin-left: 0;
margin-right: 0;
}
@page:first {
margin-top: 138pt;
@top-left {
content: element(header);
}
}
#header {
position: running(header);
}
</style>
</head>
<body style="margin-left: 36pt; margin-right: 36pt;">
<img id="header" style="width: 210mm" src="header.jpg"/>
<p>Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123Testing 123 </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<table border="1" cellpadding="1" cellspacing="1" style="width:500px; page-break-before: always;">
<tbody>
<tr>
<td>This is a test</td>
<td>This is a test</td>
</tr>
<tr>
<td>This is a test</td>
<td>This is a test</td>
</tr>
<tr>
<td>This is a test</td>
<td>This is a test</td>
</tr>
</tbody>
</table>
</body>
视觉结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.