Extract text in header and footer and replace with new value


#1

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


#2

@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.


#3

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


#4

@naveenkumar_navitas_net

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


#5

@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.