List of usage examples for com.lowagie.text ImgTemplate ImgTemplate
public ImgTemplate(PdfTemplate template) throws BadElementException
From source file:com.centurylink.mdw.designer.pages.ExportHelper.java
License:Apache License
public void printImagePdf(String filename, DesignerCanvas canvas, Dimension graphsize) { try {//from w w w. j a v a 2 s. co m DefaultFontMapper mapper = new DefaultFontMapper(); FontFactory.registerDirectories(); mapper.insertDirectory("c:\\winnt\\fonts"); // mapper.insertDirectory("c:\\windows\\fonts"); // we create a template and a Graphics2D object that corresponds // with it int margin = 72; // 1 inch float scale = 0.5f; boolean multiple_page = true; Rectangle page_size; if (multiple_page) { page_size = PageSize.LETTER.rotate(); } else { page_size = new Rectangle((int) (graphsize.getWidth() * scale) + margin, (int) (graphsize.getHeight() * scale) + margin); } Document document = new Document(page_size); DocWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); document.open(); document.setPageSize(page_size); int image_w = (int) page_size.getWidth() - margin; int image_h = (int) page_size.getHeight() - margin; boolean edsave = canvas.editable; canvas.editable = false; Color bgsave = canvas.getBackground(); canvas.setBackground(Color.white); if (multiple_page) { int horizontal_pages = (int) (graphsize.width * scale) / image_w + 1; int vertical_pages = (int) (graphsize.height * scale) / image_h + 1; for (int i = 0; i < horizontal_pages; i++) { for (int j = 0; j < vertical_pages; j++) { Image img; PdfContentByte cb = ((PdfWriter) writer).getDirectContent(); PdfTemplate tp = cb.createTemplate(image_w, image_h); Graphics2D g2 = tp.createGraphics(image_w, image_h, mapper); tp.setWidth(image_w); tp.setHeight(image_h); g2.scale(scale, scale); g2.translate(-i * image_w / scale, -j * image_h / scale); canvas.paintComponent(g2); g2.dispose(); img = new ImgTemplate(tp); document.add(img); } } } else { Image img; PdfContentByte cb = ((PdfWriter) writer).getDirectContent(); PdfTemplate tp = cb.createTemplate(image_w, image_h); Graphics2D g2 = tp.createGraphics(image_w, image_h, mapper); tp.setWidth(image_w); tp.setHeight(image_h); g2.scale(scale, scale); canvas.paintComponent(g2); g2.dispose(); img = new ImgTemplate(tp); document.add(img); } canvas.setBackground(bgsave); canvas.editable = edsave; document.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.centurylink.mdw.designer.pages.ExportHelper.java
License:Apache License
private void printGraphPdf(DocWriter writer, CanvasCommon canvas, Graph process, Rectangle page_size, String type, String filename, Chapter chapter, int chapter_number) throws Exception { Dimension graphsize = process.getGraphSize(); // we create a fontMapper and read all the fonts in the font directory DefaultFontMapper mapper = new DefaultFontMapper(); FontFactory.registerDirectories();//w ww . j ava 2s . co m mapper.insertDirectory("c:\\winnt\\fonts"); // mapper.insertDirectory("c:\\windows\\fonts"); // we create a template and a Graphics2D object that corresponds with it int w, h; float scale; if ((float) graphsize.width < page_size.getWidth() * 0.8 && (float) graphsize.height < page_size.getHeight() * 0.8 || type.equals(HTML)) { w = graphsize.width + 36; h = graphsize.height + 36; scale = -1f; } else { scale = page_size.getWidth() * 0.8f / (float) graphsize.width; if (scale > page_size.getHeight() * 0.8f / (float) graphsize.height) scale = page_size.getHeight() * 0.8f / (float) graphsize.height; w = (int) (graphsize.width * scale) + 36; h = (int) (graphsize.height * scale) + 36; } Image img; int zoomSave = process.zoom; process.zoom = 100; Color bgsave = canvas.getBackground(); boolean edsave = canvas.editable; canvas.editable = false; canvas.setBackground(Color.white); if (type.equals(PDF)) { PdfContentByte cb = ((PdfWriter) writer).getDirectContent(); PdfTemplate tp = cb.createTemplate(w, h); Graphics2D g2 = tp.createGraphics(w, h, mapper); if (scale > 0) g2.scale(scale, scale); tp.setWidth(w); tp.setHeight(h); canvas.paintComponent(g2); g2.dispose(); // cb.addTemplate(tp, 50, 400); img = new ImgTemplate(tp); } else { String imgfilename = filename + "." + process.getName() + "_ch" + chapter_number + ".jpg"; printImage(imgfilename, -1f, canvas, graphsize); img = Image.getInstance(imgfilename); if (scale > 0) img.scalePercent(scale * 100); } process.zoom = zoomSave; canvas.setBackground(bgsave); canvas.editable = edsave; if (img != null) chapter.add(img); }