Get PDF Page Labels in C#

2019-03-26 07:12:21 Written by  support iceblue
Rate this item
(0 votes)

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:

Get PDF Page Labels in C#

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:

Get PDF Page Labels in C#

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());
        }
    }
}

Additional Info

  • tutorial_title:
Last modified on Sunday, 26 September 2021 01:47