0

我在 Adob​​e Acrobat X Pro 中编辑 pdf 的页码。 测试PDF

  • 结果:
    • 1-封面
    • 2-正面折叠
    • 3-i
    • 4-ii
    • 5-iii
    • 6-1
    • 7-2
    • 8-3
    • 9-4
    • 10-5
    • 11-BackFold
    • 12-封底

但是 GetPageLabels 的这个结果是错误的

  • 页码:
    • 0-封面1
    • 1-前折叠1
    • 2-FrontFoldi
    • 3-FrontFoldii
    • 4-FrontFoldiii
    • 5-前折叠1
    • 6-前折2
    • 7-前折叠3
    • 8-前折4
    • 9-前折5
    • 10-BackFold1
    • 11-封底1

C#代码:

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

如何获得像 Adob​​e Acrobat X Pro 这样的正确结果?

4

2 回答 2

1

中有一个小错误PdfPageLabels.GetPageLabels(PdfReader)。当遇到没有P(前缀)条目的新页面标签字典时,它不会重置当前prefix值:

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';
        }
    } 
    ...
}

您可以通过将以下else子句添加到相关问题中来解决此if问题:

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

我得到了这个改变

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:Java iText 版本中存在相同的问题,在ReadPageLabels.java中进行了测试。

于 2015-09-07T13:00:31.017 回答
0

感谢您帮助解决我的问题,这是我的完整程序。

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;
}
于 2015-09-08T03:21:32.157 回答