代码之家  ›  专栏  ›  技术社区  ›  gon250

ABCpdf-为每页添加页边空白,并将页脚保持在一个块中

  •  2
  • gon250  · 技术社区  · 10 年前

    我已经开始将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");
    

    谢谢你抽出时间。

    3 回复  |  直到 8 年前
        1
  •  1
  •   gon250    10 年前

    我找到了一种在顶部和底部获得利润的方法,但这并不完全是我想要的。不管怎样,它是工作的,所以看看下面。

    Css公司:

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

    Html:

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

    并在里面设置所需的边距。

    希望这有帮助。

        2
  •  0
  •   Tineler    10 年前

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

    1: 您可以定义文档中已用空间的大小 doc.Rect 。这对以下所有页面都有效。您还可以在处理过程中设置矩形的新大小。

    2: 为了增加条款和条件,我会打电话 doc.AddImageDoc(template, 1, doc.Rect); 在循环中,在压平页面之前。只需确保调整 文档Rect 预先

        3
  •  0
  •   RabiShankar    6 年前

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

    插图X、插图Y

    doc.Rect.Inset(15, 15);
    

    根据您的需要添加inset中的值。