List of usage examples for com.itextpdf.text.pdf PdfContentByte restoreState
public void restoreState()
From source file:mymoney.view.java
private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed getContentPane().setLayout(new BorderLayout()); DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); JFileChooser cho = new JFileChooser(); cho.showSaveDialog(null);//from w w w. j a v a 2 s . c om File f = cho.getSelectedFile(); String filename = f.getAbsolutePath(); com.itextpdf.text.Document document = new com.itextpdf.text.Document(); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename + ".pdf")); document.open(); PdfContentByte cb = writer.getDirectContent(); cb.saveState(); PdfTemplate pdfTemplate = cb.createTemplate(jTable1.getWidth(), jTable1.getHeight()); Graphics2D g2 = cb.createGraphicsShapes(800, 500); Shape oldClip = g2.getClip(); g2.clipRect(0, 0, 800, 500); jTable1.print(g2); g2.setClip(oldClip); g2.dispose(); cb.restoreState(); JOptionPane.showMessageDialog(null, "Data Exported to pdf"); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Error"); } document.close(); // TODO add your handling code here: }
From source file:mymoney.view.java
private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton10ActionPerformed getContentPane().setLayout(new BorderLayout()); DefaultTableModel model = (DefaultTableModel) jTable2.getModel(); JFileChooser cho = new JFileChooser(); cho.showSaveDialog(null);// w ww . j a va 2 s .c o m File f = cho.getSelectedFile(); String filename = f.getAbsolutePath(); com.itextpdf.text.Document document = new com.itextpdf.text.Document(); try { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename + ".pdf")); document.open(); PdfContentByte cb = writer.getDirectContent(); cb.saveState(); PdfTemplate pdfTemplate = cb.createTemplate(jTable2.getWidth(), jTable2.getHeight()); Graphics2D g2 = cb.createGraphicsShapes(800, 500); Shape oldClip = g2.getClip(); g2.clipRect(0, 0, 800, 500); jTable2.print(g2); g2.setClip(oldClip); g2.dispose(); cb.restoreState(); JOptionPane.showMessageDialog(null, "Data Exported to pdf"); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Error"); } document.close(); // TODO add your handling code here: }
From source file:nz.ac.waikato.cms.doc.SimplePDFOverlay.java
License:Open Source License
/** * Applies the instructions to the input PDF. * * @return null if successful, otherwise error message *//*from ww w . ja v a 2 s. c o m*/ public String execute() { String result; String line; BufferedReader breader; FileReader freader; int i; int lineNo; String units; int pageNo; PdfReader reader; PdfStamper stamper; PdfContentByte cb; ColumnText ct; Font font; String[] parts; StringBuilder text; result = null; freader = null; breader = null; try { reader = new PdfReader(new FileInputStream(m_Pdf.getAbsolutePath())); stamper = new PdfStamper(reader, new FileOutputStream(m_Output.getAbsolutePath())); freader = new FileReader(m_Instructions); breader = new BufferedReader(freader); lineNo = 0; units = "pt"; pageNo = 1; cb = stamper.getOverContent(pageNo); font = null; while ((line = breader.readLine()) != null) { lineNo++; if (line.trim().startsWith(PREFIX_COMMENT)) continue; if (line.trim().length() == 0) continue; if (line.startsWith(PREFIX_UNITS)) { units = line.substring(PREFIX_UNITS.length()).trim().toLowerCase(); } else if (line.startsWith(PREFIX_PAGE)) { pageNo = Integer.parseInt(line.substring(PREFIX_PAGE.length()).trim()); cb = stamper.getOverContent(pageNo); } else if (line.startsWith(PREFIX_FONT)) { parts = line.substring(PREFIX_FONT.length()).trim().split(" "); if (parts.length == 3) font = FontFactory.getFont(parts[0], Float.parseFloat(parts[1]), new BaseColor(parseColor(parts[2]).getRGB())); else m_Logger.warning("Font instruction not in expected format (" + FORMAT_FONT + "):\n" + line); } else if (line.startsWith(PREFIX_TEXT)) { parts = line.substring(PREFIX_TEXT.length()).trim().split(" "); if (parts.length >= 7) { ct = new ColumnText(cb); ct.setSimpleColumn(parseLocation(parts[0], reader.getPageSize(pageNo).getWidth(), units), // llx parseLocation(parts[1], reader.getPageSize(pageNo).getHeight(), units), // lly parseLocation(parts[2], reader.getPageSize(pageNo).getWidth(), units), // urx parseLocation(parts[3], reader.getPageSize(pageNo).getHeight(), units), // ury Float.parseFloat(parts[4]), // leading parseAlignment(parts[5])); // alignment text = new StringBuilder(); for (i = 6; i < parts.length; i++) { if (text.length() > 0) text.append(" "); text.append(parts[i]); } if (font == null) ct.setText(new Phrase(text.toString())); else ct.setText(new Phrase(text.toString(), font)); ct.go(); } else { m_Logger.warning("Text instruction not in expected format (" + FORMAT_TEXT + "):\n" + line); } } else if (line.startsWith(PREFIX_LINE)) { parts = line.substring(PREFIX_LINE.length()).trim().split(" "); if (parts.length >= 6) { cb.saveState(); cb.setLineWidth(Float.parseFloat(parts[4])); // line width cb.setColorStroke(new BaseColor(parseColor(parts[5]).getRGB())); // color cb.moveTo(parseLocation(parts[0], reader.getPageSize(pageNo).getWidth(), units), // x parseLocation(parts[1], reader.getPageSize(pageNo).getWidth(), units)); // y cb.lineTo(parseLocation(parts[2], reader.getPageSize(pageNo).getWidth(), units), // w parseLocation(parts[3], reader.getPageSize(pageNo).getWidth(), units)); // h cb.stroke(); cb.restoreState(); } else { m_Logger.warning("Line instruction not in expected format (" + FORMAT_LINE + "):\n" + line); } } else if (line.startsWith(PREFIX_RECT)) { parts = line.substring(PREFIX_RECT.length()).trim().split(" "); if (parts.length >= 6) { cb.saveState(); cb.rectangle(parseLocation(parts[0], reader.getPageSize(pageNo).getWidth(), units), // x parseLocation(parts[1], reader.getPageSize(pageNo).getWidth(), units), // y parseLocation(parts[2], reader.getPageSize(pageNo).getWidth(), units), // w parseLocation(parts[3], reader.getPageSize(pageNo).getWidth(), units) // h ); cb.setLineWidth(Float.parseFloat(parts[4])); // line width cb.setColorStroke(new BaseColor(parseColor(parts[5]).getRGB())); // stroke if (parts.length >= 7) { cb.setColorFill(new BaseColor(parseColor(parts[6]).getRGB())); // fill cb.fillStroke(); } else { cb.stroke(); } cb.restoreState(); } else { m_Logger.warning( "Rectangle instruction not in expected format (" + FORMAT_RECT + "):\n" + line); } } else if (line.startsWith(PREFIX_OVAL)) { parts = line.substring(PREFIX_OVAL.length()).trim().split(" "); if (parts.length >= 6) { cb.saveState(); cb.ellipse(parseLocation(parts[0], reader.getPageSize(pageNo).getWidth(), units), // x1 parseLocation(parts[1], reader.getPageSize(pageNo).getWidth(), units), // y1 parseLocation(parts[2], reader.getPageSize(pageNo).getWidth(), units), // x2 parseLocation(parts[3], reader.getPageSize(pageNo).getWidth(), units) // y2 ); cb.setLineWidth(Float.parseFloat(parts[4])); // line width cb.setColorStroke(new BaseColor(parseColor(parts[5]).getRGB())); // stroke if (parts.length >= 7) { cb.setColorFill(new BaseColor(parseColor(parts[6]).getRGB())); // fill cb.fillStroke(); } else { cb.stroke(); } cb.restoreState(); } else { m_Logger.warning("Oval instruction not in expected format (" + FORMAT_OVAL + "):\n" + line); } } else { m_Logger.warning("Unknown command on line #" + lineNo + ":\n" + line); } } stamper.close(); } catch (Exception e) { result = "Failed to process!\n" + Utils.throwableToString(e); } finally { FileUtils.closeQuietly(breader); FileUtils.closeQuietly(freader); } return result; }
From source file:org.durel.mydooble.PDF.java
License:Open Source License
public void newCard() { col++;// w w w.j a v a2 s . c om if (col > (COL_NUM - 1)) { col = 0; row++; if (row > (ROW_NUM - 1)) newPage(); } float x1 = col * (WIDTH + MARGIN) + MARGIN; float x2 = (col + 1) * (WIDTH + MARGIN); float y2 = (row + 1) * (HEIGHT + MARGIN); float y1 = row * (HEIGHT + MARGIN) + MARGIN; cardBox = new Rectangle(x1, y1, x2, y2); try { PdfContentByte cb = writer.getDirectContent(); cb.saveState(); cardBox.setBorderWidth(1.2f); cardBox.setBorderColor(BaseColor.BLACK); cardBox.setBorder(Rectangle.BOX); cb.rectangle(cardBox); cb.stroke(); cb.restoreState(); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.frobic.colorednodes.renderers.NewEdgeRenderer.java
License:Open Source License
@Override public void renderSelfLoop(Item nodeItem, float thickness, Color color, PreviewProperties properties, RenderTarget renderTarget) {//from w w w .j a v a 2s .co m Float x = nodeItem.getData(NodeItem.X); Float y = nodeItem.getData(NodeItem.Y); Float size = nodeItem.getData(NodeItem.SIZE); Node node = (Node) nodeItem.getSource(); PVector v1 = new PVector(x, y); v1.add(size, -size, 0); PVector v2 = new PVector(x, y); v2.add(size, size, 0); if (renderTarget instanceof ProcessingTarget) { PGraphics graphics = ((ProcessingTarget) renderTarget).getGraphics(); graphics.strokeWeight(thickness); graphics.stroke(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()); graphics.noFill(); graphics.bezier(x, y, v1.x, v1.y, v1.x, v2.y, x, y); } else if (renderTarget instanceof SVGTarget) { SVGTarget svgTarget = (SVGTarget) renderTarget; Element selfLoopElem = svgTarget.createElement("path"); selfLoopElem.setAttribute("d", String.format(Locale.ENGLISH, "M %f,%f C %f,%f %f,%f %f,%f", x, y, v1.x, v1.y, v2.x, v2.y, x, y)); selfLoopElem.setAttribute("class", node.getNodeData().getId()); selfLoopElem.setAttribute("stroke", svgTarget.toHexString(color)); selfLoopElem.setAttribute("stroke-opacity", (color.getAlpha() / 255f) + ""); selfLoopElem.setAttribute("stroke-width", Float.toString(thickness * svgTarget.getScaleRatio())); selfLoopElem.setAttribute("fill", "none"); svgTarget.getTopElement(SVGTarget.TOP_EDGES).appendChild(selfLoopElem); } else if (renderTarget instanceof PDFTarget) { PDFTarget pdfTarget = (PDFTarget) renderTarget; PdfContentByte cb = pdfTarget.getContentByte(); cb.moveTo(x, -y); cb.curveTo(v1.x, -v1.y, v2.x, -v2.y, x, -y); cb.setRGBColorStroke(color.getRed(), color.getGreen(), color.getBlue()); cb.setLineWidth(thickness); if (color.getAlpha() < 255) { cb.saveState(); float alpha = color.getAlpha() / 255f; PdfGState gState = new PdfGState(); gState.setStrokeOpacity(alpha); cb.setGState(gState); } cb.stroke(); if (color.getAlpha() < 255) { cb.restoreState(); } } }
From source file:org.frobic.colorednodes.renderers.NewEdgeRenderer.java
License:Open Source License
@Override public void renderCurvedEdge(Item edgeItem, Item sourceItem, Item targetItem, float thickness, Color color, PreviewProperties properties, RenderTarget renderTarget) { Edge edge = (Edge) edgeItem.getSource(); Float x1 = sourceItem.getData(NodeItem.X); Float x2 = targetItem.getData(NodeItem.X); Float y1 = sourceItem.getData(NodeItem.Y); Float y2 = targetItem.getData(NodeItem.Y); //Curved edgs PVector direction = new PVector(x2, y2); direction.sub(new PVector(x1, y1)); float length = direction.mag(); direction.normalize();//from w ww . jav a2s .c o m float factor = properties.getFloatValue(BEZIER_CURVENESS) * length; // normal vector to the edge PVector n = new PVector(direction.y, -direction.x); n.mult(factor); // first control point PVector v1 = new PVector(direction.x, direction.y); v1.mult(factor); v1.add(new PVector(x1, y1)); v1.add(n); // second control point PVector v2 = new PVector(direction.x, direction.y); v2.mult(-factor); v2.add(new PVector(x2, y2)); v2.add(n); if (renderTarget instanceof ProcessingTarget) { PGraphics graphics = ((ProcessingTarget) renderTarget).getGraphics(); graphics.strokeWeight(thickness); graphics.stroke(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()); graphics.noFill(); graphics.bezier(x1, y1, v1.x, v1.y, v2.x, v2.y, x2, y2); } else if (renderTarget instanceof SVGTarget) { SVGTarget svgTarget = (SVGTarget) renderTarget; Element edgeElem = svgTarget.createElement("path"); edgeElem.setAttribute("class", edge.getSource().getNodeData().getId() + " " + edge.getTarget().getNodeData().getId()); edgeElem.setAttribute("d", String.format(Locale.ENGLISH, "M %f,%f C %f,%f %f,%f %f,%f", x1, y1, v1.x, v1.y, v2.x, v2.y, x2, y2)); edgeElem.setAttribute("stroke", svgTarget.toHexString(color)); edgeElem.setAttribute("stroke-width", Float.toString(thickness * svgTarget.getScaleRatio())); edgeElem.setAttribute("stroke-opacity", (color.getAlpha() / 255f) + ""); edgeElem.setAttribute("fill", "none"); svgTarget.getTopElement(SVGTarget.TOP_EDGES).appendChild(edgeElem); } else if (renderTarget instanceof PDFTarget) { PDFTarget pdfTarget = (PDFTarget) renderTarget; PdfContentByte cb = pdfTarget.getContentByte(); cb.moveTo(x1, -y1); cb.curveTo(v1.x, -v1.y, v2.x, -v2.y, x2, -y2); cb.setRGBColorStroke(color.getRed(), color.getGreen(), color.getBlue()); cb.setLineWidth(thickness); if (color.getAlpha() < 255) { cb.saveState(); float alpha = color.getAlpha() / 255f; PdfGState gState = new PdfGState(); gState.setStrokeOpacity(alpha); cb.setGState(gState); } cb.stroke(); if (color.getAlpha() < 255) { cb.restoreState(); } } }
From source file:org.frobic.colorednodes.renderers.NewEdgeRenderer.java
License:Open Source License
@Override public void renderStraightEdge(Item edgeItem, Item sourceItem, Item targetItem, float thickness, Color color, PreviewProperties properties, RenderTarget renderTarget) { Edge edge = (Edge) edgeItem.getSource(); Float x1 = sourceItem.getData(NodeItem.X); Float x2 = targetItem.getData(NodeItem.X); Float y1 = sourceItem.getData(NodeItem.Y); Float y2 = targetItem.getData(NodeItem.Y); //Target radius - to start at the base of the arrow Float targetRadius = edgeItem.getData(TARGET_RADIUS); //Avoid edge from passing the node's center: if (targetRadius != null && targetRadius < 0) { PVector direction = new PVector(x2, y2); direction.sub(new PVector(x1, y1)); direction.normalize();//from w w w. j a v a 2 s . c o m direction = new PVector(direction.x, direction.y); direction.mult(targetRadius); direction.add(new PVector(x2, y2)); x2 = direction.x; y2 = direction.y; } //Source radius Float sourceRadius = edgeItem.getData(SOURCE_RADIUS); //Avoid edge from passing the node's center: if (sourceRadius != null && sourceRadius < 0) { PVector direction = new PVector(x1, y1); direction.sub(new PVector(x2, y2)); direction.normalize(); direction = new PVector(direction.x, direction.y); direction.mult(sourceRadius); direction.add(new PVector(x1, y1)); x1 = direction.x; y1 = direction.y; } if (renderTarget instanceof ProcessingTarget) { PGraphics graphics = ((ProcessingTarget) renderTarget).getGraphics(); graphics.strokeWeight(thickness); graphics.strokeCap(PGraphics.SQUARE); graphics.stroke(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()); graphics.noFill(); graphics.line(x1, y1, x2, y2); } else if (renderTarget instanceof SVGTarget) { SVGTarget svgTarget = (SVGTarget) renderTarget; Element edgeElem = svgTarget.createElement("path"); edgeElem.setAttribute("class", edge.getSource().getNodeData().getId() + " " + edge.getTarget().getNodeData().getId()); edgeElem.setAttribute("d", String.format(Locale.ENGLISH, "M %f,%f L %f,%f", x1, y1, x2, y2)); edgeElem.setAttribute("stroke", svgTarget.toHexString(color)); edgeElem.setAttribute("stroke-width", Float.toString(thickness * svgTarget.getScaleRatio())); edgeElem.setAttribute("stroke-opacity", (color.getAlpha() / 255f) + ""); edgeElem.setAttribute("fill", "none"); svgTarget.getTopElement(SVGTarget.TOP_EDGES).appendChild(edgeElem); } else if (renderTarget instanceof PDFTarget) { PDFTarget pdfTarget = (PDFTarget) renderTarget; PdfContentByte cb = pdfTarget.getContentByte(); cb.moveTo(x1, -y1); cb.lineTo(x2, -y2); cb.setRGBColorStroke(color.getRed(), color.getGreen(), color.getBlue()); cb.setLineWidth(thickness); if (color.getAlpha() < 255) { cb.saveState(); float alpha = color.getAlpha() / 255f; PdfGState gState = new PdfGState(); gState.setStrokeOpacity(alpha); cb.setGState(gState); } cb.stroke(); if (color.getAlpha() < 255) { cb.restoreState(); } } }
From source file:org.frobic.colorednodes.renderers.NewNodeRenderer.java
License:Open Source License
@Override public void renderPDF(Item item, PDFTarget target, PreviewProperties properties) { Float x = item.getData(NodeItem.X); Float y = item.getData(NodeItem.Y); Float size = item.getData(NodeItem.SIZE); Float angle = item.getData(NodeItem.ANGLE); size /= 2f;/*w w w . ja v a 2 s . c o m*/ Color color = item.getData(NodeItem.COLOR); Integer nbcolors = item.getData(NodeItem.NBCOLOR); Color[] colors = item.getData(NodeItem.COLORS); Color borderColor = ((DependantColor) properties.getValue(PreviewProperty.NODE_BORDER_COLOR)) .getColor(color); float borderSize = properties.getFloatValue(PreviewProperty.NODE_BORDER_WIDTH); float alpha = properties.getIntValue(PreviewProperty.NODE_OPACITY) / 100f; PdfContentByte cb = target.getContentByte(); cb.setRGBColorStroke(borderColor.getRed(), borderColor.getGreen(), borderColor.getBlue()); cb.setLineWidth(borderSize); cb.setRGBColorFill(color.getRed(), color.getGreen(), color.getBlue()); if (alpha < 1f) { cb.saveState(); PdfGState gState = new PdfGState(); gState.setFillOpacity(alpha); gState.setStrokeOpacity(alpha); cb.setGState(gState); } for (int i = 0; i < nbcolors; i++) { cb.setRGBColorFill(colors[nbcolors - i - 1].getRed(), colors[nbcolors - i - 1].getGreen(), colors[nbcolors - i - 1].getBlue()); if (size >= 0.5) { cb.newPath(); ArrayList ar = cb.bezierArc(x - size, -y + size, x + size, 0 - size - y, 360f * (nbcolors - i - 1) / nbcolors + (360f / 6.28f) * angle, 360f * (1) / nbcolors); //cb.arc(x-size,-y+size,x+size,0-size-y,360f*(nbcolors-i-1)/nbcolors+angle,360f*(1)/nbcolors) ; cb.moveTo(x, -y); float pt[] = (float[]) ar.get(0); cb.moveTo(pt[0], pt[1]); for (int k = 0; k < ar.size(); ++k) { pt = (float[]) ar.get(k); cb.curveTo(pt[2], pt[3], pt[4], pt[5], pt[6], pt[7]); } cb.lineTo(x, -y); //strokeAndFill(); // cb.ClosePathFillStroke(); if (borderSize > 0) { cb.fill(); } else { cb.fill(); } if (borderSize > 0) { cb.circle(x, -y, size); cb.stroke(); } } } if (alpha < 1f) { cb.restoreState(); } }
From source file:org.gephi.edgelayout.api.SubdividedEdgeRenderer.java
License:Open Source License
private void renderSimplePDFItem(SubdividedEdgeItem item, RenderTarget target, PreviewProperties properties, Point2D.Double[] points) { Color color = new Color(originalColor.getRed(), originalColor.getGreen(), originalColor.getBlue(), (int) (255 * alpha)); PDFTarget pdfTarget = (PDFTarget) target; PdfContentByte cb = pdfTarget.getContentByte(); for (int i = 0; i < points.length - 1; i++) { cb.moveTo((float) points[i].x, (float) points[i].y); cb.lineTo((float) points[i + 1].x, (float) points[i + 1].y); }/*from w w w.j a v a 2 s .com*/ cb.setRGBColorStroke(color.getRed(), color.getGreen(), color.getBlue()); cb.setLineWidth(thickness); if (color.getAlpha() < 255) { cb.saveState(); float alpha = color.getAlpha() / 255f; PdfGState gState = new PdfGState(); gState.setStrokeOpacity(alpha); cb.setGState(gState); } cb.stroke(); if (color.getAlpha() < 255) { cb.restoreState(); } }
From source file:org.gephi.edgelayout.api.SubdividedEdgeRenderer.java
License:Open Source License
private void renderBigPDFItem(SubdividedEdgeBigItem item, RenderTarget target, PreviewProperties properties) { for (SortedEdgeWrapper edgeWrapper : item.edges) { Edge edge = edgeWrapper.edge;//from ww w . j a v a 2 s .c om EdgeLayoutData data = (EdgeLayoutData) edge.getEdgeData().getLayoutData(); Point2D.Double[] points = data.getSubdivisonPoints(); if (data.getEdgeColor() == null || points == null) { continue; } Color color = new Color(data.getEdgeColor().getRed(), data.getEdgeColor().getGreen(), data.getEdgeColor().getBlue(), (int) (255 * alpha)); PDFTarget pdfTarget = (PDFTarget) target; PdfContentByte cb = pdfTarget.getContentByte(); for (int i = 0; i < points.length - 1; i++) { cb.moveTo((float) points[i].x, (float) points[i].y); cb.lineTo((float) points[i + 1].x, (float) points[i + 1].y); } cb.setRGBColorStroke(color.getRed(), color.getGreen(), color.getBlue()); cb.setLineWidth(thickness); float usedAlpha = (forceAlpha ? intAlpha : color.getAlpha()); if (usedAlpha < 255) { cb.saveState(); float alpha = usedAlpha / 255f; PdfGState gState = new PdfGState(); gState.setStrokeOpacity(alpha); cb.setGState(gState); } cb.stroke(); if (usedAlpha < 255) { cb.restoreState(); } } }