Hi,
just another patch (against current SVN). This time I want to allow
users to tweak page labels, so I need to be able to read the old page
labels from a PDF in a format suitable for tweaking (and getting a
String[] with all labels is not, in my opinion). Other solutions to
solve my problem are, of course, welcome as well.
By the way: What is the reason that this code here does not work?
FileOutputStream fos = new FileOutputStream("...");
PdfStamper stamper = new PdfStamper(currentReader, fos);
stamper.getWriter().setPageLabels(lbls);
stamper.close();
It copies the PDF, but does not set page labels.
I got it working by using a PdfCopy and importing every page separately.
Is there an easier solution?
TIA,
Michael
(not subscribed to mailing list any longer due to high traffic)
diff -Naur src-orig/com/lowagie/text/pdf/PdfPageLabelFormat.java
src/com/lowagie/text/pdf/PdfPageLabelFormat.java
--- src-orig/com/lowagie/text/pdf/PdfPageLabelFormat.java 1970-01-01
01:00:00.000000000 +0100
+++ src/com/lowagie/text/pdf/PdfPageLabelFormat.java 2007-06-22
23:24:32.000000000 +0200
@@ -0,0 +1,32 @@
+package com.lowagie.text.pdf;
+
+/**
+ * A specification how page labels for a range of pages
+ * should look like. Each page label format is valid for all subsequent
+ * pages that do not define a page label format themselves.
+ */
+public class PdfPageLabelFormat {
+
+ private final int physicalPage;
+ private final int numberStyle;
+ private final String prefix;
+ private final int logicalPage;
+
+ /** Creates a page label format.
+ * @param physicalPage the real page to start the numbering. First page is
1
+ * @param numberStyle the numbering style such as LOWERCASE_ROMAN_NUMERALS
+ * @param prefix the text to prefix the number. Can be <CODE>null</CODE>
or empty
+ * @param logicalPage the first logical page number
+ */
+ public PdfPageLabelFormat(int physicalPage, int numberStyle, String
prefix, int logicalPage) {
+ this.physicalPage = physicalPage;
+ this.numberStyle = numberStyle;
+ this.prefix = prefix;
+ this.logicalPage = logicalPage;
+ }
+
+ public int getPhysicalPage() { return physicalPage; }
+ public int getNumberStyle() { return numberStyle; }
+ public String getPrefix() { return prefix; }
+ public int getLogicalPage() {return logicalPage;}
+}
diff -Naur src-orig/com/lowagie/text/pdf/PdfPageLabels.java
src/com/lowagie/text/pdf/PdfPageLabels.java
--- src-orig/com/lowagie/text/pdf/PdfPageLabels.java 2007-06-22
23:16:04.000000000 +0200
+++ src/com/lowagie/text/pdf/PdfPageLabels.java 2007-06-22 23:30:50.000000000
+0200
@@ -52,6 +52,7 @@
import java.util.HashMap;
import java.util.Iterator;
+import java.util.Map;
import java.util.TreeMap;
import com.lowagie.text.factories.RomanAlphabetFactory;
@@ -100,6 +101,12 @@
}
/** Adds or replaces a page label.
+ */
+ public void addPageLabel(PdfPageLabelFormat format) {
+ addPageLabel(format.getPhysicalPage(), format.getNumberStyle(),
format.getPrefix(), format.getLogicalPage());
+ }
+
+ /** Adds or replaces a page label.
* @param page the real page to start the numbering. First page is 1
* @param numberStyle the numbering style such as LOWERCASE_ROMAN_NUMERALS
* @param text the text to prefix the number. Can be <CODE>null</CODE> or
empty
@@ -234,4 +241,60 @@
}
return labelstrings;
}
+
+ /**
+ * Retrieves the page labels from a PDF as an array of [EMAIL PROTECTED]
PdfPageLabelFormat} objects.
+ * @param reader a PdfReader object that has the page labels you want to
retrieve
+ * @return a PdfPageLabelEntry array, containing an entry for each format
change
+ */
+ public static PdfPageLabelFormat[] getPageLabelFormats(PdfReader reader) {
+ PdfDictionary dict = reader.getCatalog();
+ PdfDictionary labels =
(PdfDictionary)PdfReader.getPdfObject((PdfObject)dict.get(PdfName.PAGELABELS));
+ if (labels == null) return new PdfPageLabelFormat[0];
+ PdfArray numbers =
(PdfArray)PdfReader.getPdfObject((PdfObject)labels.get(PdfName.NUMS));
+ PdfNumber pageIndex;
+ PdfDictionary pageLabel;
+ TreeMap numberTree = new TreeMap();
+ for (Iterator i = numbers.listIterator(); i.hasNext(); ) {
+ pageIndex = (PdfNumber)i.next();
+ pageLabel = (PdfDictionary)
PdfReader.getPdfObject((PdfObject)i.next());
+ numberTree.put(new Integer(pageIndex.intValue()),
pageLabel);
+ }
+ PdfPageLabelFormat[] formats = new
PdfPageLabelFormat[numberTree.size()];
+ String prefix;
+ int numberStyle;
+ int pagecount;
+ int i=0;
+ for (Iterator it = numberTree.entrySet().iterator();
it.hasNext();) {
+ Map.Entry entry = (Map.Entry) it.next();
+ Integer key = (Integer)entry.getKey();
+ PdfDictionary d = (PdfDictionary) entry.getValue();
+ 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)).toString();
+ } else {
+ prefix = "";
+ }
+ if (d.contains(PdfName.S)) {
+ char type =
((PdfName)d.get(PdfName.S)).toString().charAt(1);
+ switch(type) {
+ case 'R': numberStyle =
UPPERCASE_ROMAN_NUMERALS; break;
+ case 'r': numberStyle =
LOWERCASE_ROMAN_NUMERALS; break;
+ case 'A': numberStyle = UPPERCASE_LETTERS;
break;
+ case 'a': numberStyle = LOWERCASE_LETTERS;
break;
+ default: numberStyle = DECIMAL_ARABIC_NUMERALS;
break;
+ }
+ } else {
+ numberStyle = EMPTY;
+ }
+ formats[i] = new PdfPageLabelFormat(key.intValue()+1,
numberStyle, prefix, pagecount);
+ i++;
+ }
+ return formats;
+ }
}
\ No newline at end of file
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/