⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplebookmark.java

📁 源码包含生成 PDF 和 HTML 的类库
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* * Copyright 2003 by Paulo Soares. * * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the License. * * The Original Code is 'iText, a free JAVA-PDF library'. * * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie. * All Rights Reserved. * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved. * * Contributor(s): all the names of the contributors are added in the source code * where applicable. * * Alternatively, the contents of this file may be used under the terms of the * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the * provisions of LGPL are applicable instead of those above.  If you wish to * allow use of your version of this file only under the terms of the LGPL * License and not to allow others to use your version of this file under * the MPL, indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by the LGPL. * If you do not delete the provisions above, a recipient may use your version * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE. * * This library is free software; you can redistribute it and/or modify it * under the terms of the MPL as stated above or under the terms of the GNU * Library General Public License as published by the Free Software Foundation; * either version 2 of the License, or any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more * details. * * If you didn't download this code from the following link, you should check if * you aren't using an obsolete version: * http://www.lowagie.com/iText/ */package com.lowagie.text.pdf;import java.io.BufferedWriter;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.Reader;import java.io.Writer;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Stack;import java.util.StringTokenizer;import com.lowagie.text.xml.simpleparser.IanaEncodings;import com.lowagie.text.xml.simpleparser.SimpleXMLDocHandler;import com.lowagie.text.xml.simpleparser.SimpleXMLParser;/** * Bookmark processing in a simple way. It has some limitations, mainly the only * action types supported are GoTo, GoToR, URI and Launch. * <p> * The list structure is composed by a number of HashMap, keyed by strings, one HashMap * for each bookmark. * The element values are all strings with the exception of the key "Kids" that has * another list for the child bookmarks. * <p> * All the bookmarks have a "Title" with the * bookmark title and optionally a "Style" that can be "bold", "italic" or a * combination of both. They can also have a "Color" key with a value of three * floats separated by spaces. The key "Open" can have the values "true" or "false" and * signals the open status of the children. It's "true" by default. * <p> * The actions and the parameters can be: * <ul> * <li>"Action" = "GoTo" - "Page" | "Named" * <ul> * <li>"Page" = "3 XYZ 70 400 null" - page number followed by a destination (/XYZ is also accepted) * <li>"Named" = "named_destination" * </ul> * <li>"Action" = "GoToR" - "Page" | "Named" | "NamedN", "File", ["NewWindow"] * <ul> * <li>"Page" = "3 XYZ 70 400 null" - page number followed by a destination (/XYZ is also accepted) * <li>"Named" = "named_destination_as_a_string" * <li>"NamedN" = "named_destination_as_a_name" * <li>"File" - "the_file_to_open" * <li>"NewWindow" - "true" or "false" * </ul> * <li>"Action" = "URI" - "URI" * <ul> * <li>"URI" = "http://sf.net" - URI to jump to * </ul> * <li>"Action" = "Launch" - "File" * <ul> * <li>"File" - "the_file_to_open_or_execute" * </ul> * @author Paulo Soares (psoares@consiste.pt) */public final class SimpleBookmark implements SimpleXMLDocHandler {        private ArrayList topList;    private Stack attr = new Stack();        /** Creates a new instance of SimpleBookmark */    private SimpleBookmark() {    }        private static List bookmarkDepth(PdfReader reader, PdfDictionary outline, IntHashtable pages) {        ArrayList list = new ArrayList();        while (outline != null) {            HashMap map = new HashMap();            PdfString title = (PdfString)PdfReader.getPdfObjectRelease(outline.get(PdfName.TITLE));            map.put("Title", title.toUnicodeString());            PdfArray color = (PdfArray)PdfReader.getPdfObjectRelease(outline.get(PdfName.C));            if (color != null && color.getArrayList().size() == 3) {                ByteBuffer out = new ByteBuffer();                ArrayList arr = color.getArrayList();                out.append(((PdfNumber)arr.get(0)).floatValue()).append(' ');                out.append(((PdfNumber)arr.get(1)).floatValue()).append(' ');                out.append(((PdfNumber)arr.get(2)).floatValue());                map.put("Color", PdfEncodings.convertToString(out.toByteArray(), null));            }            PdfNumber style = (PdfNumber)PdfReader.getPdfObjectRelease(outline.get(PdfName.F));            if (style != null) {                int f = style.intValue();                String s = "";                if ((f & 1) != 0)                    s += "italic ";                if ((f & 2) != 0)                    s += "bold ";                s = s.trim();                if (s.length() != 0)                     map.put("Style", s);            }            PdfNumber count = (PdfNumber)PdfReader.getPdfObjectRelease(outline.get(PdfName.COUNT));            if (count != null && count.intValue() < 0)                map.put("Open", "false");            try {                PdfObject dest = PdfReader.getPdfObjectRelease(outline.get(PdfName.DEST));                if (dest != null) {                    mapGotoBookmark(map, dest, pages); //changed by ujihara 2004-06-13                }                else {                    PdfDictionary action = (PdfDictionary)PdfReader.getPdfObjectRelease(outline.get(PdfName.A));                    if (action != null) {                        if (PdfName.GOTO.equals(PdfReader.getPdfObjectRelease(action.get(PdfName.S)))) {                            dest = PdfReader.getPdfObjectRelease(action.get(PdfName.D));                            if (dest != null) {                                mapGotoBookmark(map, dest, pages);                            }                        }                        else if (PdfName.URI.equals(PdfReader.getPdfObjectRelease(action.get(PdfName.S)))) {                            map.put("Action", "URI");                            map.put("URI", ((PdfString)PdfReader.getPdfObjectRelease(action.get(PdfName.URI))).toUnicodeString());                        }                        else if (PdfName.GOTOR.equals(PdfReader.getPdfObjectRelease(action.get(PdfName.S)))) {                            dest = PdfReader.getPdfObjectRelease(action.get(PdfName.D));                            if (dest != null) {                                if (dest.isString())                                    map.put("Named", dest.toString());                                else if (dest.isName())                                    map.put("NamedN", PdfName.decodeName(dest.toString()));                                else if (dest.isArray()) {                                    ArrayList arr = ((PdfArray)dest).getArrayList();                                    StringBuffer s = new StringBuffer();                                    s.append(arr.get(0).toString());                                    s.append(' ').append(arr.get(1).toString());                                    for (int k = 2; k < arr.size(); ++k)                                        s.append(' ').append(arr.get(k).toString());                                    map.put("Page", s.toString());                                }                            }                            map.put("Action", "GoToR");                            PdfObject file = PdfReader.getPdfObjectRelease(action.get(PdfName.F));                            if (file != null) {                                if (file.isString())                                    map.put("File", ((PdfString)file).toUnicodeString());                                else if (file.isDictionary()) {                                    file = PdfReader.getPdfObject(((PdfDictionary)file).get(PdfName.F));                                    if (file.isString())                                        map.put("File", ((PdfString)file).toUnicodeString());                                }                            }                            PdfObject newWindow = PdfReader.getPdfObjectRelease(action.get(PdfName.NEWWINDOW));                            if (newWindow != null)                                map.put("NewWindow", newWindow.toString());                        }                        else if (PdfName.LAUNCH.equals(PdfReader.getPdfObjectRelease(action.get(PdfName.S)))) {                            map.put("Action", "Launch");                            PdfObject file = PdfReader.getPdfObjectRelease(action.get(PdfName.F));                            if (file == null)                                file = PdfReader.getPdfObjectRelease(action.get(PdfName.WIN));                            if (file != null) {                                if (file.isString())                                    map.put("File", ((PdfString)file).toUnicodeString());                                else if (file.isDictionary()) {                                    file = PdfReader.getPdfObjectRelease(((PdfDictionary)file).get(PdfName.F));                                    if (file.isString())                                        map.put("File", ((PdfString)file).toUnicodeString());                                }                            }                        }                    }                }            }            catch (Exception e) {                //empty on purpose            }            PdfDictionary first = (PdfDictionary)PdfReader.getPdfObjectRelease(outline.get(PdfName.FIRST));            if (first != null) {                map.put("Kids", bookmarkDepth(reader, first, pages));            }            list.add(map);            outline = (PdfDictionary)PdfReader.getPdfObjectRelease(outline.get(PdfName.NEXT));        }        return list;    }    	private static void mapGotoBookmark(HashMap map, PdfObject dest, IntHashtable pages) 	{		if (dest.isString())			map.put("Named", dest.toString());		else if (dest.isName())			map.put("Named", PdfName.decodeName(dest.toString()));		else if (dest.isArray()) 			map.put("Page", makeBookmarkParam((PdfArray)dest, pages)); //changed by ujihara 2004-06-13		map.put("Action", "GoTo");	}	private static String makeBookmarkParam(PdfArray dest, IntHashtable pages)	{		ArrayList arr = dest.getArrayList();		StringBuffer s = new StringBuffer();        if (((PdfObject)arr.get(0)).isNumber())            s.append(((PdfNumber)arr.get(0)).intValue() + 1);        else            s.append(pages.get(getNumber((PdfIndirectReference)arr.get(0)))); //changed by ujihara 2004-06-13		s.append(' ').append(arr.get(1).toString().substring(1));		for (int k = 2; k < arr.size(); ++k)			s.append(' ').append(arr.get(k).toString());		return s.toString();	}	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -