List of usage examples for com.lowagie.text.pdf PdfCopyFields setOutlines
public void setOutlines(List outlines)
From source file:ErrMsgException.java
License:Open Source License
public static void main(String args[]) { // ?/*w w w . ja va2 s . co m*/ if (args.length < 3 || args.length > 5) { usage(); return; } // ?? ArrayList<HashMap> bookmark = new ArrayList<HashMap>(); HashMap<String, Object> lastmap = null; ArrayDeque<ArrayList> queue = new ArrayDeque<ArrayList>(); queue.push(bookmark); // ????? try { File file = new File(args[2]); BufferedReader br = new BufferedReader(new FileReader(file)); String line; int previouslevel = 0; int lno = 0; while ((line = br.readLine()) != null) { lno++; if (line.startsWith("#")) continue; // ?# int level = 0; if (line.startsWith("\t")) { // do { line = line.substring(1); level++; } while (line.startsWith("\t")); } String lines[] = line.split("\t"); line = ""; if (lines.length > 2) { for (int i = 0; i < lines.length - 1; i++) { line += lines[i]; if (i < lines.length - 1) line += ""; } } else { line = lines[0]; } if (lines.length < 2) continue; int preample = 0; if (args.length >= 4) { preample = Integer.parseInt(args[3]); } int page = roman2arabic(lines[lines.length - 1], preample); HashMap<String, Object> map = new HashMap<String, Object>(); map.put("Title", line); map.put("Action", "GoTo"); map.put("Page", page + " Fit"); if (args.length >= 5) { // ?? int foldlevel = Integer.parseInt(args[4]); if (foldlevel <= level) map.put("Open", "false"); } if (level > previouslevel) { if (level - previouslevel > 1) { // ??? throw new ErrMsgException( lno + ": ??2?????????"); } // ArrayList<HashMap> kids = new ArrayList<HashMap>(); kids.add(map); if (lastmap != null) { lastmap.put("Kids", kids); } else { throw new ErrMsgException( lno + ": ??????????"); } queue.push(kids); } else if (level < previouslevel) { // ? for (int i = previouslevel; i > level; i--) { queue.pop(); } queue.peek().add(map); } else { // ???????? queue.peek().add(map); } lastmap = map; previouslevel = level; } br.close(); } catch (IOException e) { System.out.println(e); return; } catch (ErrMsgException e) { System.out.println("?????\n" + e.getMessage()); return; } // PDF???? try { PdfReader reader = new PdfReader(args[0]); int maxpage = reader.getNumberOfPages(); OutputStream os = new FileOutputStream(args[1]); PdfCopyFields copyFields = new PdfCopyFields(os); copyFields.addDocument(reader); copyFields.setOutlines(bookmark); if (args.length > 3) { // ? PdfPageLabels labels = new PdfPageLabels(); if (maxpage > 0) labels.addPageLabel(1, PdfPageLabels.LOWERCASE_ROMAN_NUMERALS, null, 1); if (maxpage > Integer.parseInt(args[3])) labels.addPageLabel(Integer.parseInt(args[3]) + 1, PdfPageLabels.DECIMAL_ARABIC_NUMERALS, null, 1); copyFields.getWriter().setPageLabels(labels); } copyFields.close(); os.close(); } catch (DocumentException e) { System.out.println(e); return; } catch (IOException e) { System.out.println(e); return; } System.out.println(args[1] + " ?????"); }