gpt4 book ai didi

c# - ABCpdf - 为每个页面添加边距顶部并将页脚保持在一个 block 中

转载 作者:太空宇宙 更新时间:2023-11-03 13:10:01 25 4
gpt4 key购买 nike

我已经开始将 abcpdf 用于 C# 应用程序。首先我得到 pdf,然后我得到一个屏幕截图以将其转换为 pdf(我这样做是因为我加载了一些图表)。我有两个问题:

1- 我可以在我的 html 的顶部添加边距,但问题是当我的 html 的大小比一页长时,所以..我有不止一页,我不知道如何添加x 每页顶部和底部边距。

我发现有人在问类似的问题,但他没有得到答案。 ABCpdf, render an HTML within a "template": How to add margin?

2- 我的页脚也有问题,因为我必须始终在底部打印条款和条件,这取决于我的 html 的大小,它打破了措辞障碍,我不希望这样。我必须始终将它打印在底部,并且只打印一个 block 而不是两个。

下面是我实现的代码。

public FileStreamResult HtmlToPdf(Uri pdfUrl, bool landscape, int screenResolutionWidth, bool enableCache)
{
var pdfStream = new MemoryStream();
using (var theDoc = new Doc())
{
if (landscape)
{
// apply a rotation transform
double w = theDoc.MediaBox.Width;
double h = theDoc.MediaBox.Height;
double l = theDoc.MediaBox.Left;
double b = theDoc.MediaBox.Bottom;
theDoc.Transform.Rotate(90, l, b);
theDoc.Transform.Translate(w, 0);
// rotate our rectangle
theDoc.Rect.Width = h;
theDoc.Rect.Height = w;
}
theDoc.HtmlOptions.Engine = EngineType.Gecko;
theDoc.HtmlOptions.PageCacheClear();
theDoc.HtmlOptions.PageCachePurge();
theDoc.HtmlOptions.UseScript = true;
theDoc.HtmlOptions.Timeout = 90000;
theDoc.HtmlOptions.AddLinks = false;

var url = Uri.UnescapeDataString(pdfUrl.ToString());

var pageRef = theDoc.AddImageUrl(url, true, screenResolutionWidth, true);
while (theDoc.Chainable(pageRef))
{
theDoc.Page = theDoc.AddPage();
pageRef = theDoc.AddImageToChain(pageRef);
}
for (var i = 1; i <= theDoc.PageCount; i++)
{
theDoc.PageNumber = i;
theDoc.Flatten();
}

theDoc.Save(pdfStream);
theDoc.Clear();
}
var byteInfo = pdfStream.ToArray();
pdfStream.Write(byteInfo, 0, byteInfo.Length);
pdfStream.Position = 0;
return new FileStreamResult(pdfStream, "application/pdf");

感谢您的宝贵时间。

最佳答案

我找到了一种在顶部和底部获得边距的方法,但这并不是我想要的。无论如何它都在工作,所以请看下面。

CSS:

@media print {
div.header {
top: 0;
}

HTML:

<div class="header"></div>

并在里面设置你想要的边距。

希望对你有帮助。

关于c# - ABCpdf - 为每个页面添加边距顶部并将页脚保持在一个 block 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29346563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com