ABCpdf,在“模板"中呈现 HTML:如何添加边距? [英] ABCpdf, render an HTML within a "template": How to add margin?
问题描述
我正在尝试在预定义的 PDF 模板内(例如在框架内)呈现 HTML.模板/框架应该到达边缘.但是 HTML 不应该这样做.所以我只需要 HTML 的某种边距.到目前为止,这是我的代码:
I'm trying to render an HTML within a predefined PDF-template (e.g. within a frame.) The template/frame should reach the edges. But the HTML shouldn't do that. So I need some kind of margin for the HTML only. Here is my code so far:
var doc = new Doc();
doc.MediaBox.String = "A4";
doc.Rect.String = doc.MediaBox.String;
var id = doc.AddImageUrl(url.ToString());
doc.AddImageDoc("template.pdf", 1, doc.MediaBox);
while (doc.Chainable(id))
{
doc.Page = doc.AddPage();
id = doc.AddImageToChain(id);
doc.AddImageDoc("template.pdf", 1, doc.MediaBox);
}
for (var i = 1; i <= doc.PageCount; i++)
{
doc.PageNumber = i;
doc.Flatten();
}
我明白了,有可能将一个 Rect 传递给 #AddImageDoc
.但是对于 #AddImageUrl
,我没有这个选项.
I see, that there is a possibility to pass a Rect to #AddImageDoc
. But I don't have this option for #AddImageUrl
.
推荐答案
以下是我解决问题的方法:
Here is how I could solve the problem:
首先,我设置doc.Rect
的位置和边距:
First, I set the position and margins of the doc.Rect
:
doc.Rect.Position(15, 15);
doc.Rect.Width = pageWidth - 2*15;
doc.Rect.Height = pageHeight - 2*15;
然后我用解析后的 URL 中的图像填充文档:
Then I filled the doc with the images from the parsed URL:
var id = doc.AddImageUrl(url.ToString());
while (doc.Chainable(id))
{
doc.Page = doc.AddPage();
id = doc.AddImageToChain(id);
}
此后,我将 doc.Rect
重置为实际纸张的大小(在 ma 情况下:A4):
After this, I reset the doc.Rect
to the size of the actual paper (in ma case: A4):
doc.Rect.String = "A4";
现在我可以遍历所有页面并将模板添加到它们中:
Now I can loop over all pages and add the template to them:
for (var i = 1; i <= doc.PageCount; i++)
{
doc.PageNumber = i;
doc.AddImageDoc(template, 1, doc.Rect);
doc.Flatten();
}
这篇关于ABCpdf,在“模板"中呈现 HTML:如何添加边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!