2
votes

I have started to use abcpdf for a c# application. First I get the pdf and then I get a screenshot to convert it in pdf(I do it because I load some charts). I have 2 problems:

1- I can add margin to the top of my html but the problem is when I the size of my html is longer than a page so.. I have more than one page and I don't know how can I add x margin per page in the top and in the bottom.

I found a guy asking something similar but he didn't get an answer. ABCpdf, render an HTML within a "template": How to add margin?

2- Also I have a problem with the footer because I have to print terms and conditions always in the bottom and depends on the size of my html it's breaking the block of wording and I don't want that. I have to print it always in the bottom and in only one block not in two.

Below is the code I have implemented.

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

Thanks for your time.

3

3 Answers

1
votes

I found a way to get margin in the top and in the bottom but it's not exactly what I was looking. Anyway it's working so take a look below.

Css:

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

Html:

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

And set the margin you want inside.

hope it's helps.

0
votes

I was able to solve my problem in the post you mentioned.

1: You can define the size of the used space in your document to doc.Rect. This is valid for all following pages. You are also able to set a new size to the rect during processing.

2: For adding the terms and conditions I would call doc.AddImageDoc(template, 1, doc.Rect); inside your loop, before you flatten a page. Just ensure, that you adjust the size and position of the doc.Rect beforehand.

0
votes

In addition to @Tinelers comment yo can set the below value to have fix header and footer margin on all pages

insetX, insetY

doc.Rect.Inset(15, 15);

add the values in inset as per your needs.