We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Larger Repeating Header

I was wondering how was the best way to do this.

I want to dynamically create a PDF, of an unknown length, but at the top of each page is a multi line header.

first row (left side - text and a date, right corner page # of Total #)
second Rpw - Larger Page Title centered red
third Row - Secondary Title centered
4th row horizontal rule.

This seems to be a little more involved than a text stamp.

This section would repeat a top every page

I am hoping this can be done directly in the PDF or other wise, the only other thing I can think of would be to do this as a word document and then save to pdf

We have an Aspose Total License.

Thanks

@dlaskey

If you decide to work with Aspose.PDF, you can achieve it by adding a table in the header of every page in the PDF. Please check below articles to add table in the PDF and let us know in case you face any issues:

I have seen the examples, and in principal am able to do what you suggest, but I run into overall issues.

The size of what I am trying to create as a header is about 10% of the page. I am able to create the header I want which has 3 rows and has some larger title fonts, but the problem I get is overlap as I believe table is larger than the general amount allocated by the Header of for the PDF so when I then add paragraphs to the PDF, I have overlap of the header table and the earlier paragraphs on both pages.

Is there a way to specify/force the height of the header?

I am able to create the header, and it repeats as I want, but I need to get rid of the overlap.

I am only creating a single page and then adding paragraphs of text which will be coming from a data set and I do not know at run time whether there are 3 short paragraphs of 30 long ones.

I think I am almost there if I can set the size of the header.

I have tried adding the table to the header and the header to the page before adding the paragraphs as well as trying to add at the end before saving, but neither has produced different results.

@dlaskey

Can you please share the complete sample code snippet which you have tried so far along with sample PDF document? We will test the scenario in our environment and address it accordingly.

        Aspose.Pdf.Document document = new Aspose.Pdf.Document();
        document.PageInfo.Height = Aspose.Pdf.PageSize.PageLetter.Height;
        document.PageInfo.Width = Aspose.Pdf.PageSize.PageLetter.Width;

        Aspose.Pdf.Page page = document.Pages.Add();
        Aspose.Pdf.HeaderFooter header = new Aspose.Pdf.HeaderFooter();

        // Initializes a new instance of the Table
        Aspose.Pdf.Table hdrTable = new Aspose.Pdf.Table
        {
            // Set the table border color as LightGray
            //Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Color.Black),
            // Set the border for table cells
            //DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, .5f, Color.Black)
            
        };

        hdrTable.HorizontalAlignment = HorizontalAlignment.Center;
 
        hdrTable.ColumnWidths = "400";
        Aspose.Pdf.MarginInfo marginHDR = new Aspose.Pdf.MarginInfo();
        marginHDR.Top = 10f;
        marginHDR.Left = 1f;
        marginHDR.Right = 1f;
        marginHDR.Bottom = 10f;


        Aspose.Pdf.Row hdrrow1 = hdrTable.Rows.Add();
        hdrrow1.Cells.Add("Date Submitted: 08 / 25 / 20").Alignment = HorizontalAlignment.Left;
        hdrrow1.DefaultCellPadding = marginHDR;

        Aspose.Pdf.Text.TextFragment alertTitle = new Aspose.Pdf.Text.TextFragment("Alert Page");
        alertTitle.TextState.FontSize = 30;
        alertTitle.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("TimesNewRoman");
        alertTitle.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
        alertTitle.TextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;

        Aspose.Pdf.Row hdrrow2 = hdrTable.Rows.Add();
        hdrrow2.Cells.Add(alertTitle.Text).Alignment = HorizontalAlignment.Center;
        hdrrow2.DefaultCellTextState.FontSize = 30;
        hdrrow2.DefaultCellTextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("TimesNewRoman");
        hdrrow2.DefaultCellTextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red);
        hdrrow2.DefaultCellPadding = marginHDR;

        Aspose.Pdf.Text.TextFragment protocolTitle = new Aspose.Pdf.Text.TextFragment("DF / HCC Protocol #: ");
        protocolTitle.TextState.FontSize = 24;
        protocolTitle.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("TimesNewRoman");
        protocolTitle.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black);
        protocolTitle.TextState.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
        protocolTitle.TextState.Underline = true;

        Aspose.Pdf.Text.TextFragment protocolNumber = new Aspose.Pdf.Text.TextFragment("16-999");
        protocolNumber.TextState.FontSize = 24;
        protocolNumber.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("TimesNewRoman");
        protocolNumber.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Blue);
        protocolTitle.Text = protocolTitle.Text + protocolNumber.Text;

        Aspose.Pdf.Row hdrrow3 = hdrTable.Rows.Add();
        hdrrow3.Cells.Add(protocolTitle.Text).Alignment = HorizontalAlignment.Center;
        hdrrow3.Cells.Add(alertTitle.Text).Alignment = HorizontalAlignment.Center;
        hdrrow3.DefaultCellTextState.FontSize = 24;
        hdrrow3.DefaultCellTextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("TimesNewRoman");
        hdrrow3.DefaultCellTextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black);
        hdrrow3.DefaultCellPadding = marginHDR;


        Aspose.Pdf.Table catHeader = new Aspose.Pdf.Table();

        catHeader.ColumnWidths = "400";
        catHeader.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.3F);
        catHeader.DefaultCellTextState.FontSize = 14;
        catHeader.DefaultCellTextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;

        catHeader.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 1F);
        Aspose.Pdf.MarginInfo margin = new Aspose.Pdf.MarginInfo();
        margin.Top = 5f;
        margin.Left = 5f;
        margin.Right = 5f;
        margin.Bottom = 5f;
        catHeader.DefaultCellPadding = margin;
        Aspose.Pdf.Row row1 = catHeader.Rows.Add();
        row1.Cells.Add("Safety / Drug(includes preparation, administration, dose modifications,equations)");


        Aspose.Pdf.Table clarHeader = new Aspose.Pdf.Table();

        clarHeader.ColumnWidths = "400";
        clarHeader.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.3F);
        clarHeader.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 1F);
        clarHeader.DefaultCellTextState.FontSize = 14;
        clarHeader.DefaultCellTextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;

        clarHeader.DefaultCellPadding = margin;
        Aspose.Pdf.Row clarRow = clarHeader.Rows.Add();
        clarRow.Cells.Add("Protocol Clarifications(non-drug related e.g.eligibility criteria, study assessments)");

        Aspose.Pdf.Table treatHeader = new Aspose.Pdf.Table();

        treatHeader.ColumnWidths = "400";
        treatHeader.DefaultCellBorder = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 0.3F);
        treatHeader.Border = new Aspose.Pdf.BorderInfo(Aspose.Pdf.BorderSide.All, 1F);
        treatHeader.DefaultCellTextState.FontSize = 14;
        treatHeader.DefaultCellTextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;

        treatHeader.DefaultCellPadding = margin;
        Aspose.Pdf.Row treatRow = treatHeader.Rows.Add();
        treatRow.Cells.Add("Treatment Section(non-drug related e.g.eligibility criteria, study assessments)");

        header.Paragraphs.Add(hdrTable);
        page.Header = header;


        //page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Safety / Drug(includes preparation, administration, dose modifications,equations)"));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(catHeader);
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Anti-bacterial and anti-fungal prophylaxis will be used on this study. Protocol Section: 5.2 Infection Control and Prophylaxis Fever and neutropenia criteria differs from BCH and DFCI policy.Please follow BCH / DFCI institutional guidelines for the treatment of fever and neutropenia as these are stricter than those listed in the protocol."));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("B This is sample alert text.Nothing special, but meant to at least give an idea of how this data will reflect on the page over time and see how things go with pagination, etc.There is nothing special about this text other than it is text.This should be a fairly good length for testing purposes. Just a little more text added for a longer section"));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(clarHeader);
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("C This is sample alert text.Nothing special, but meant to at least give an idea of how this data will reflect on the page over time and see how things go with pagination, etc.There is nothing special about this text other than it is text.This should be a fairly good length for testing purposes."));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("D This is sample alert text.Nothing special, but meant to at least give an idea of how this data will reflect on the page over time and see how things go with pagination, etc.There is nothing special about this text other than it is text.This should be a fairly good length for testing purposes."));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("E This is sample alert text.Nothing special, but meant to at least give an idea of how this data will reflect on the page over time and see how things go with pagination, etc.There is nothing special about this text other than it is text.This should be a fairly good length for testing purposes. This is sample alert text.Nothing special, but meant to at least give an idea of how this data will reflect on the page over time and see how things go with pagination, etc.There is nothing special about this text other than it is text.This should be a fairly good length for testing purposes."));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("F his is sample alert text.Nothing special, but meant to at least give an idea of how this data will reflect on the page over time and see how things go with pagination, etc.There is nothing special about this text other than it is text.This should be a fairly good length for testing purposes."));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(treatHeader);
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("G This is sample alert text.Nothing special, but meant to at least give an idea of how this data will reflect on the page over time and see how things go with pagination, etc.There is nothing special about this text other than it is text.This should be a fairly good length for testing purposes."));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("H This is sample alert text.Nothing special, but meant to at least give an idea of how this data will reflect on the page over time and see how things go with pagination, etc.There is nothing special about this text other than it is text.This should be a fairly good length for testing purposes. This is sample alert text.Nothing special, but meant to at least give an idea of how this data will reflect on the page over time and see how things go with pagination, etc.There is nothing special about this text other than it is text.This should be a fairly good length for testing purposes."));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("I This is sample alert text.Nothing special, but meant to at least give an idea of how this data will reflect on the page over time and see how things go with pagination, etc.There is nothing special about this text other than it is text.This should be a fairly good length for testing purposes."));
        page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment(""));

        // Save updated PDF

        document.Save(dataDir + "AlertPageHdr.pdf");

@dlaskey

In your code snippet, please try to add the below line before saving the document and see if output PDF satisfies your requirements:

// Save updated PDF
page.PageInfo.Margin.Top = (double)hdrTable.GetHeight(); // <= add this line
document.Save(dataDir + "AlertPageHdr.pdf");

AlertPageHdr.pdf (84.3 KB)

Hello. That helps and seems to work. What I am curious and it seems you have it in the PDF you posted as well, what is the random character at the bottom right of the Header. On Page 1 it is very large ‘A’ and then on Page 2, it is a ‘1’ (Large number one)

Thanks for your help. Would be interested how to suppress this character

@dlaskey

We will check it shortly and get back to you.

@dlaskey

These characters are appearing due to the below line of your code part that we commented out. Please remove it on your side and issue will be resolved.

Aspose.Pdf.Row hdrrow3 = hdrTable.Rows.Add();
hdrrow3.Cells.Add(protocolTitle.Text).Alignment = HorizontalAlignment.Center;
//hdrrow3.Cells.Add(alertTitle.Text).Alignment = HorizontalAlignment.Center; // <= Remove this line