List of usage examples for com.lowagie.text.pdf PdfWriter setViewerPreferences
public void setViewerPreferences(int preferences)
From source file:org.frameworkset.web.servlet.view.document.AbstractPdfView.java
License:Apache License
/** * Prepare the given PdfWriter. Called before building the PDF document, * that is, before the call to <code>Document.open()</code>. * <p>Useful for registering a page event listener, for example. * The default implementation sets the viewer preferences as returned * by this class's <code>getViewerPreferences()</code> method. * @param model the model, in case meta information must be populated from it * @param writer the PdfWriter to prepare * @param request in case we need locale etc. Shouldn't look at attributes. * @throws DocumentException if thrown during writer preparation * @see com.lowagie.text.Document#open() * @see com.lowagie.text.pdf.PdfWriter#setPageEvent * @see com.lowagie.text.pdf.PdfWriter#setViewerPreferences * @see #getViewerPreferences()/*from ww w . j a va2 s. c o m*/ */ protected void prepareWriter(Map<String, Object> model, PdfWriter writer, HttpServletRequest request) throws DocumentException { writer.setViewerPreferences(getViewerPreferences()); }
From source file:org.schreibubi.JCombinations.ui.GridChartPanel.java
License:Open Source License
/** * Create PDF//from w w w. j a v a2 s . c o m * * @param name * Filename * @param selection * Selected Nodes * @param dm * DataModel * @param pl * ProgressListener */ @SuppressWarnings("unchecked") public static void generatePDF(File name, DataModel dm, ArrayList<TreePath> selection, ProgressListener pl) { com.lowagie.text.Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50); try { ArrayList<ExtendedJFreeChart> charts = dm.getCharts(selection); if (charts.size() > 0) { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(name)); writer.setViewerPreferences(PdfWriter.PageModeUseOutlines); document.addAuthor("Jrg Werner"); document.addSubject("Created by JCombinations"); document.addKeywords("JCombinations"); document.addCreator("JCombinations using iText"); // we define a header and a footer HeaderFooter header = new HeaderFooter(new Phrase("JCombinations by Jrg Werner"), false); HeaderFooter footer = new HeaderFooter(new Phrase("Page "), new Phrase(".")); footer.setAlignment(Element.ALIGN_CENTER); document.setHeader(header); document.setFooter(footer); document.open(); DefaultFontMapper mapper = new DefaultFontMapper(); FontFactory.registerDirectories(); mapper.insertDirectory("c:\\WINNT\\fonts"); PdfContentByte cb = writer.getDirectContent(); pl.progressStarted(new ProgressEvent(GridChartPanel.class, 0, charts.size())); for (int i = 0; i < charts.size(); i++) { ExtendedJFreeChart chart = charts.get(i); PdfTemplate tp = cb.createTemplate(document.right(EXTRA_MARGIN) - document.left(EXTRA_MARGIN), document.top(EXTRA_MARGIN) - document.bottom(EXTRA_MARGIN)); Graphics2D g2d = tp.createGraphics(document.right(EXTRA_MARGIN) - document.left(EXTRA_MARGIN), document.top(EXTRA_MARGIN) - document.bottom(EXTRA_MARGIN), mapper); Rectangle2D r2d = new Rectangle2D.Double(0, 0, document.right(EXTRA_MARGIN) - document.left(EXTRA_MARGIN), document.top(EXTRA_MARGIN) - document.bottom(EXTRA_MARGIN)); chart.draw(g2d, r2d); g2d.dispose(); cb.addTemplate(tp, document.left(EXTRA_MARGIN), document.bottom(EXTRA_MARGIN)); PdfDestination destination = new PdfDestination(PdfDestination.FIT); TreePath treePath = chart.getTreePath(); PdfOutline po = cb.getRootOutline(); for (int j = 0; j < treePath.getPathCount(); j++) { ArrayList<PdfOutline> lpo = po.getKids(); PdfOutline cpo = null; for (PdfOutline outline : lpo) if (outline.getTitle().compareTo(treePath.getPathComponent(j).toString()) == 0) cpo = outline; if (cpo == null) cpo = new PdfOutline(po, destination, treePath.getPathComponent(j).toString()); po = cpo; } document.newPage(); pl.progressIncremented(new ProgressEvent(GridChartPanel.class, i)); } document.close(); pl.progressEnded(new ProgressEvent(GridChartPanel.class)); } } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } }
From source file:questions.importpages.NameCard.java
public static void createOneCard() throws DocumentException, IOException { Rectangle rect = new Rectangle(Utilities.millimetersToPoints(86.5f), Utilities.millimetersToPoints(55)); Document document = new Document(rect); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(CARD)); writer.setViewerPreferences(PdfWriter.PrintScalingNone); document.open();/*w w w. j ava 2 s.c o m*/ PdfReader reader = new PdfReader(LOGO); Image img = Image.getInstance(writer.getImportedPage(reader, 1)); img.scaleToFit(rect.getWidth() / 1.5f, rect.getHeight() / 1.5f); img.setAbsolutePosition((rect.getWidth() - img.getScaledWidth()) / 2, (rect.getHeight() - img.getScaledHeight()) / 2); document.add(img); document.newPage(); BaseFont bf = BaseFont.createFont(FONT, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); Font font = new Font(bf, 12); font.setColor(new CMYKColor(1, 0.5f, 0, 0.467f)); ColumnText column = new ColumnText(writer.getDirectContent()); Paragraph p; p = new Paragraph("Bruno Lowagie\n1T3XT\nbruno@1t3xt.com", font); p.setAlignment(Element.ALIGN_CENTER); column.addElement(p); column.setSimpleColumn(0, 0, rect.getWidth(), rect.getHeight() * 0.75f); column.go(); document.close(); }
From source file:questions.objects.ChaptersAndMemory.java
/** * Generates a PDF file with autonumbered chapters and an open bookmark tab * //www . j a v a 2 s.c o m * @param args * no arguments needed here */ public static void main(String[] args) { // step 1: creation of a document-object Document document = new Document(); try { // step 2: // we create a writer PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); writer.setViewerPreferences(PdfWriter.PageModeUseOutlines); // step 3: we open the document document.open(); // step 4: we add a paragraph to the document Phrase text = new Phrase("Quick brown fox jumps over the lazy dog. "); ChapterAutoNumber chapter1 = new ChapterAutoNumber("This is a sample sentence:"); chapter1.setBookmarkTitle("The fox"); chapter1.setBookmarkOpen(false); chapter1.setComplete(false); Section section1 = chapter1.addSection("Quick"); section1.add(text); section1.add(text); section1.add(text); document.add(chapter1); Section section2 = chapter1.addSection("Fox"); section2.add(text); section2.setComplete(false); document.add(chapter1); section2.add(text); section2.add(text); section2.setComplete(true); chapter1.setComplete(true); document.add(chapter1); ChapterAutoNumber chapter2 = new ChapterAutoNumber("Jumps"); chapter2.setComplete(false); Section section = chapter2.addSection("Over"); section.setComplete(false); section.add(text); section.add(text); section.add(text); Section subsection1 = section.addSection("Lazy"); subsection1.setIndentationLeft(30); subsection1.add(text); subsection1.setComplete(false); document.add(chapter2); subsection1.add(text); subsection1.add(text); subsection1.add(text); subsection1.add(text); subsection1.setComplete(true); Section subsection2 = section.addSection("Dog"); subsection2.setIndentationRight(30); subsection2.add(text); subsection2.add(text); subsection2.add(text); subsection2.add(text); subsection2.add(text); Section subsection3 = section.addSection("Did you see it?"); subsection3.setIndentation(50); subsection3.add(text); subsection3.add(text); subsection3.add(text); subsection3.add(text); subsection3.add(text); section.setComplete(true); document.add(chapter2); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } // step 5: we close the document document.close(); }
From source file:questions.ocg.LockedLayers.java
public static void main(String[] args) { Document document = new Document(); try {/*from w ww.j av a 2s. c om*/ PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); writer.setPdfVersion(PdfWriter.VERSION_1_5); writer.setViewerPreferences(PdfWriter.PageModeUseOC); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfLayer nested = new PdfLayer("Nested Layers", writer); PdfLayer nested_1 = new PdfLayer("Nested Layer 1", writer); PdfLayer nested_2 = new PdfLayer("Nested Layer 2", writer); nested.addChild(nested_1); nested.addChild(nested_2); cb.beginLayer(nested); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layers"), 50, 775, 0); cb.endLayer(); cb.beginLayer(nested_1); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layer 1"), 100, 800, 0); cb.endLayer(); cb.beginLayer(nested_2); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("nested layer 2"), 100, 750, 0); cb.endLayer(); PdfLayer group = PdfLayer.createTitle("Grouped layers", writer); PdfLayer layer1 = new PdfLayer("Group: layer 1", writer); PdfLayer layer2 = new PdfLayer("Group: layer 2", writer); group.addChild(layer1); group.addChild(layer2); cb.beginLayer(layer1); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("layer 1 in the group"), 50, 700, 0); cb.endLayer(); cb.beginLayer(layer2); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("layer 2 in the group"), 50, 675, 0); cb.endLayer(); PdfLayer radiogroup = PdfLayer.createTitle("Radio Group", writer); PdfLayer radio1 = new PdfLayer("Radiogroup: layer 1", writer); radio1.setOn(true); PdfLayer radio2 = new PdfLayer("Radiogroup: layer 2", writer); radio2.setOn(false); PdfLayer radio3 = new PdfLayer("Radiogroup: layer 3", writer); radio3.setOn(false); radiogroup.addChild(radio1); radiogroup.addChild(radio2); radiogroup.addChild(radio3); ArrayList<PdfLayer> options = new ArrayList<PdfLayer>(); options.add(radio1); options.add(radio2); options.add(radio3); writer.addOCGRadioGroup(options); cb.beginLayer(radio1); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("option 1"), 50, 600, 0); cb.endLayer(); cb.beginLayer(radio2); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("option 2"), 50, 575, 0); cb.endLayer(); cb.beginLayer(radio3); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("option 3"), 50, 550, 0); cb.endLayer(); PdfLayer not_printed = new PdfLayer("not printed", writer); not_printed.setOnPanel(false); not_printed.setPrint("Print", false); cb.beginLayer(not_printed); ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase("PRINT THIS PAGE"), 300, 700, 90); cb.endLayer(); PdfLayer zoom = new PdfLayer("Zoom 0.75-1.25", writer); zoom.setOnPanel(false); zoom.setZoom(0.75f, 1.25f); cb.beginLayer(zoom); ColumnText.showTextAligned(cb, Element.ALIGN_LEFT, new Phrase("Only visible if the zoomfactor is between 75 and 125%"), 30, 530, 90); cb.endLayer(); writer.lockLayer(nested_1); writer.lockLayer(layer2); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } document.close(); }
From source file:questions.stamppages.BookmarksToTOC1.java
@SuppressWarnings("unchecked") public static void main(String[] args) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {//from ww w.j av a 2 s .co m Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, baos); writer.setViewerPreferences(PdfWriter.PageModeUseOutlines); writer.setPageEvent(new ParagraphBookmarkEvents(false)); document.open(); BufferedReader reader = new BufferedReader(new FileReader(RESOURCE)); String line; Paragraph p; while ((line = reader.readLine()) != null) { p = new Paragraph(line); p.setAlignment(Element.ALIGN_JUSTIFIED); document.add(p); document.add(Chunk.NEWLINE); } reader.close(); document.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } try { PdfReader reader = new PdfReader(baos.toByteArray()); Rectangle rect = reader.getPageSizeWithRotation(1); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); stamper.insertPage(1, rect); ColumnText column = new ColumnText(stamper.getOverContent(1)); column.setSimpleColumn(rect.getLeft(36), rect.getBottom(36), rect.getRight(36), rect.getTop(36)); column.addElement(new Paragraph("TABLE OF CONTENTS")); List<Map> list = SimpleBookmark.getBookmark(reader); Chunk link; PdfAction action; String info; int p = 1; float y = 10; for (Map<String, String> bookmark : list) { link = new Chunk(bookmark.get("Title")); info = bookmark.get("Page"); p = Integer.parseInt(info.substring(0, info.indexOf(' '))); y = Float.parseFloat(info.substring(info.lastIndexOf(' ') + 1) + "f"); action = PdfAction.gotoLocalPage(p, new PdfDestination(PdfDestination.FITH, y), stamper.getWriter()); link.setAction(action); column.addElement(new Paragraph(link)); } column.go(); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:questions.stamppages.BookmarksToTOC2.java
@SuppressWarnings("unchecked") public static void main(String[] args) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {//from w ww.j a v a2 s . c o m Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, baos); writer.setViewerPreferences(PdfWriter.PageModeUseOutlines); writer.setPageEvent(new ParagraphBookmarkEvents(true)); document.open(); BufferedReader reader = new BufferedReader(new FileReader(RESOURCE)); String line; Paragraph p; while ((line = reader.readLine()) != null) { p = new Paragraph(line); p.setAlignment(Element.ALIGN_JUSTIFIED); document.add(p); document.add(Chunk.NEWLINE); } reader.close(); document.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } try { PdfReader reader = new PdfReader(baos.toByteArray()); Rectangle rect = reader.getPageSizeWithRotation(1); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); stamper.insertPage(1, rect); ColumnText column = new ColumnText(stamper.getOverContent(1)); column.setSimpleColumn(rect.getLeft(36), rect.getBottom(36), rect.getRight(36), rect.getTop(36)); column.addElement(new Paragraph("TABLE OF CONTENTS")); List<Map> list = SimpleBookmark.getBookmark(reader); Chunk link; String dest; for (Map<String, String> bookmark : list) { link = new Chunk(bookmark.get("Title")); dest = bookmark.get("Named"); link.setAction(PdfAction.gotoLocalPage(dest, false)); column.addElement(new Paragraph(link)); } column.go(); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }