List of usage examples for com.itextpdf.text.pdf PdfContentByte endLayer
public void endLayer()
From source file:com.vectorprint.report.itext.debug.DebugHelper.java
License:Open Source License
public static void debugBackground(PdfContentByte canvas, Rectangle rect, BaseColor color, String prefix, EnhancedMap settings, LayerManager layerAware) { canvas = canvas.getPdfWriter().getDirectContentUnder(); int rgb = color.getRed() + color.getBlue() + color.getGreen(); rect.setBackgroundColor(color);// w w w . j ava 2 s . c om canvas.rectangle(rect); layerAware.startLayerInGroup(DEBUG, canvas); debugFont(canvas, settings); BaseColor txtColor = (rgb < 150) ? color.brighter().brighter() : color.darker().darker(); canvas.setColorFill(txtColor); canvas.setColorStroke(txtColor); canvas.beginText(); canvas.showTextAligned(Element.ALIGN_LEFT, prefix + color.toString().replace(Color.class.getName(), ""), rect.getLeft() + rect.getWidth() / 2, rect.getTop() - rect.getHeight() / 2, 0); canvas.endText(); canvas.endLayer(); }
From source file:com.vectorprint.report.itext.debug.DebugHelper.java
License:Open Source License
public static void debugRect(PdfContentByte canvas, Rectangle rect, float[] dash, float borderWidth, EnhancedMap settings, LayerManager layerAware) { layerAware.startLayerInGroup(DEBUG, canvas); debugFont(canvas, settings);/*from w w w. ja va 2 s. c o m*/ canvas.setLineWidth(borderWidth); canvas.setLineDash(dash, 0); canvas.moveTo(rect.getLeft(), rect.getBottom()); canvas.lineTo(rect.getRight(), rect.getBottom()); canvas.lineTo(rect.getRight(), rect.getTop()); canvas.lineTo(rect.getLeft(), rect.getTop()); canvas.closePathStroke(); ItextHelper.resetLineDash(canvas); canvas.endLayer(); }
From source file:com.vectorprint.report.itext.debug.DebugHelper.java
License:Open Source License
public static void styleLink(PdfContentByte canvas, String styleClass, String extraInfo, float x, float y, EnhancedMap settings, LayerManager layerAware) { if (styleClass == null) { log.warning("not showing link to styleClass because there is no styleClass"); return;//from w w w. j a v a2s. com } Font dbf = DebugHelper.debugFontLink(canvas, settings); layerAware.startLayerInGroup(DEBUG, canvas); PdfAction act = PdfAction.gotoLocalPage(styleClass, true); Chunk c = new Chunk(styleClass + extraInfo, dbf); float w = ItextHelper.getTextWidth(c); float h = ItextHelper.getTextHeight(c); float tan = (float) Math.tan(Math.toRadians(8)); canvas.setAction(act, x, y, x + w, y + h + tan * w); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(c), x, y, 8); canvas.endLayer(); }
From source file:com.vectorprint.report.itext.DefaultElementProducer.java
License:Open Source License
@Override public PdfLayer initLayerGroup(String layerId, PdfContentByte canvas) { if (!layerGroups.containsKey(layerId)) { PdfLayer parent;/*w w w . ja va 2s . c o m*/ try { parent = new PdfLayer(layerId, canvas.getPdfWriter()); } catch (IOException ex) { throw new VectorPrintRuntimeException(ex); } layerGroups.put(layerId, parent); canvas.beginLayer(parent); canvas.endLayer(); } return layerGroups.get(layerId); }
From source file:com.vectorprint.report.itext.style.stylers.AdvancedImpl.java
License:Open Source License
@Override public void resetCanvas(PdfContentByte canvas) { if (needRestore) { canvas.restoreState();/*from w w w .j ava 2s. c o m*/ needRestore = false; } if (getLayerName() != null) { canvas.endLayer(); } }
From source file:com.vectorprint.report.itext.TocOutputStream.java
License:Open Source License
private void printToc(Document d, PdfWriter w, VectorPrintDocument vpd) throws VectorPrintException, InstantiationException, IllegalAccessException, DocumentException { DocumentStyler ds = outer.getStylerFactory().getDocumentStyler(); if (ds.getValue(DocumentSettings.TOCAPPEND, Boolean.class)) { d.add(Chunk.NEXTPAGE);//from w ww . j av a 2s . c om } if (outer.isWasDebug()) { outer.getSettings().put(ReportConstants.DEBUG, Boolean.TRUE.toString()); PdfContentByte canvas = w.getDirectContent(); outer.startLayerInGroup(ReportConstants.DEBUG, canvas); BaseFont bf = DebugHelper.debugFont(canvas, outer.getSettings()); canvas.showTextAligned(Element.ALIGN_RIGHT, "FOR DEBUG INFO IN THE DOCUMENT TURN OFF TOC (-DocumentSettings.toc=false)", d.right(), d.getPageSize().getHeight() - ItextHelper.getTextHeight("F", bf, 8), 0); canvas.endLayer(); } ElementProducer ep = outer.getElementProducer(); StylerFactory sf = outer.getStylerFactory(); PdfPTable tocTable = ep.createElement(null, PdfPTable.class, sf.getStylers(DocumentSettings.TOCTABLEKEY)); for (Map.Entry<Integer, List<Section>> e : vpd.getToc().entrySet()) { String link = null; for (Section s : e.getValue()) { if (ds.isParameterSet(DocumentSettings.TOCMAXDEPTH) && ds.getValue(DocumentSettings.TOCMAXDEPTH, Integer.class) < s.getDepth()) { continue; } if (link == null) { link = s.getTitle().getContent(); } Chunk c = ep.createElement(s.getTitle().getContent(), Chunk.class, sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY)); if (ds.getValue(DocumentSettings.TOCDOTS, Boolean.class)) { float tw = ItextHelper.getTextWidth(c); float cw = tocTable.getAbsoluteWidths()[0]; float dw = ItextHelper.getTextWidth( ep.createElement(".", Chunk.class, sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY))) * 1.5f; int numDots = (int) ((cw > tw) ? (cw - tw) / dw : 0); char[] dots = new char[numDots]; Arrays.fill(dots, '.'); c = ep.createElement(s.getTitle().getContent() + " " + String.valueOf(dots), Chunk.class, sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY)); } c.setLocalGoto(link); tocTable.addCell( ep.createElement(c, PdfPCell.class, sf.getStylers(DocumentSettings.TOCTITLESTYLEKEY))); c = ep.createElement(e.getKey(), Chunk.class, sf.getStylers(DocumentSettings.TOCNRSTYLEKEY)); c.setLocalGoto(link); tocTable.addCell( ep.createElement(c, PdfPCell.class, sf.getStylers(DocumentSettings.TOCNRSTYLEKEY))); } } d.add(tocTable); }
From source file:org.orbisgis.core_export.GeoSpatialPDF.java
License:Open Source License
/** * Process all layers in the layermodel and build the corresponding pdf tree * layer// w ww. j a v a2 s . co m * * @param layer */ private void processSubLayer(ILayer layer, MapTransform mt, PdfWriter writer, PdfContentByte cb, ProgressMonitor pm, PdfLayer mainLayer) { if (layer.acceptsChilds() && layer.getLayerCount() > 0) { PdfLayer groupLayer = new PdfLayer(layer.getName(), writer); //To manage group of layers for (ILayer subLayer : layer.getChildren()) { processSubLayer(subLayer, mt, writer, cb, pm, groupLayer); } } else { if (layer.isVisible() && layer.getEnvelope().intersects(mt.getAdjustedExtent())) { PdfLayer mapLayer = new PdfLayer(layer.getName(), writer); if (mainLayer != null) { mainLayer.addChild(mapLayer); } PdfTemplate layerTemplate = cb.createTemplate(width, height); Graphics2D g2dLayer = layerTemplate.createGraphics(width, height); cb.beginLayer(mapLayer); PdfRenderer renderer2 = new PdfRenderer(layerTemplate, width, height); renderer2.draw(mt, g2dLayer, (int) width, (int) height, layer, pm); cb.addTemplate(layerTemplate, 0, 0); g2dLayer.dispose(); cb.endLayer(); } } }
From source file:org.orbisgis.mapcomposer.controller.utils.exportThreads.ExportPDFThread.java
License:Open Source License
@Override public void run() { try {//from ww w. j a v a 2 s . com Document pdfDocument = null; //Find the Document GE to create the BufferedImage where all the GE will be drawn for (GraphicalElement ge : geIsVectorMap.keySet()) { if (ge instanceof org.orbisgis.mapcomposer.model.graphicalelement.element.Document) { pdfDocument = new Document(new Rectangle(ge.getWidth(), ge.getHeight())); height = ge.getHeight(); } } //If no Document was created, throw an exception if (pdfDocument == null) { throw new IllegalArgumentException(i18n.tr( "Error on export : The list of GraphicalElement to export does not contain any Document GE.")); } //Open the document PdfWriter writer = PdfWriter.getInstance(pdfDocument, new FileOutputStream(path)); writer.setUserProperties(true); writer.setRgbTransparencyBlending(true); writer.setTagged(); pdfDocument.open(); PdfContentByte cb = writer.getDirectContent(); progressBar.setIndeterminate(true); progressBar.setStringPainted(true); progressBar.setString(i18n.tr("Exporting the document ...")); int geCount = 0; int numberOfGe[] = new int[geManager.getRegisteredGEClasses().size()]; for (int i = 0; i < numberOfGe.length; i++) { numberOfGe[i] = 0; } //Draw each GraphicalElement in the BufferedImage for (GraphicalElement ge : geStack) { if ((ge instanceof org.orbisgis.mapcomposer.model.graphicalelement.element.Document)) continue; double rad = Math.toRadians(ge.getRotation()); double newHeight = Math.abs(sin(rad) * ge.getWidth()) + Math.abs(cos(rad) * ge.getHeight()); double newWidth = Math.abs(sin(rad) * ge.getHeight()) + Math.abs(cos(rad) * ge.getWidth()); int maxWidth = Math.max((int) newWidth, ge.getWidth()); int maxHeight = Math.max((int) newHeight, ge.getHeight()); String layerName = ge.getGEName() + numberOfGe[geManager.getRegisteredGEClasses().indexOf(ge.getClass())]; if (geIsVectorMap.get(ge)) { PdfTemplate pdfTemplate = cb.createTemplate(maxWidth, maxHeight); Graphics2D g2dTemplate = pdfTemplate.createGraphics(maxWidth, maxHeight); PdfLayer layer = new PdfLayer(layerName, writer); cb.beginLayer(layer); ((RendererVector) geManager.getRenderer(ge.getClass())).drawGE(g2dTemplate, ge); cb.addTemplate(pdfTemplate, ge.getX() + (ge.getWidth() - maxWidth) / 2, -ge.getY() + height - ge.getHeight() + (ge.getHeight() - maxHeight) / 2); g2dTemplate.dispose(); cb.endLayer(); } else { ByteArrayOutputStream baos = new ByteArrayOutputStream(); BufferedImage bi = ((RendererRaster) geManager.getRenderer(ge.getClass())).createGEImage(ge, null); ImageIO.write(bi, "png", baos); Image image = Image.getInstance(baos.toByteArray()); image.setAbsolutePosition(ge.getX() + (ge.getWidth() - maxWidth) / 2, -ge.getY() + height - ge.getHeight() + (ge.getHeight() - maxHeight) / 2); PdfTemplate pdfTemplate = cb.createTemplate(maxWidth, maxHeight); Graphics2D g2dTemplate = pdfTemplate.createGraphics(maxWidth, maxHeight); PdfLayer layer = new PdfLayer(layerName, writer); cb.beginLayer(layer); g2dTemplate.drawImage(bi, 0, 0, null); cb.addTemplate(pdfTemplate, ge.getX() + (ge.getWidth() - maxWidth) / 2, -ge.getY() + height - ge.getHeight() + (ge.getHeight() - maxHeight) / 2); g2dTemplate.dispose(); cb.endLayer(); } numberOfGe[geManager.getRegisteredGEClasses().indexOf(ge.getClass())]++; progressBar.setIndeterminate(false); progressBar.setValue((geCount * 100) / geIsVectorMap.keySet().size()); progressBar.revalidate(); geCount++; } pdfDocument.close(); //Wait a bit before erasing the progress bar progressBar.setValue(progressBar.getMaximum()); progressBar.setString(i18n.tr("Document successfully exported.")); try { Thread.sleep(1500); } catch (InterruptedException e) { LoggerFactory.getLogger(ExportPDFThread.class).error(e.getMessage()); } progressBar.setValue(0); progressBar.setStringPainted(false); } catch (IllegalArgumentException | IOException | DocumentException ex) { LoggerFactory.getLogger(ExportPDFThread.class).error(ex.getMessage()); } }