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());

                //fileStamp.Close();
                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.AddStamp(stampEsignatureMeaningLeft);
                    page.AddStamp(stampEsignatureMeaningCenter);
                    page.AddStamp(stampEsignatureMeaningRight);
                    //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
                pdfDocumentissuance.Pages[1].Accept(textFragmentAbsorber);
                //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)
                {
                    page.AddStamp(stampEsignatureMeaningLeft);
                    page.AddStamp(stampEsignatureMeaningCenter);
                    //page.AddStamp(stampEsignatureMeaningRight);
                    stampEsignatureMeaningRight.Value = "Page { "+page.Number+"} of { "+pdfDocumentissuance.Pages.Count+"}";
                    page.AddStamp(stampEsignatureMeaningRight);
                }
                pdfDocumentissuance.Save(OutputFileName);
                pdfDocumentissuance.Dispose();

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 ?

Thanks
Naveenkumar S S

@naveenkumar_navitas_net

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.

Thanks
Naveenkumar S S

@naveenkumar_navitas_net

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

@naveenkumar_navitas_net

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
            page.Accept(TextFragmentAbsorberAddress);
            // 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…

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

@naveenkumar_navitas_net

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.

Dear Support,

We tried the same for other customer documents also, getting the same kind of issue.

Can you please help us to resolve this at the earliest.

Thanks
Naveenkumar S S

@naveenkumar_navitas_net

Would you please share recent files which reproduce this problem, so that we may investigate further to help you out.

Dear Support,

We are not able to update the footer text .

sample document is alreay avaiable in the preview threads.

By using the given the rectangle coordinates for header text able to extract the header text . but unable to extract the footer text.
( TextFragmentAbsorberAddress.TextSearchOptions.Rectangle = new Aspose.Pdf.Rectangle(0, page.PageInfo.Height - 70, page.PageInfo.Width, page.PageInfo.Height))

Please provide the rectangle coordinates for extracting the footer text.

Thanks,
Naveenkumar S S

@naveenkumar_navitas_net

We are afraid previously shared PDF document Source.pdf is not accessible and HeaderFooterEg.pdf does not contain any footer, so you may change rectangular coordinates as per your sample document and check if shared workaround works for your current PDF files or not.

Moreover, the basic measuring unit in Aspose.PDF for .NET is point, where 1 inch = 72 points and 1 cm = 1/2.54 inch = 0.3937 inch = 28.3 points. This will help you to specify the coordinates as per your requirements.

Dear Support,

Please find the attached sample footer contains document for footer.

please share the sample code for update the footer text.

Thanks,
Naveenkumar S S
TextInHeaderFooter_18.7.pdf (183.0 KB)

@naveenkumar_navitas_net

Please update the rectangular coordinates in the code snippet shared above. It will allow you to replace text in page footer of a PDF document.

TextFragmentAbsorberAddress.TextSearchOptions.Rectangle = new Aspose.Pdf.Rectangle(0, 0, page.PageInfo.Width, 72);

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

Dear support ,

Now we are able to read the footer text from the Rectangle object ,
But we not able to update the header and footer , which document contains image in header,

Please find the attached document .

Thanks,
Naveen kumar
source.pdf (82.1 KB)

@naveenkumar_navitas_net

Thank you for your kind feedback.

Kindly refer to Rectangle Constructor for detailed information about the parameters. Moreover, basic measuring unit in Aspose.PDF for .NET is point, where 1 inch = 72 points and 1 cm = 1/2.54 inch = 0.3937 inch = 28.3 points. The coordinates we had shared above were to extract footer text only. Likewise, based on information shared above, you may simply devise the coordinates to extract text from any region of a page.

We hope this will be helpful. Please feel free to contact us if you need any further information.