PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue May 28, 2024 7:07 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Drawing PageLabels
PostPosted: Fri Apr 14, 2023 12:18 am 
Offline

Joined: Fri Dec 10, 2021 3:33 pm
Posts: 7
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


Last edited by gtm on Mon Oct 30, 2023 2:14 pm, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: Drawing PageLabels
PostPosted: Sun Apr 16, 2023 10:56 pm 
Offline

Joined: Fri Dec 10, 2021 3:33 pm
Posts: 7
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Drawing PageLabels
PostPosted: Tue Apr 18, 2023 8:17 pm 
Offline

Joined: Fri Dec 10, 2021 3:33 pm
Posts: 7
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 77 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group