阅读页面的罗马页码

时间:2015-03-20 10:44:58

标签: itextsharp itext

在Adobe Reader中,电子书的第一页可以使用罗马格式的页码,如下图所示

图片:http://i.stack.imgur.com/GSm0Q.jpg

我想用iText读取这些页码(不是索引的页码),但我不知道应该使用哪些属性(标签或注释......)。我已经可以用PdfReader打开文件,遍历所有页面,但不知道我应该访问这些罗马数字

using (Stream pdfStream = new FileStream(sourceFileName, FileMode.Open))
{
    PdfReader pdfReader = new PdfReader(pdfStream);
    for (int index = 1; index <= pdfReader.NumberOfPages; index++)
    {

    }
}

感谢。

1 个答案:

答案 0 :(得分:1)

您正在寻找PageLabelExample。在此示例中,我们有一个PDF page_labels.pdf,其页面编号如下:

enter image description here

listPageLabels()方法中,我们创建一个包含所有页面标签的txt文件:

public void listPageLabels(String src, String dest) throws IOException {
    // no PDF, just a text file
    PrintStream out = new PrintStream(new FileOutputStream(dest));
    PdfReader reader = new PdfReader(src);
    String[] labels = PdfPageLabels.getPageLabels(reader);
    for (int i = 0; i < labels.length; i++) {
        out.println(labels[i]);
    }
    out.flush();
    out.close();
    reader.close();
}

结果如下:

A
B
1
2
3
Movies-4
Movies-5
Movies-6
Movies-7
Movies-8

如果您需要iTextSharp示例,请查看此方法:

/**
 * Reads the page labels from an existing PDF
 * @param src the existing PDF
 */
public string ListPageLabels(byte[] src) {
    StringBuilder sb = new StringBuilder();
    String[] labels = PdfPageLabels.GetPageLabels(new PdfReader(src));
    for (int i = 0; i < labels.Length; i++) {
        sb.Append(labels[i]);
        sb.AppendLine();
    }
    return sb.ToString();
} 

<强>更新

正如评论部分所承诺的那样:PdfPageLabels.cs

我不是C#开发人员,但这是一个没有添加前缀的GetPageLabels()方法的快速和脏版本:

public static String[] GetPageLabels(PdfReader reader) {
    int n = reader.NumberOfPages;
    PdfDictionary dict = reader.Catalog;
    PdfDictionary labels = (PdfDictionary)PdfReader.GetPdfObjectRelease(dict.Get(PdfName.PAGELABELS));
    if (labels == null)
        return null;
    String[] labelstrings = new String[n];
    Dictionary<int, PdfObject> numberTree = PdfNumberTree.ReadTree(labels);    
    int pagecount = 1;
    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.S)) {
                type = ((PdfName)d.Get(PdfName.S)).ToString()[1];
            }
            else {
                type = 'e';
            }
        }
        switch (type) {
        default:
            labelstrings[i] = "" + pagecount;
            break;
        case 'R':
            labelstrings[i] = RomanNumberFactory.GetUpperCaseString(pagecount);
            break;
        case 'r':
            labelstrings[i] = RomanNumberFactory.GetLowerCaseString(pagecount);
            break;
        case 'A':
            labelstrings[i] = RomanAlphabetFactory.GetUpperCaseString(pagecount);
            break;
        case 'a':
            labelstrings[i] = RomanAlphabetFactory.GetLowerCaseString(pagecount);
            break;
        case 'e':
            labelstrings[i] = "";
            break;
        }
        pagecount++;
    }
    return labelstrings;
}