Read and edit PDF page label metadata using C#
Page labels in a PDF file are metadata assigned for a page, and they’re grouped as a page label range. By default, a PDF document has only one page label range, which starts from 1
. It’s possible to add multiple page label ranges, and you can specify from which number each page label range starts.
Getting the page label
To get the page label of a PDF page, use the GetPageLabel
method and specify the page number as its parameter.
To get the page label for all pages of a PDF document, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");int pageCount = gdpicturePDF.GetPageCount();for(int i = 1; i <= pageCount; i++){ // Get the page label. Console.WriteLine(gdpicturePDF.GetPageLabel(i));}
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") Dim pageCount As Integer = gdpicturePDF.GetPageCount() For i = 1 To pageCount ' Get the page label. Console.WriteLine(gdpicturePDF.GetPageLabel(i)) NextEnd Using
Used methods
Related topics
Getting the page label range
To get the page label range of a PDF document, use the GetPageLabelsRange
method. It requires the page label range index as its parameter. The GetPageLabelsRange
method returns the following values:
StartPage
— Value of the starting position of the page label range.Style
— Style of the specified label range, which is a member of thePdfPageLabelStyle
enumeration.Prefix
— If defined, the page label prefix.NumPortion
— Value of the numeric portion of the first page label.
A PDF document can have multiple page label ranges. To get the total amount in a PDF, use the GetPageLabelsRangeCount
method.
Use a condition to execute the GetPageLabelsRange
method only when the amount of page label ranges is greater than 0
, in case the PDF document doesn’t have any page label ranges set up.
To get the details of all page label ranges of a PDF document, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");// Get total amount of page label ranges.int labelCount = gdpicturePDF.GetPageLabelsRangeCount();int startPage = 0, numPortion = 0;PdfPageLabelStyle style = 0 ;String prefix = "";// Execute only when there are more than zero page label ranges.if(labelCount > 0){ for (int i = 0; i < labelCount; i++) { // Get page label range details. gdpicturePDF.GetPageLabelsRange(i, ref startPage, ref style, ref prefix, ref numPortion); Console.WriteLine($"Page Label Range nr {i}:\nStart Page: {startPage}\n" + $"Prefix: {prefix}\nStyle: {style}\nNumber Portion: {numPortion}"); }}
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Get total amount of page label ranges. Dim labelCount As Integer = gdpicturePDF.GetPageLabelsRangeCount() Dim startPage = 0, numPortion = 0 Dim style As PdfPageLabelStyle = 0 Dim prefix = "" ' Execute only when there are more than zero page label ranges. If labelCount > 0 Then For i = 0 To labelCount - 1 ' Get page label range details. gdpicturePDF.GetPageLabelsRange(i, startPage, style, prefix, numPortion) Console.WriteLine($"Page Label Range nr {i}: Start Page: {startPage} " & $"Prefix: {prefix} Style: {style} Number Portion: {numPortion}") Next End IfEnd Using
Related topics
Adding a page label range
To add a page label range to a PDF document, use the AddPageLabelsRange
method. It requires the following parameters:
StartPage
— Value of the starting position of the page label range.Style
— Style of the page label that’s a member of thePdfPageLabelStyle
enumeration (such as Arabic numerals or lowercase Roman numerals).Prefix
— String prefix of each page label.NumPortion
— Value of the numeric portion of the first page label.
Before adding a new page label range, delete all page label ranges previously added with the DeletePageLabels
method.
To add a page label range for the last three appendix pages of a PDF document, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");// Delete previous page label ranges.gdpicturePDF.DeletePageLabels();// Get total amount of pages.int pageCount = gdpicturePDF.GetPageCount();// Add the page label range for the appendix pages.gdpicturePDF.AddPageLabelsRange(pageCount - 2, PdfPageLabelStyle.PdfPageLabelStyleDecimalArabicNumerals, "A-", 1);// Save the PDF document.gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Delete previous page label ranges. gdpicturePDF.DeletePageLabels() ' Get total amount of pages. Dim pageCount As Integer = gdpicturePDF.GetPageCount() ' Add the page label range for the appendix pages. gdpicturePDF.AddPageLabelsRange(pageCount - 2, PdfPageLabelStyle.PdfPageLabelStyleDecimalArabicNumerals, "A-", 1) ' Save the PDF document. gdpicturePDF.SaveToFile("C:\temp\output.pdf")End Using
Used methods
Related topics
Adding multiple page label ranges
To add multiple page label ranges, use the AddPageLabelsRange
method. Remember to use the AddPageLabelsRange
method from the beginning of the PDF document to the end. This way, the next executed method won’t override the previous one.
To learn more about how to use the AddPageLabelsRange
method, refer to the previous section.
To add three page label ranges for the title, main, and appendix pages, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");// Delete previous page label ranges.gdpicturePDF.DeletePageLabels();// Get total amount of pages.int pageCount = gdpicturePDF.GetPageCount();// Add the page label range for the title page.gdpicturePDF.AddPageLabelsRange(1, PdfPageLabelStyle.PdfPageLabelStyleUndefined, "Title Page", 0);// Add the page label range for the main pages.gdpicturePDF.AddPageLabelsRange(2, PdfPageLabelStyle.PdfPageLabelStyleDecimalArabicNumerals, "", 1);// Add the page label range for the appendix pages.gdpicturePDF.AddPageLabelsRange(pageCount - 2, PdfPageLabelStyle.PdfPageLabelStyleDecimalArabicNumerals, "A-", 1);// Save the PDF document.gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Delete previous page label ranges. gdpicturePDF.DeletePageLabels() ' Get total amount of pages. Dim pageCount As Integer = gdpicturePDF.GetPageCount() ' Add the page label range for the title page. gdpicturePDF.AddPageLabelsRange(1, PdfPageLabelStyle.PdfPageLabelStyleUndefined, "Title Page", 0) ' Add the page label range for the main pages. gdpicturePDF.AddPageLabelsRange(2, PdfPageLabelStyle.PdfPageLabelStyleDecimalArabicNumerals, "", 1) ' Add the page label range for the appendix pages. gdpicturePDF.AddPageLabelsRange(pageCount - 2, PdfPageLabelStyle.PdfPageLabelStyleDecimalArabicNumerals, "A-", 1) ' Save the PDF document. gdpicturePDF.SaveToFile("C:\temp\output.pdf")End Using
Used methods
Related topics
Deleting a page label range
To delete a page label range, use the DeletePageLabelsRange
method. It only requires the page label range index.
A PDF document can have multiple page label ranges. To get the total amount in a PDF, use the GetPageLabelsRangeCount
method.
To delete the last page label range of a PDF document, use the following code:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");// Get page label range count.int rangeCount = gdpicturePDF.GetPageLabelsRangeCount();// Delete the last page label range.gdpicturePDF.DeletePageLabelsRange(rangeCount - 1);gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Get page label range count. Dim rangeCount As Integer = gdpicturePDF.GetPageLabelsRangeCount() ' Delete the last page label range. gdpicturePDF.DeletePageLabelsRange(rangeCount - 1) gdpicturePDF.SaveToFile("C:\temp\output.pdf")End Using
Related topics
Modifying a page label range
To modify an existing page label range, use any of the following methods:
SetPageLabelsRangeStartPage
— Changes the value of the starting position of the page label range.SetPageLabelsRangeStyle
— Changes the page label style that’s a member of thePdfPageLabelStyle
enumeration (such as Arabic numerals or lowercase Roman numerals).SetPageLabelsRangePrefix
— Changes the string prefix of each page label.SetPageLabelsRangeNumPortion
— Changes the value of the numeric portion of the first page label.
All methods mentioned above require the page label range index and the value of the parameter you want to change.
The code example below shows how to change all parameters of a page label range:
using GdPicturePDF gdpicturePDF = new GdPicturePDF();gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");// Change the start page to `1`.gdpicturePDF.SetPageLabelsRangeStartPage(0, 1);// Change the style to uppercase letters.gdpicturePDF.SetPageLabelsRangeStyle(0, PdfPageLabelStyle.PdfPageLabelStyleUppercaseLetters);// Change the prefix to `Page `.gdpicturePDF.SetPageLabelsRangePrefix(0, "Page ");// Change the starting number to `5`.gdpicturePDF.SetPageLabelsRangeNumPortion(0, 5);gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF() gdpicturePDF.LoadFromFile("C:\temp\source.pdf") ' Change the start page to `1`. gdpicturePDF.SetPageLabelsRangeStartPage(0, 1) ' Change the style to uppercase letters. gdpicturePDF.SetPageLabelsRangeStyle(0, PdfPageLabelStyle.PdfPageLabelStyleUppercaseLetters) ' Change the prefix to `Page `. gdpicturePDF.SetPageLabelsRangePrefix(0, "Page ") ' Change the starting number to `5`. gdpicturePDF.SetPageLabelsRangeNumPortion(0, 5) gdpicturePDF.SaveToFile("C:\temp\output.pdf")End Using
Used methods
Related topics