Set margins, headers and footers for PDF without overlapping

I need help setting up my PDF header / footer next to my text areas. The first page looks fine, and from there it gets even worse. Is the header and footer in my existing spaces?

I would like to know what is going wrong and what I can configure to install below:

  • Page Widths
  • Field width
  • Headline
  • Footer
  • Text area

margins

Pdf

My header override function looks like this:

public partial class Header : PdfPageEventHelper
{
public override void OnStartPage(PdfWriter writer, Document doc)
    {
    PdfPTable headerTbl = new PdfPTable(2);
    headerTbl.SetWidths(new float[] { 4, 1 });
    headerTbl.TotalWidth = doc.PageSize.Width;

    iTextSharp.text.Image logo = iTextSharp.text.Image.GetInstance(HttpContext.Current.Server.MapPath("~/Images/view.gif"));
    logo.ScalePercent(5);
    PdfPCell cell = new PdfPCell(logo);
    cell.HorizontalAlignment = Element.ALIGN_RIGHT;
    cell.PaddingRight = 20;
    cell.Border = Rectangle.NO_BORDER;


    Font timesH = new Font(Font.FontFamily.TIMES_ROMAN, 20);
    Font times = new Font(Font.FontFamily.TIMES_ROMAN, 10);
    Chunk c1= new Chunk("THIS IS MY HEADER TEXT", timesH);
    Chunk c = new Chunk("\n", times);
    Chunk c2=new Chunk("PLEASE HAVE A NICE DAY", times);
    Phrase p = new Phrase();
    p.Add(c1);
    p.Add(c);
    p.Add(c2);
    PdfPCell cell2 = new PdfPCell(p);
    cell2.Border = Rectangle.NO_BORDER;

    headerTbl.AddCell(cell2);
    headerTbl.AddCell(cell);

    headerTbl.WriteSelectedRows(0, -1, 0, (doc.PageSize.Height - 10), writer.DirectContent);

    }
}

stringWrite is a StringWriter containing a bunch of data. More clarity is HERE .

I create pdf as follows:

    StringReader sr = new StringReader(stringWrite.ToString());
    Document pdfDoc = new Document(new Rectangle(288f, 144f), 10f, 10f, 30f, 30f);
    pdfDoc.SetPageSize(PageSize.A4.Rotate());
    HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
    PdfWriter pdfwriter = PdfWriter.GetInstance(pdfDoc, HttpContext.Current.Response.OutputStream);
    pdfwriter.PageEvent = new Footer();
    pdfwriter.PageEvent = new Header();
    pdfDoc.Open();
    htmlparser.Parse(sr);
    pdfDoc.Close();
    HttpContext.Current.Response.Write(pdfDoc);
    HttpContext.Current.Response.End();

I am using iTextSharp, C #, Asp.net in my application.

+4
1

Document pdfDoc = new Document(new Rectangle(288f, 144f), 10f, 10f, 30f, 30f);

, 10 , , 30 - , . .

, , , .

, , 20 , - 10 . , 30 ( , , ). :

headerTbl.WriteSelectedRows(0, -1, 0, (doc.PageSize.Height - 10), writer.DirectContent);

, 10 . , , 30. , , 10 .

, 20 ( ):

Document pdfDoc = new Document(new Rectangle(288f, 144f), 10f, 10f, 50f, 30f);

, , , HTML ( , HTMLWorker).

:

  • OnStartPage . OnEndPage , , , ,...

  • HTMLWorker . XMLWorker.

  • , ( new Document), ?

+8

Source: https://habr.com/ru/post/1538001/


All Articles