PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

Drawing PageLabels
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4430
Page 1 of 1

Author:  gtm [ Fri Apr 14, 2023 12:18 am ]
Post subject:  Drawing PageLabels

PagesLabels were added in PDF specification 1.3. The definition below is part of the PdfCatalog in the PdfSharpCore.Pdf.Advanced namespace. I have tried to figure out how to access it but with no success.

/// <summary>
/// (Optional; PDF 1.3) A number tree defining the page labeling for the document.
/// The keys in this tree are page indices; the corresponding values are page label dictionaries.
/// Each page index denotes the first page in a labeling range to which the specified page
/// label dictionary applies. The tree must include a value for pageindex 0.
/// </summary>
[KeyInfo("1.3", KeyType.NumberTree | KeyType.Optional)]
public const string PageLabels = "/PageLabels";

On a single page document the file structure for a Page Label would look something like this:

4 0 obj<</Type/Catalog/Pages 2 0 R/PageLabels<</Nums[0<</P(SK1)>>]>>>>endobj

Can someone explain how I would add /PageLabels to the document catalog and assign a key /P for a prefix to a specific page?
In the file line above the SK1 is the prefix or name of the page.

Thank you,
Todd

Author:  gtm [ Sun Apr 16, 2023 10:56 pm ]
Post subject:  Re: Drawing PageLabels

I have been working on this for awhile and haven't quite figured it out. If anyone has any input, it would be greatly appreciated.

This is the obj line in the PDF file I wish to duplicate. /P is the key that sets the prefix of the page label name to SK1.

4 0 obj<</Type/Catalog/Pages 2 0 R/PageLabels<</Nums[0<</P(SK1)>>]>>>>endobj

The line I am able to create is this:

2 0 obj<</Type/Catalog/Pages 3 0 R/PageLabels<</Nums[<</P(SK1)>>]>>>>endobj

The only difference in the line I am creating is that after Nums[ there is no 0.

This is the code I am using to produce it.

// Get access to the Catalog of the document
PdfCatalog catalog = document.Internals.Catalog;

// Create a dictionary that will contain the page label name for each page.
// since only a one page document, create one dictionary

PdfDictionary LabelItem1 = new PdfDictionary();

// Add the /P as the prefix Key and the name as a PdfItem to the LabelItem1 PdfDictionary.

LabelItem1.Elements.Add("/P", new PdfString("SK1"));

// Create an array to hold the page label dictionary.

PdfArray pageLabel = new PdfArray();

// Add the LabelItem1 to the pageLable array
pageLabel.Elements.Add(LabelItem1);


// Create a page number tree (as a dictionary) to hold the
PdfDictionary pageNumberTree = new PdfDictionary();


// Add the pageLabel array to pageNumberTree as a /Nums key.
pageNumberTree.Elements.Add("/Nums", pageLabel);

// I add pageNumberTree to the catalog elements.
catalog.Elements.Add("/PageLabels", pageNumberTree);

The 0 that I am missing is the page index of where the dictionary starts applying the /P to.
If I open the file and add it manually, it messes up all of the PDF display but it does give me the page number.

I will keep trying and post the final code when I figure it out but any help would be appreciated.

Thank!
Todd

Author:  gtm [ Tue Apr 18, 2023 8:17 pm ]
Post subject:  Re: Drawing PageLabels

SOLVED:

PDFSharp does not directly support the Catalog KEY PageLabels because it is a NumberTree Type, and NumberTrees are not supported.
There is however a PdfItem called PdfLiteral. This can be used to add a literal string to the document catalog. Just format the string the way that a numbertree would and add it to the catalog with the KEY /PageLabels. See below:


PdfCatalog catalog = document.Internals.Catalog;
var pageLabelItem = new PdfLiteral("<</Nums[0<</P(SK1)>>]>>");
catalog.Elements.Add("/PageLabels", pageLabelItem);

<</Nums[0<</P(SK1)>>]>> is what a numbertree would produce in the file. 0 = the starting page number to apply the the prefix to.

If this is something you are interested in, you should be able to read the section 8.3.1 of the PDf Refence Manual and figure out of the possible /PageLabels options.

Thanks!
Todd

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/