How to determine the page number of a particular text within a Word document?
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:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.DocToPDFConverter.WinForms and Syncfusion.PdfViewer.Windows NuGet packages as a reference to your .NET Framework applications from NuGet.org.
- 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);
}
- 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;
- 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(); }
- 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.
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.
I hope you enjoyed learning about how to determine the page number of a particular text within a Word document.
You can
refer to our WinForms word feature
tour page
to know about its other groundbreaking feature
representations and documentation, and
how to quickly get started for configuration specifications. You can also
explore our
WinForms word example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!