📄 simplebookmark.java
字号:
* (Contributed by Kazuya Ujihara)
* @param indirect
* 2004-06-13
*/
private static int getNumber(PdfIndirectReference indirect)
{
PdfDictionary pdfObj = (PdfDictionary)PdfReader.getPdfObjectRelease(indirect);
if (pdfObj.contains(PdfName.TYPE) && pdfObj.get(PdfName.TYPE).equals(PdfName.PAGES) && pdfObj.contains(PdfName.KIDS))
{
PdfArray kids = (PdfArray)pdfObj.get(PdfName.KIDS);
indirect = (PdfIndirectReference)kids.arrayList.get(0);
}
return indirect.getNumber();
}
/**
* Gets a <CODE>List</CODE> with the bookmarks. It returns <CODE>null</CODE> if
* the document doesn't have any bookmarks.
* @param reader the document
* @return a <CODE>List</CODE> with the bookmarks or <CODE>null</CODE> if the
* document doesn't have any
*/
public static List getBookmark(PdfReader reader) {
PdfDictionary catalog = reader.getCatalog();
PdfObject obj = PdfReader.getPdfObjectRelease(catalog.get(PdfName.OUTLINES));
if (obj == null || !obj.isDictionary())
return null;
PdfDictionary outlines = (PdfDictionary)obj;
IntHashtable pages = new IntHashtable();
int numPages = reader.getNumberOfPages();
for (int k = 1; k <= numPages; ++k) {
pages.put(reader.getPageOrigRef(k).getNumber(), k);
reader.releasePage(k);
}
return bookmarkDepth(reader, (PdfDictionary)PdfReader.getPdfObjectRelease(outlines.get(PdfName.FIRST)), pages);
}
/**
* Removes the bookmark entries for a number of page ranges. The page ranges
* consists of a number of pairs with the start/end page range. The page numbers
* are inclusive.
* @param list the bookmarks
* @param pageRange the page ranges, always in pairs.
*/
public static void eliminatePages(List list, int pageRange[]) {
if (list == null)
return;
for (Iterator it = list.listIterator(); it.hasNext();) {
HashMap map = (HashMap)it.next();
boolean hit = false;
if ("GoTo".equals(map.get("Action"))) {
String page = (String)map.get("Page");
if (page != null) {
page = page.trim();
int idx = page.indexOf(' ');
int pageNum;
if (idx < 0)
pageNum = Integer.parseInt(page);
else
pageNum = Integer.parseInt(page.substring(0, idx));
int len = pageRange.length & 0xfffffffe;
for (int k = 0; k < len; k += 2) {
if (pageNum >= pageRange[k] && pageNum <= pageRange[k + 1]) {
hit = true;
break;
}
}
}
}
List kids = (List)map.get("Kids");
if (kids != null) {
eliminatePages(kids, pageRange);
if (kids.isEmpty()) {
map.remove("Kids");
kids = null;
}
}
if (hit) {
if (kids == null)
it.remove();
else {
map.remove("Action");
map.remove("Page");
map.remove("Named");
}
}
}
}
/**
* For the pages in range add the <CODE>pageShift</CODE> to the page number.
* The page ranges
* consists of a number of pairs with the start/end page range. The page numbers
* are inclusive.
* @param list the bookmarks
* @param pageShift the number to add to the pages in range
* @param pageRange the page ranges, always in pairs. It can be <CODE>null</CODE>
* to include all the pages
*/
public static void shiftPageNumbers(List list, int pageShift, int pageRange[]) {
if (list == null)
return;
for (Iterator it = list.listIterator(); it.hasNext();) {
HashMap map = (HashMap)it.next();
if ("GoTo".equals(map.get("Action"))) {
String page = (String)map.get("Page");
if (page != null) {
page = page.trim();
int idx = page.indexOf(' ');
int pageNum;
if (idx < 0)
pageNum = Integer.parseInt(page);
else
pageNum = Integer.parseInt(page.substring(0, idx));
boolean hit = false;
if (pageRange == null)
hit = true;
else {
int len = pageRange.length & 0xfffffffe;
for (int k = 0; k < len; k += 2) {
if (pageNum >= pageRange[k] && pageNum <= pageRange[k + 1]) {
hit = true;
break;
}
}
}
if (hit) {
if (idx < 0)
page = Integer.toString(pageNum + pageShift);
else
page = (pageNum + pageShift) + page.substring(idx);
}
map.put("Page", page);
}
}
List kids = (List)map.get("Kids");
if (kids != null)
shiftPageNumbers(kids, pageShift, pageRange);
}
}
static void createOutlineAction(PdfDictionary outline, HashMap map, PdfWriter writer, boolean namedAsNames) {
try {
String action = (String)map.get("Action");
if ("GoTo".equals(action)) {
String p;
if ((p = (String)map.get("Named")) != null) {
if (namedAsNames)
outline.put(PdfName.DEST, new PdfName(p));
else
outline.put(PdfName.DEST, new PdfString(p, null));
}
else if ((p = (String)map.get("Page")) != null) {
PdfArray ar = new PdfArray();
StringTokenizer tk = new StringTokenizer(p);
int n = Integer.parseInt(tk.nextToken());
ar.add(writer.getPageReference(n));
if (!tk.hasMoreTokens()) {
ar.add(PdfName.XYZ);
ar.add(new float[]{0, 10000, 0});
}
else {
String fn = tk.nextToken();
if (fn.startsWith("/"))
fn = fn.substring(1);
ar.add(new PdfName(fn));
for (int k = 0; k < 4 && tk.hasMoreTokens(); ++k) {
fn = tk.nextToken();
if (fn.equals("null"))
ar.add(PdfNull.PDFNULL);
else
ar.add(new PdfNumber(fn));
}
}
outline.put(PdfName.DEST, ar);
}
}
else if ("GoToR".equals(action)) {
String p;
PdfDictionary dic = new PdfDictionary();
if ((p = (String)map.get("Named")) != null)
dic.put(PdfName.D, new PdfString(p, null));
else if ((p = (String)map.get("NamedN")) != null)
dic.put(PdfName.D, new PdfName(p));
else if ((p = (String)map.get("Page")) != null){
PdfArray ar = new PdfArray();
StringTokenizer tk = new StringTokenizer(p);
ar.add(new PdfNumber(tk.nextToken()));
if (!tk.hasMoreTokens()) {
ar.add(PdfName.XYZ);
ar.add(new float[]{0, 10000, 0});
}
else {
String fn = tk.nextToken();
if (fn.startsWith("/"))
fn = fn.substring(1);
ar.add(new PdfName(fn));
for (int k = 0; k < 4 && tk.hasMoreTokens(); ++k) {
fn = tk.nextToken();
if (fn.equals("null"))
ar.add(PdfNull.PDFNULL);
else
ar.add(new PdfNumber(fn));
}
}
dic.put(PdfName.D, ar);
}
String file = (String)map.get("File");
if (dic.size() > 0 && file != null) {
dic.put(PdfName.S, PdfName.GOTOR);
dic.put(PdfName.F, new PdfString(file));
String nw = (String)map.get("NewWindow");
if (nw != null) {
if (nw.equals("true"))
dic.put(PdfName.NEWWINDOW, PdfBoolean.PDFTRUE);
else if (nw.equals("false"))
dic.put(PdfName.NEWWINDOW, PdfBoolean.PDFFALSE);
}
outline.put(PdfName.A, dic);
}
}
else if ("URI".equals(action)) {
String uri = (String)map.get("URI");
if (uri != null) {
PdfDictionary dic = new PdfDictionary();
dic.put(PdfName.S, PdfName.URI);
dic.put(PdfName.URI, new PdfString(uri));
outline.put(PdfName.A, dic);
}
}
else if ("Launch".equals(action)) {
String file = (String)map.get("File");
if (file != null) {
PdfDictionary dic = new PdfDictionary();
dic.put(PdfName.S, PdfName.LAUNCH);
dic.put(PdfName.F, new PdfString(file));
outline.put(PdfName.A, dic);
}
}
}
catch (Exception e) {
// empty on purpose
}
}
public static Object[] iterateOutlines(PdfWriter writer, PdfIndirectReference parent, List kids, boolean namedAsNames) throws IOException {
PdfIndirectReference refs[] = new PdfIndirectReference[kids.size()];
for (int k = 0; k < refs.length; ++k)
refs[k] = writer.getPdfIndirectReference();
int ptr = 0;
int count = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -