2

The problem is a header file, which I have to include on each page of the pdf file generated by abcpdf.

The header file contains more than one image file and several lines of text, which varies from case to case.

The problem is that I do not know how to calculate the size of the header. I need to have its size to allocate the rectangle positions to put the rest of html file on each page together with header. I am using C#.

TheAlbear
  • 5,507
  • 8
  • 51
  • 84
Alex Cosic
  • 21
  • 1
  • 2

1 Answers1

2

First off you need to create your document with enough space at the top to allow a header to be added. The settings below are for a normal A4 document with a header of about 1/5 of the page. Remember the coordinates on a PDF are from the bottom right not the top left..

//Setting to create the document using ABCPdf 8
var theDoc = new Doc();
theDoc.MediaBox.String = "A4";

theDoc.HtmlOptions.PageCacheEnabled = false;
theDoc.HtmlOptions.ImageQuality = 101;
theDoc.Rect.Width = 719;
theDoc.Rect.Height = 590;
theDoc.Rect.Position(2, 70);
theDoc.HtmlOptions.Engine = EngineType.Gecko;

The code below out puts a header across each page on the document, with a header image then a colored box under the image with some custom text in.

The header image in this case is 1710 x 381 to keep the resolution of the image as high as possible to stop it looking fuzzy when printed.

private static Doc AddHeader(Doc theDoc)
{
    int theCount = theDoc.PageCount;
    int i = 0;

    //Image header 
    for (i = 1; i <= theCount; i++)
    {
         theDoc.Rect.Width = 590;
         theDoc.Rect.Height = 140;
         theDoc.Rect.Position(0, 706);

         theDoc.PageNumber = i;
         string imagefilePath = HttpContext.Current.Server.MapPath("/images/pdf/pdf-header.png");

         Bitmap myBmp = (Bitmap)Bitmap.FromFile(imagefilePath);
         theDoc.AddImage(myBmp);
     }

     //Blue header box
     for (i = 2; i <= theCount; i++)
     {
         theDoc.Rect.String = "20 15 590 50";
         theDoc.Rect.Position(13, 672);
         System.Drawing.Color c = System.Drawing.ColorTranslator.FromHtml("#468DCB");
         theDoc.Color.Color = c;
         theDoc.PageNumber = i;
         theDoc.FillRect();
     }

     //Blue header text
     for (i = 2; i <= theCount; i++)
     {
         theDoc.Rect.String = "20 15 586 50";
         theDoc.Rect.Position(25, 660);
         System.Drawing.Color cText = System.Drawing.ColorTranslator.FromHtml("#ffffff");
         theDoc.Color.Color = cText;
         string theFont = "Century Gothic";
         theDoc.Font = theDoc.AddFont(theFont);
         theDoc.FontSize = 14;
         theDoc.PageNumber = i;
         theDoc.AddText("Your Text Here");
     }
     return theDoc;
}
TheAlbear
  • 5,507
  • 8
  • 51
  • 84