List of usage examples for com.lowagie.text.pdf PdfWriter setDefaultColorspace
public void setDefaultColorspace(PdfName key, PdfObject cs)
From source file:de.cuseb.bilderbuch.pdf.PdfController.java
License:Open Source License
@RequestMapping(value = "/pdf", method = RequestMethod.GET) public void generatePdf(HttpSession session, HttpServletResponse httpServletResponse) { try {// w w w . j a v a2s . com PdfRequest pdfRequest = (PdfRequest) session.getAttribute("pdfRequest"); httpServletResponse.setContentType("application/pdf"); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, httpServletResponse.getOutputStream()); writer.setDefaultColorspace(PdfName.COLORSPACE, PdfName.DEFAULTRGB); //document.addAuthor(pdfRequest.getAuthor()); //document.addTitle(pdfRequest.getTitle()); document.setPageSize( new Rectangle(Utilities.millimetersToPoints(156), Utilities.millimetersToPoints(148))); document.open(); FontFactory.defaultEmbedding = true; FontFactory.register("IndieRock.ttf", "IndieRock"); Font font = FontFactory.getFont("IndieRock"); BaseFont baseFont = font.getBaseFont(); PdfContentByte cb = writer.getDirectContent(); Iterator<PdfPage> pages = pdfRequest.getPages().iterator(); while (pages.hasNext()) { PdfPage page = pages.next(); if (page.getImage() != null) { Image image = Image.getInstance(new URL(page.getImage().getUrl())); image.setDpi(300, 300); image.setAbsolutePosition(0f, 0f); image.scaleAbsolute(document.getPageSize().getWidth(), document.getPageSize().getHeight()); document.add(image); cb.saveState(); cb.beginText(); cb.setColorFill(Color.WHITE); cb.moveText(10f, 10f); cb.setFontAndSize(baseFont, 18); cb.showText(page.getSentence()); cb.endText(); cb.restoreState(); if (pages.hasNext()) { document.newPage(); } } } document.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:tufts.vue.PresentationNotes.java
License:Educational Community License
public static void createMapAsPDF(File file, LWMap map) { // step 1: creation of a document-object Document document = new Document(PageSize.LETTER.rotate()); try {//ww w . j a va 2 s . c o m GUI.activateWaitCursor(); // step 2: // we create a writer that listens to the document // and directs a PDF-stream to a file PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); writer.setDefaultColorspace(PdfName.DEFAULTRGB, null); // writer.setStrictImageSequence(true); // step 3: we open the document document.open(); PdfContentByte cb = writer.getDirectContent(); // cb.setFontAndSize(arg0, arg1) PdfTemplate tp = cb.createTemplate(document.getPageSize().getWidth() - 70, document.getPageSize().getHeight() - 70); // tp.createGraphicsShapes(arg0, arg1) PdfGraphics2D g2d = (PdfGraphics2D) tp.createGraphics(document.getPageSize().getWidth() - 70, document.getPageSize().getHeight() - 70, getFontMapper(), false, 60.0f); Dimension page = new Dimension((int) document.getPageSize().getWidth() - 70, (int) document.getPageSize().getHeight() - 70); // compute zoom & offset for visible map components Point2D.Float offset = new Point2D.Float(); offset.x = 35; offset.y = 35; // center vertically only if landscape mode //if (format.getOrientation() == PageFormat.LANDSCAPE) //TODO: allow horizontal centering, but not vertical centering (handle in computeZoomFit) Rectangle2D bounds = map.getBounds(); double scale = ZoomTool.computeZoomFit(page, 5, bounds, offset, true); // System.out.println(scale + " zoom factor..."); // set up the DrawContext DrawContext dc = new DrawContext(g2d, scale, -offset.x, -offset.y, null, // frame would be the PageFormat offset & size rectangle map, false); // todo: absolute links shouldn't be spec'd here // dc.setAntiAlias(true); dc.setMapDrawing(); // dc.setPDFRender(true); //dc.setPrioritizeQuality(false); // why was this low quality? dc.setPrintQuality(); //dc.setAntiAlias(false); // why was this turned off? was it redundant? dc.setClipOptimized(true); // dc.setDraftQuality(true); // dc.setRawDrawing(); //dc.setClipOptimized(false); dc.setInteractive(false); dc.setDrawPathways(false); // VUE.getActiveMap().draw(dc); LWPathway.setShowSlides(false); map.drawZero(dc); LWPathway.setShowSlides(true); g2d.dispose(); // document.add(new Paragraph(new Chunk().setAnchor("http://www.cnn.com"))); cb.addTemplate(tp, 0, 0); document.newPage(); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } finally { GUI.clearWaitCursor(); } // step 5: we close the document document.close(); }
From source file:tufts.vue.PresentationNotes.java
License:Educational Community License
public static void createPresentationSlidesDeck(File file) { // step 1: creation of a document-object final Document document = new Document(PageSize.LETTER.rotate()); try {/*from w ww .j a v a 2s . c o m*/ GUI.activateWaitCursor(); // step 2: // we create a writer that listens to the document // and directs a PDF-stream to a file final PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); writer.setDefaultColorspace(PdfName.DEFAULTRGB, null); writer.setStrictImageSequence(true); // step 3: we open the document document.open(); final float pageWidth = document.getPageSize().getWidth(); final float pageHeight = document.getPageSize().getHeight(); final float fillWidth = pageWidth - 70; final float fillHeight = pageHeight - 70; if (DEBUG.Enabled) { System.out.println("\n---------------------------------"); System.out.println("PDF DOCUMENT: pageSize " + document.getPageSize()); System.out.println("fillWidth=" + fillWidth + " fillHeight=" + fillHeight); } int currentIndex = VUE.getActivePathway().getIndex(); VUE.getActivePathway().setIndex(-1); for (LWPathway.Entry entry : VUE.getActivePathway().getEntries()) { if (DEBUG.Enabled) Log.debug("\n\nHANDLING DECK ENTRY " + entry); final LWSlide slide = entry.produceSlide(); final LWComponent toDraw = (slide == null ? entry.node : slide); final PdfTemplate template = PdfTemplate.createTemplate(writer, fillWidth, fillHeight); final PdfGraphics2D graphics = (PdfGraphics2D) template.createGraphics(fillWidth, fillHeight, getFontMapper(), false, 60.0f); final DrawContext dc = new DrawContext(graphics, 1.0); // //final DrawContext dc = new DrawContext(graphics, scale); //final DrawContext dc = new DrawContext(graphics, toDraw); // ideally, should use this dc.setClipOptimized(false); dc.setInteractive(false); // should be un-needed dc.setPrintQuality(); // We set dc.focused to the node, which is needed for portals so they know to render contents in their clip-region. // Normally a portal knows to do this because it's the focal (dc.focal), but in this case, the dc.focal could, // at best, be the slide. The current code actually uses NO focal in creating the DrawContext above, // and I'm not changing that just now as that would require lots of regression testing of printing. SMF 6/24/10 dc.focused = entry.node; if (DEBUG.Enabled) { Log.debug("DRAWING INTO " + dc + " g=" + graphics + " clip=" + tufts.Util.fmt(graphics.getClip())); if (DEBUG.PDF) { dc.g.setColor(Color.green); dc.g.fillRect(-Short.MAX_VALUE / 2, -Short.MAX_VALUE / 2, Short.MAX_VALUE, Short.MAX_VALUE); } } try { if (DEBUG.Enabled) dc.clearDebug(); toDraw.drawFit(dc, 0); } catch (Throwable t) { Log.error("exception drawing " + toDraw, t); } try { if (DEBUG.Enabled) Log.debug("painted " + DrawContext.getDebug() + " to " + dc); if (DEBUG.PDF) { final String dcDesc = dc.toString() + String.format(" scale=%.1f%%", dc.g.getTransform().getScaleX() * 100); dc.setRawDrawing(); dc.g.setColor(Color.red); dc.g.setFont(VueConstants.FixedSmallFont); dc.g.drawString(dcDesc, 10, fillHeight - 27); dc.g.drawString(entry.toString(), 10, fillHeight - 16); dc.g.drawString(toDraw.toString(), 10, fillHeight - 5); } // the graphics dispose appears to be very important -- we've seen completely intermittant // problems with generating many page PDF documents, which would be well explained by // java or internal itext buffers running out of memory. graphics.dispose(); document.add(Image.getInstance(template)); document.newPage(); } catch (Throwable t) { Log.error("exception finishing " + toDraw + " in " + dc, t); } } VUE.getActivePathway().setIndex(currentIndex); if (DEBUG.Enabled) Log.debug("PROCESSED ALL ENTRIES"); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } finally { GUI.clearWaitCursor(); } // step 5: we close the document document.close(); }