Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Tue Jun 23, 2020 3:17 pm

Hello,

is it possible to search text only in pdf header/footer not in the whole content? My final goal is to split large document into several pdf documents based on search results found only in header/footer.

Regards

profiler007
 
Posts: 73
Joined: Wed Nov 13, 2019 11:32 am

Wed Jun 24, 2020 2:30 am

Hello,

Thanks for your inquiry.
Please refer to the following code. If there is any question, please provide us with your input file for further investigation.
Code: Select all
    static void Main(string[] args)
    {
        PdfDocument pdf = new PdfDocument();
        pdf.LoadFromFile("test.pdf");
        PdfPageBase page = pdf.Pages[0];
        float headerHeight = 105;
        float footerHeight = 150;
        bool result = SearchText(page, headerHeight, footerHeight, "searchText");         
    }

    public static bool SearchText(PdfPageBase page, float headerHeight, float footerHeight, string searchText)
    {
        PdfDocument temp = new PdfDocument();
        PdfPageBase newPage = temp.Pages.Add(new SizeF(page.Size.Width, page.Size.Height), new PdfMargins(0));
        //Draw template
        newPage.Canvas.DrawTemplate(page.CreateTemplate(), new PointF(0, 0));
        //Draw a white rectangle to cover the content
        RectangleF rect = new RectangleF(0, headerHeight, page.Size.Width, page.Size.Height - headerHeight - footerHeight);
        newPage.Canvas.DrawRectangle(PdfBrushes.White, rect);
           
        PdfTextFind[] result = newPage.FindText(searchText, TextFindParameter.IgnoreCase).Finds;
        return result != null;
    }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Wed Jun 24, 2020 11:17 am

Thanks for the quick reply!

The sample code you provide me still search in the whole content of the document instead only in header or/and footer. Could you try with the attached document and the string "content".

Regards

profiler007
 
Posts: 73
Joined: Wed Nov 13, 2019 11:32 am

Thu Jun 25, 2020 2:20 am

Hello,

Thanks for your feedback.
Please refer to the following modified code. If there is any question, please get back to us.
Code: Select all
    static void Main(string[] args)
    {
        PdfDocument pdf = new PdfDocument();
        pdf.LoadFromFile("PDF master.pdf");
        PdfPageBase page = pdf.Pages[0];
        float headerHeight = 50;
        float footerHeight = 50;
        bool result = SearchText(page, headerHeight, footerHeight, "content");   
    }

    public static bool SearchText(PdfPageBase page, float headerHeight, float footerHeight, string searchText)
    {
        //Extract headerText
        RectangleF rect = new RectangleF(0, 0, page.Size.Width, headerHeight);
        string headerText = page.ExtractText(rect);
        //Extract footerText
        rect = new RectangleF(0, page.Size.Height - footerHeight, page.Size.Width, footerHeight);
        string footerText = page.ExtractText(rect);
        if (headerText.Contains(searchText))
        {
            return true;
        }
        if (footerText.Contains(searchText))
        {
            return true;
        }
        return false;
    }


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Tue Jul 07, 2020 11:22 am

Hello,

Greetings from E-iceblue!
Has your issue been resolved? Could you please give us some feedback at your convenience?
Thanks in advance.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Tue Jul 07, 2020 11:44 am

Hello,

It works fine. Thanks!

Regards

profiler007
 
Posts: 73
Joined: Wed Nov 13, 2019 11:32 am

Tue Jul 07, 2020 12:03 pm

Hello,

Thanks for your prompt response.
Feel free to contact us if you need further assistance.
Wish you all the best!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.PDF