【问题标题】:ABCpdf - Add margin-top to each page and keep the footer in a blockABCpdf - 将页边距添加到每个页面并将页脚保持在一个块中
【发布时间】:2015-06-03 11:44:39
【问题描述】:

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

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

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

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

下面是我实现的代码。

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

感谢您的宝贵时间。

【问题讨论】:

    标签: c# pdf-generation abcpdf


    【解决方案1】:

    除了@Tinelers 注释之外,您还可以设置以下值以在所有页面上固定页眉和页脚边距

    插图X,插图Y

    doc.Rect.Inset(15, 15);
    

    根据您的需要在插图中添加值。

    【讨论】:

      【解决方案2】:

      我能够在你提到的帖子中解决我的问题。

      1:您可以将文档中已用空间的大小定义为doc.Rect。这适用于所有后续页面。您还可以在处理过程中为矩形设置新大小。

      2:为了添加条款和条件,我会在您的循环中调用doc.AddImageDoc(template, 1, doc.Rect);,然后再展平页面。只需确保事先调整 doc.Rect 的大小和位置即可。

      【讨论】:

        【解决方案3】:

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

        CSS:

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

        HTML:

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

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

        希望对你有帮助。

        【讨论】:

        • 这帮助我添加了边距。但是,页边距顶部/底部边距似乎不会将页面应用于页面,而只会影响第一页(顶部边距)和最后一页(底部边距)。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-17
        • 2022-11-23
        • 1970-01-01
        • 1970-01-01
        • 2016-01-05
        • 1970-01-01
        • 2019-05-13
        相关资源
        最近更新 更多