Articles in this section
Category / Section

How to determine the page number of a particular text within a Word document

2 mins read

Syncfusion Essential DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can determine the page number of a particular text within a Word document.

Steps to determine the page number of a particular text within a Word document programmatically:

  1. Create a new C# Windows Forms application project.

 

Create Windows Forms Project

  1. Install the Syncfusion.DocToPDFConverter.WinForms and Syncfusion.PdfViewer.Windows NuGet packages as a reference to your .NET Framework applications from NuGet.org.

Install DocToPDFConverter

 

Install PDFViewer

  1. Add a new button in Form1.Designer.cs as follows.

 

private System.Windows.Forms.Button button1;
 
private void InitializeComponent()
{
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(265, 167);
    this.button1.Name = "Submit";
    this.button1.Size = new System.Drawing.Size(201, 83);
    this.button1.TabIndex = 0;
    this.button1.Text = "Submit";
    this.button1.UseVisualStyleBackColor = true;
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(800, 450);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);
}

 

  1. Include the following namespace in the Form1.cs file.
    using Syncfusion.DocIO;
    using Syncfusion.DocIO.DLS;
    using Syncfusion.DocToPDFConverter;
    using Syncfusion.Pdf;
    using Syncfusion.Windows.Forms.PdfViewer;
    

 

  1. Use the following code example to determine the page number of a particular text within a Word document.
    private void button1_Click(object sender, EventArgs e)
    {
         WordDocument wordDocument = new WordDocument(@"source.docx");
         DocToPDFConverter converter = new DocToPDFConverter();
         PdfDocument pdfDocument = converter.ConvertToPDF(wordDocument);
         MemoryStream stream = new MemoryStream();
         pdfDocument.Save(stream);
         pdfDocument.Close(true);
         wordDocument.Close();
         converter.Dispose();
         stream.Position = 0;
     
         FileStream fileStream = new FileStream("Output.pdf", FileMode.Create);
         stream.CopyTo(fileStream);
         fileStream.Close();
     
         PdfViewerControl documentViewer = new PdfViewerControl();
         //Load the PDF document
         documentViewer.Load(stream);
     
         //Get the occurrences of the target text and location.
         Dictionary<int, List<RectangleF>> textSearch = new Dictionary<int, List<RectangleF>>();
         bool IsMatchFound = documentViewer.FindText("Revision History", out textSearch);
         List<int> pageNumbers = null;
         if (IsMatchFound)
         {
             pageNumbers = GetPageNumebers(textSearch);
         }
         documentViewer.Dispose();
    }
    

 

  1. Helper method to get the page number of given text.
    /// <summary>
    /// Get the page number of give text
    /// </summary>
    /// <param name="textSearch"></param>
    /// <returns></returns>
    private List<int> GetPageNumebers(Dictionary<int, List<RectangleF>> textSearch)
    {
        List<int> list = new List<int>();
        int i = 0;
        foreach (List<RectangleF> rectangles in textSearch.Values)
        {
            if (rectangles.Count != 0)
            {
                list.Add(textSearch.FirstOrDefault(x => x.Value == rectangles).Key + 1);
                i++;
            }
        }
        return list;
    }
    

 

A complete working sample to find the list of fonts used in the Word document using C# can be downloaded from here

Take a moment to peruse the document where you can find basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.

Explore more about the rich set of Syncfusion Word Framework features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied