Page labels are used to identify each page visually on the screen or in print. This article demonstrates how to get the PDF page labels using Spire.PDF.
Below is the screenshot of the sample PDF document:
Detail steps:
Step 1: Create a PdfDocument instance and load the sample.pdf file.
PdfDocument pdf = new PdfDocument(); pdf.LoadFromFile("sample.pdf");
Step 2: Get the labels of the pages in the PDF file.
StringBuilder sb = new StringBuilder(); for (int i = 0; i < pdf.Pages.Count; i++) { sb.AppendLine(pdf.Pages[i].PageLabel); }
Step 3: Save to a .txt file.
File.WriteAllText("PageLabels.txt", sb.ToString());
Output:
Full code:
using System.IO; using System.Text; using Spire.Pdf; namespace Get_PDF_Page_Labels { class Program { static void Main(string[] args) { //Create a PdfDocument instance PdfDocument pdf = new PdfDocument(); //Load the PDF file pdf.LoadFromFile("sample.pdf"); //Create a StringBuilder instance StringBuilder sb = new StringBuilder(); //Get the lables of the pages in the PDF file for (int i = 0; i < pdf.Pages.Count; i++) { sb.AppendLine(pdf.Pages[i].PageLabel); } //Save to a .txt file File.WriteAllText("PageLabels.txt", sb.ToString()); } } }