Add header on all pages of PDF document in C# using Aspose.PDF for .NET - content overlapping issue

Hi there,
I need to add header on all the pages of existing pdf. I tried following code but the header overlapped the content of the PDF. Is there any way to shrink original content or any other option such that it is not overlapped with header?

  Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(dataDir + "input.pdf"); 
  foreach (Page page in pdfDocument.Pages)
            {
			// Create a Header Section of the PDF file
                Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter();
            // Set the Header for the PDF file
            page.Header = header;
            // Set the top margin for the header section
            header.Margin.Top = 20;
				...
				...
				...
				...

            // Instantiate a table object
            Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
            // Save the Pdf file
            pdfDocument.Save(dataDir + "sample.pdf");
          
        }

@RD1,

Kindly send us your source PDF and the complete code. We will investigate and share our findings with you. Your response is awaited.

Hi @imran.rafique,
Below is the sample code, also attached AsposeHeaderOverlap.zip (645.8 KB)
is the working sample.

PDF can be any scanned document, plain form or text with header

        Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(dataDir + "sample.pdf");
        foreach (Page page in pdfDocument.Pages)
        {
            Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter();
            page.Header = header;
            header.Margin.Top = 20;
            Aspose.Pdf.Table tab1 = new Aspose.Pdf.Table();
            header.Paragraphs.Add(tab1);
            tab1.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.1F);
            tab1.ColumnWidths = "200 200";
            Aspose.Pdf.Row row1 = tab1.Rows.Add();
            row1.MinRowHeight = 100;
            row1.BackgroundColor = Color.Gray;
            row1.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;

            row1.Cells.Add("column 1");
            row1.Cells.Add("column 2");
            
        }
        // Save the Pdf file
        pdfDocument.Save(dataDir + "sample_out.pdf");

@RD1,

You can adjust the vertical header size by modifying the Top margin and min row height of the table. Please change the following lines of code:

[C#]

header.Margin.Top = 5;
row1.MinRowHeight = 40;

This is the output PDF file: sample_out.pdf (279.1 KB)

HI @imran.rafique ,
Thanks for your response. As I mentioned initially that PDF can be a scanned document which means content could start right from the top. In that case, any header will cover the content no matter what margin or row height we give.

Is it possible to shrink the content before adding the header so that whole content of the page from original pdf fit between header and footer instead of starting from the top of the page?

@RD1,

You can resize the page content ContentsResizeParameters class instance. Please try the following code example: Resize Page Contents of Specific Pages in a PDF file