0

I edit page number of pdf in Adobe Acrobat X Pro. Test PDF

  • result:
    • 1-FrontCover
    • 2-FrontFold
    • 3-i
    • 4-ii
    • 5-iii
    • 6-1
    • 7-2
    • 8-3
    • 9-4
    • 10-5
    • 11-BackFold
    • 12-BackCover

But this result of GetPageLabels is wrong

  • page number:
    • 0-FrontCover1
    • 1-FrontFold1
    • 2-FrontFoldi
    • 3-FrontFoldii
    • 4-FrontFoldiii
    • 5-FrontFold1
    • 6-FrontFold2
    • 7-FrontFold3
    • 8-FrontFold4
    • 9-FrontFold5
    • 10-BackFold1
    • 11-BackCover1

C# Code:

objLabels = PdfPageLabels.GetPageLabels(objReader);
TextBox1.Text += "page number:" + Environment.NewLine;
if (objLabels != null) {
    for (i = 0; i <= objLabels.Length - 1; i++) {
        TextBox1.Text += i + "-" + objLabels(i) + Environment.NewLine;
    }
}

How to get the correct result like Adobe Acrobat X Pro?

黃梅子
  • 3
  • 2
  • This appears to be reported as [early as 2008](http://itextsharp.10939.n7.nabble.com/Issue-with-GetPageLabels-td3279.html). Check the solution offered there. Also verify if your version of iText is the latest. – Jongware Sep 04 '15 at 09:59
  • I replaced the latest version(5.5.6.0) of iTextSharp, But there is still wrong part of the page code. new result: `0-FrontCover` `1-FrontFold` `2-FrontFoldi` `3-FrontFoldii` `4-FrontFoldiii` `5-FrontFold1` `6-FrontFold2` `7-FrontFold3` `8-FrontFold4` `9-FrontFold5` `10-BackFold` `11-BackCover` – 黃梅子 Sep 07 '15 at 01:36

2 Answers2

1

There is a small bug in PdfPageLabels.GetPageLabels(PdfReader). When encountering a new page label dictionary without a P (prefix) entry, it does not reset the current prefix value:

int pagecount = 1;
String prefix = "";
char type = 'D';
for (int i = 0; i < n; i++) {
    if (numberTree.ContainsKey(i)) {
        PdfDictionary d = (PdfDictionary)PdfReader.GetPdfObjectRelease(numberTree[i]);
        if (d.Contains(PdfName.ST)) {
            pagecount = ((PdfNumber)d.Get(PdfName.ST)).IntValue;
        }
        else {
            pagecount = 1;
        }
        if (d.Contains(PdfName.P)) {
            prefix = ((PdfString)d.Get(PdfName.P)).ToUnicodeString();
        }
        if (d.Contains(PdfName.S)) {
            type = ((PdfName)d.Get(PdfName.S)).ToString()[1];
        }
        else {
            type = 'e';
        }
    } 
    ...
}

You can fix this by adding the following else clause to the if in question:

        if (d.Contains(PdfName.P)) {
            prefix = ((PdfString)d.Get(PdfName.P)).ToUnicodeString();
        }
        else
        {
            prefix = "";
        }

Whith this change I get I get

page number:
 0 - FrontCover
 1 - FrontFold
 2 - i
 3 - ii
 4 - iii
 5 - 1
 6 - 2
 7 - 3
 8 - 4
 9 - 5
10 - BackFold
11 - BackCover

PS: The same issue is present in the Java iText version, tested in ReadPageLabels.java.

mkl
  • 90,588
  • 15
  • 125
  • 265
0

Thank you for helping to solve my problem, Here is my complete program.

public string[] ReadPageLabel(PdfReader objReader, int intPageCount)
{
    PdfDictionary objDictionary ;
    Dictionary<int, PdfObject> objTree ;
    string[] arrLabels ;
    int i ;
    char chrLabelKind ;
    string strLabelPrefix ;
    int intLableNumber ;
    //PdfPageLabels is wrong
    //arrLabels = PdfPageLabels.GetPageLabels(objReader)


    arrLabels = new string[intPageCount];

    if (objReader.Catalog.Get(PdfName.PAGELABELS) != null) {
        objTree = PdfNumberTree.ReadTree(PdfReader.GetPdfObjectRelease(objReader.Catalog.Get(PdfName.PAGELABELS)));

        chrLabelKind = 'D';
        strLabelPrefix = "";
        intLableNumber = 1;
        for (i = 0; i <= intPageCount - 1; i++) {
            if (objTree.ContainsKey(i)) { //if reset page number
                objDictionary = PdfReader.GetPdfObjectRelease(objTree[i]);
                //PdfName.S:Number Kind
                if (objDictionary.Contains(PdfName.S)) {
                    chrLabelKind = ((PdfName)objDictionary.Get(PdfName.S)).ToString()(1);
                    //PdfName.S:/R,/r,/A,/a,/e,/D,ToString()(1)get alphabet of Index=1
                } else {
                    chrLabelKind = 'e';
                }
                //PdfName.P:Prefix
                if (objDictionary.Contains(PdfName.P)) {
                    strLabelPrefix = ((PdfString)objDictionary.Get(PdfName.P)).ToUnicodeString();               
                } else {
                    strLabelPrefix = "";
                }
                //PdfName.ST:Start Number
                if (objDictionary.Contains(PdfName.ST)) {
                    intLableNumber = ((PdfNumber)objDictionary.Get(PdfName.ST)).IntValue;
                } else {
                    intLableNumber = 1;
                }
            }
            switch (chrLabelKind) {
                case 'R':
                    //I,II,III
                    arrLabels[i] = strLabelPrefix + factories.RomanNumberFactory.GetUpperCaseString(intLableNumber);
                    break;
                case 'r':
                    //i,ii,iii
                    arrLabels[i] = strLabelPrefix + factories.RomanNumberFactory.GetLowerCaseString(intLableNumber);
                    break;
                case 'A':
                    //A,B,C
                    arrLabels[i] = strLabelPrefix + factories.RomanAlphabetFactory.GetUpperCaseString(intLableNumber);
                    break;
                case 'a':
                    //a,b,c
                    arrLabels[i] = strLabelPrefix + factories.RomanAlphabetFactory.GetLowerCaseString(intLableNumber);
                    break;
                case 'e':
                    //no number kind
                    arrLabels[i] = strLabelPrefix;
                    break;
                default:
                    //1,2,3
                    arrLabels[i] = strLabelPrefix + intLableNumber;
                    break;
            }
            intLableNumber += 1;

        }
    } else {
        for (i = 0; i <= intPageCount - 1; i++) {
            arrLabels[i] = i + 1;
        }
    }
    return arrLabels;
}
黃梅子
  • 3
  • 2