0

Why I am getting border on bottom and right when rendering html using abcAdf. PS I got professional license using (ABCpdf9-64.dll)

My process is to create page:

  1. I use master / child layout template in mvc (works)
  2. I point to this html using abcPdf to convert into pdf

Html rendered

When my html gets rendered (plain) format : each originating from its respective location.

<h1>Layout</h1>
<p>
    content
</p>

C# code to render pdf

My code is as follows:

        using (var pdf = new Doc())
        {
            pdf.HtmlOptions.Timeout = 600000;

            pdf.HtmlOptions.AddTags = true;
            pdf.Page = pdf.AddPage();

            var id = pdf.AddImageUrl(url, true, 1024, true);
            if (allowPaging)
            {
                while (true)
                {
                    if (!pdf.Chainable(id))
                    {
                        break;
                    }

                    pdf.Page = pdf.AddPage();
                    id = pdf.AddImageToChain(id);
                }

                for (int i = 1; i <= pdf.PageCount; i++)
                {
                    pdf.PageNumber = i;
                    pdf.Flatten();
                }

                ////reset back to page 1 so the pdf starts displaying there
                if (pdf.PageCount > 0)
                {
                    pdf.PageNumber = 1;
                }
            }

            return store(pdf);
        }

Output

My text/html gets rendered ok but I get borders that I have not asked for.

Rendered output:

enter image description here

Please note the hairline in bottom of the image.

Community
  • 1
  • 1
cpoDesign
  • 8,953
  • 13
  • 62
  • 106

1 Answers1

1

After hours of googling about setting margin in abcpdf, I found nothing and took it as a challenge to find it myself. I tried experimenting with everything I found relevant in the abcpdf documentation and finally made a chart myself for setting the margins. I have successfully implemented this in many situations. Hope this helps others.

Here is a code snippet that shows how to set margins-

string html; // my html content that should be shown in the pdf page

            Doc pdf = new Doc();

            // adjust the default rotation and save
            double w = pdf.MediaBox.Width;
            double h = pdf.MediaBox.Height;
            double l = pdf.MediaBox.Left;
            double b = pdf.MediaBox.Bottom;

            // explicitly giving page size
            pdf.MediaBox.String = "A4";
            pdf.Transform.Rotate(90, l, b);
            pdf.Transform.Translate(w, 0);
            pdf.Rect.Width = h;
            pdf.Rect.Height = w;

            int theID1 = pdf.GetInfoInt(pdf.Root, "Pages");
            pdf.SetInfo(theID1, "/Rotate", "90");

            int theID;
            pdf.Rect.String = "17 55 823 423";
            theID = pdf.AddImageHtml(html.ToString()); //Writes the HTML image to PDF

Here is a picture that describes margin layout for some junk values. Here 20 suggests that your content starts 20 pixels away from left of your pdf page 770 suggests that your content ends 770 pixels away from left of your pdf page 75 suggests that your content starts 55 pixels above the bottom of your pdf page 600 suggests that your content ends 600 pixels above the bottom of your pdf page

abcpdf margin chart by CoffeeSipper

In your case you have to add

pdf.Rect.String = "20 75 770 600"; // giving junk values

right before

var id = pdf.AddImageUrl(url, true, 1024, true);

NOTE: In this example I explicitly set landscape mode instead of portrait mode. But orientation doesn't matter for setting margins.

CoffeeSipper
  • 219
  • 2
  • 12