ABCpdf, render an HTML within a “template”: How to add margin?

限于喜欢 提交于 2019-12-25 02:58:12

问题


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

I see, that there is a possibility to pass a Rect to #AddImageDoc. But I don't have this option for #AddImageUrl.


回答1:


Here is how I could solve the problem:

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;

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

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


来源:https://stackoverflow.com/questions/29306527/abcpdf-render-an-html-within-a-template-how-to-add-margin

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