Extract text in header and footer and replace with new value


Dear Aspose Support Team,

In our product using the below code we are able to create header and footer.

Aspose.Pdf.Document pdfDocumentissuance = new Aspose.Pdf.Document(InputFileName);
TextStamp stampEsignatureMeaningLeft = new TextStamp(strEsignatureMeaning.ToString().Trim());
TextStamp stampEsignatureMeaningCenter = new TextStamp(strEsignatureMeaning.ToString().Trim());
TextStamp stampEsignatureMeaningRight = new TextStamp(strEsignatureMeaning.ToString().Trim());

                stampEsignatureMeaningLeft.BottomMargin = 40;
                stampEsignatureMeaningCenter.BottomMargin = 40;
                stampEsignatureMeaningRight.BottomMargin = 40;
                stampEsignatureMeaningLeft.RightMargin = 30;
                stampEsignatureMeaningRight.RightMargin = 25;
                stampEsignatureMeaningLeft.LeftMargin = 30;
                stampEsignatureMeaningLeft.YIndent = 90;
                stampEsignatureMeaningLeft.TopMargin = 10;
                stampEsignatureMeaningCenter.TopMargin = 10;
                stampEsignatureMeaningRight.TopMargin = 10;
                stampEsignatureMeaningLeft.TextState.FontStyle = FontStyles.Bold;
                stampEsignatureMeaningCenter.TextState.FontStyle = FontStyles.Bold;
                stampEsignatureMeaningRight.TextState.FontStyle = FontStyles.Bold;
                stampEsignatureMeaningLeft.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;
                stampEsignatureMeaningCenter.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
                stampEsignatureMeaningRight.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Right;
                stampEsignatureMeaningLeft.VerticalAlignment = VerticalAlignment.Top;
                stampEsignatureMeaningCenter.VerticalAlignment = VerticalAlignment.Top;
                stampEsignatureMeaningRight.VerticalAlignment = VerticalAlignment.Top;
                //stampEsignatureMeaning.TextState.FontSize = Convert.ToInt32(strFontSize);

                foreach (Aspose.Pdf.Page page in pdfDocumentissuance.Pages)
                    //page.Header = 
                //create TextAbsorber object to find all instances of the input search phrase
                TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(oldText);
                //accept the absorber for all the pages
                //get the extracted text fragments
                TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
                //loop through the fragments
                foreach (TextFragment textFragment in textFragmentCollection)

                    if (textFragment.TextState.HorizontalAlignment != Aspose.Pdf.HorizontalAlignment.Left)
                        textFragment.Text = "";
                        textFragment.Text = strEsignatureMeaning;
                stampEsignatureMeaningLeft.VerticalAlignment = VerticalAlignment.Bottom;
                stampEsignatureMeaningCenter.VerticalAlignment = VerticalAlignment.Bottom;
                stampEsignatureMeaningRight.VerticalAlignment = VerticalAlignment.Bottom;

                foreach (Aspose.Pdf.Page page in pdfDocumentissuance.Pages)
                    stampEsignatureMeaningRight.Value = "Page { "+page.Number+"} of { "+pdfDocumentissuance.Pages.Count+"}";

But we are not able to update the text in the header or footer if we wrongly inserted.

By using text replace feature , it replace all the occurrences in the page.

We want to update only the text in the Header and footer.

Can you please help me to sort out this issue ASAP ?

Naveenkumar S S



Thanks for contacting support.

Could you please share a sample PDF document along with the text that you want to replace in PDF document. We will test the scenario in our environment and address it accordingly.


Dear ASPOSE Support,
HeaderFooterEg.pdf (183.2 KB)
HeaderFooterEg_output.pdf (184.5 KB)

Please find the attached sample document.

In this example we try to replace the “Bookmark” text in the header but where the same applies three times in header and so many times in the body of the text.

Naveenkumar S S



We are testing the scenario and will share our findings shortly.



Thank you for being patient.

We would like to update you that there is no specific mark that defines header or footer content. However, we have devised a workaround to replace header or footer content. You can replace text in specific area of the page as in the code snippet below:

        // load PDF file
        //Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document((dataDir + "TextInHeaderFooter_18.7.pdf"));
        foreach (Aspose.Pdf.Page page in pdfDocument.Pages)
            // instantiate TextFragment Absorber object
            Aspose.Pdf.Text.TextFragmentAbsorber TextFragmentAbsorberAddress = new Aspose.Pdf.Text.TextFragmentAbsorber();
            // search text within page bound
            TextFragmentAbsorberAddress.TextSearchOptions.LimitToPageBounds = true;
            // specify the page region for TextSearch Options
            TextFragmentAbsorberAddress.TextSearchOptions.Rectangle = new Aspose.Pdf.Rectangle(0, page.PageInfo.Height - 72, page.PageInfo.Width, page.PageInfo.Height);
            // search text from first page of PDF file
            // iterate through individual TextFragment
            foreach (Aspose.Pdf.Text.TextFragment tf in TextFragmentAbsorberAddress.TextFragments)
                // update text
                tf.Text = "UPDATED";

        // save updated PDF file after text replace
        pdfDocument.Save(dataDir + "HEADERTextUpdated_18.7.pdf");

Respective source and generated PDF files have also been attached for your kind reference.

We hope this will be helpful, please feel free to contact us if you need any further assistance.


Support Team,

Thanks for the reply.

We executed the above code and we got the replacement twice in the headers.

Attached the documents for your reference…

Naveenkumar S SOutput.pdf (87.5 KB)
Source.pdf (87.4 KB)



Thank you for getting back to us.

This appears like a file specific limitation because suggested workaround works for other files, as the files shared by us earlier. We are afraid that header may not be replaced fine in such cases as the header or footers can not be distinguished with any specific mark on a PDF page.