public void tableLayout(PdfPTable table, float[][] width, float[] height, int headerRows, int rowStart, PdfContentByte[] canvas) { float widths[] = width[0]; float x1 = widths[0]; float x2 = widths[widths.length - 1]; float y = height[height.length - 1]; PdfContentByte cb = canvas[PdfPTable.LINECANVAS]; cb.moveTo(x1, y); cb.lineTo(x2, y); cb.stroke(); }
/** * Draws a partial ellipse inscribed within the rectangle x1,y1,x2,y2, * starting at startAng degrees and covering extent degrees. Angles * start with 0 to the right (+x) and increase counter-clockwise. * * @param x1 a corner of the enclosing rectangle * @param y1 a corner of the enclosing rectangle * @param x2 a corner of the enclosing rectangle * @param y2 a corner of the enclosing rectangle * @param startAng starting angle in degrees * @param extent angle extent in degrees */ public void arc(float x1, float y1, float x2, float y2, float startAng, float extent) { ArrayList ar = bezierArc(x1, y1, x2, y2, startAng, extent); if (ar.isEmpty()) return; float pt[] = (float [])ar.get(0); moveTo(pt[0], pt[1]); for (int k = 0; k < ar.size(); ++k) { pt = (float [])ar.get(k); curveTo(pt[2], pt[3], pt[4], pt[5], pt[6], pt[7]); } }
PdfContentByte canvas = writer.getDirectContent(); canvas.moveTo(36, 36); canvas.lineTo(559, 36); canvas.stroke();
/** * Draws a partial ellipse inscribed within the rectangle x1,y1,x2,y2, * starting at startAng degrees and covering extent degrees. Angles * start with 0 to the right (+x) and increase counter-clockwise. * * @param x1 a corner of the enclosing rectangle * @param y1 a corner of the enclosing rectangle * @param x2 a corner of the enclosing rectangle * @param y2 a corner of the enclosing rectangle * @param startAng starting angle in degrees * @param extent angle extent in degrees */ public void arc(float x1, float y1, float x2, float y2, float startAng, float extent) { List<float[]> ar = bezierArc(x1, y1, x2, y2, startAng, extent); if (ar.isEmpty()) return; float[] pt = ar.get(0); moveTo(pt[0], pt[1]); for (float[] anAr : ar) { pt = anAr; curveTo(pt[2], pt[3], pt[4], pt[5], pt[6], pt[7]); } }
class DottedHeader implements PdfPTableEvent { public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) { PdfContentByte canvas = canvases[PdfPTable.LINECANVAS]; canvas.setLineDash(3f, 3f); float x1 = widths[0][0]; float x2 = widths[0][widths.length]; canvas.moveTo(x1, heights[0]); canvas.lineTo(x2, heights[0]); canvas.moveTo(x1, heights[headerRows]); canvas.lineTo(x2, heights[headerRows]); canvas.stroke(); } }
class DottedCells implements PdfPTableEvent { @Override public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) { PdfContentByte canvas = canvases[PdfPTable.LINECANVAS]; canvas.setLineDash(3f, 3f); float llx = widths[0][0]; float urx = widths[0][widths[0].length -1]; for (int i = 0; i < heights.length; i++) { canvas.moveTo(llx, heights[i]); canvas.lineTo(urx, heights[i]); } for (int i = 0; i < widths.length; i++) { for (int j = 0; j < widths[i].length; j++) { canvas.moveTo(widths[i][j], heights[i]); canvas.lineTo(widths[i][j], heights[i+1]); } } canvas.stroke(); } }
/** Draws a circle. The endpoint will (x+r, y). * * @param x x center of circle * @param y y center of circle * @param r radius of circle */ public void circle(float x, float y, float r) { float b = 0.5523f; moveTo(x + r, y); curveTo(x + r, y + r * b, x + r * b, y + r, x, y + r); curveTo(x - r * b, y + r, x - r, y + r * b, x - r, y); curveTo(x - r, y - r * b, x - r * b, y - r, x, y - r); curveTo(x + r * b, y - r, x + r, y - r * b, x + r, y); }
/** Draws a circle. The endpoint will (x+r, y). * * @param x x center of circle * @param y y center of circle * @param r radius of circle */ public void circle(float x, float y, float r) { float b = 0.5523f; moveTo(x + r, y); curveTo(x + r, y + r * b, x + r * b, y + r, x, y + r); curveTo(x - r * b, y + r, x - r, y + r * b, x - r, y); curveTo(x - r, y - r * b, x - r * b, y - r, x, y - r); curveTo(x + r * b, y - r, x + r, y - r * b, x + r, y); }
/** Draws a circle. The endpoint will (x+r, y). * * @param x x center of circle * @param y y center of circle * @param r radius of circle */ public void circle(float x, float y, float r) { float b = 0.5523f; moveTo(x + r, y); curveTo(x + r, y + r * b, x + r * b, y + r, x, y + r); curveTo(x - r * b, y + r, x - r, y + r * b, x - r, y); curveTo(x - r, y - r * b, x - r * b, y - r, x, y - r); curveTo(x + r * b, y - r, x + r, y - r * b, x + r, y); }
PdfContentByte cb = writer.getDirectContentUnder(); cb.saveState(); cb.setLineWidth(1.2); cb.setColor(BaseColor.DARK_GRAY); cb.moveTo(x, y); cb.lineTo(x + width, y); cb.stroke(); cb.restoreState();
class DottedCell implements PdfPCellEvent { public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { PdfContentByte canvas = canvases[PdfPTable.LINECANVAS]; canvas.setLineDash(3f, 3f); canvas.moveTo(position.getLeft(), position.getTop()); canvas.lineTo(position.getRight(), position.getTop()); canvas.moveTo(position.getLeft(), position.getBottom()); canvas.lineTo(position.getRight(), position.getBottom()); canvas.stroke(); } }
Type3Font t3 = new Type3Font(writer, true); // Delta PdfContentByte d = t3.defineGlyph('D', 600, 0, 0, 600, 700); d.setColorStroke(new BaseColor(0xFF, 0x00, 0x00)); d.setColorFill(new GrayColor(0.7f)); d.setLineWidth(100); d.moveTo(5, 5); d.lineTo(300, 695); d.lineTo(595, 5); d.closePathFillStroke(); //Sigma PdfContentByte s = t3.defineGlyph('S', 600, 0, 0, 600, 700); s.setColorStroke(new BaseColor(0x00, 0x80, 0x80)); s.setLineWidth(100); s.moveTo(595,5); s.lineTo(5, 5); s.lineTo(300, 350); s.lineTo(5, 695); s.lineTo(595, 695); s.stroke(); // Use as regular font at size 12 Font f = new Font(t3, 12);
public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfContentByte canvas = writer.getDirectContent(); CMYKColor magentaColor = new CMYKColor(0.f, 1.f, 0.f, 0.f); canvas.setColorStroke(magentaColor); canvas.moveTo(36, 36); canvas.lineTo(36, 806); canvas.lineTo(559, 36); canvas.lineTo(559, 806); canvas.closePathStroke(); document.close(); }
public class UnderlinedCell implements PdfPCellEvent { public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { PdfContentByte canvas = canvases[PdfPTable.LINECANVAS]; canvas.setLineWidth(0.5f); canvas.setLineDash(3f, 3f); canvas.moveTo(position.getLeft(), position.getBottom()); canvas.lineTo(position.getRight(), position.getBottom()); canvas.stroke(); } }
@Override public void drawLine(int x1, int y1, int x2, int y2, int width) { //cb.closePath(); cb.setColorStroke(color); cb.setLineWidth(width/10.0f); cb.moveTo( (xofs+x1)/10f, document.getPageSize().getHeight()-(y1+yofs)/10.0f); cb.lineTo( (xofs+x2)/10f, document.getPageSize().getHeight()-(y2+yofs)/10.0f); cb.stroke(); }
PdfContentByte canvas = writer.getDirectContent(); canvas.setColorStroke(BaseColor.RED); canvas.setLineWidth(3); canvas.moveTo(220, 330); canvas.lineTo(240, 370); canvas.arc(200, 350, 240, 390, 0, (float) 180); canvas.lineTo(220, 330); canvas.closePathStroke(); canvas.setColorFill(BaseColor.RED); canvas.circle(220, 370, 10); canvas.fill();
private void renderHeader(PdfWriter writer,Document document){ // header lines cb_.moveTo(document.left(),document.top()+10); cb_.lineTo(document.right(),document.top()+10); cb_.stroke(); // header contents cb_.beginText(); cb_.setFontAndSize(PdfUtils.BASEFONT_GOTHIC,size_); cb_.setTextMatrix(headerX_, headerY_); cb_.showText(headerTitle_); cb_.endText(); }
public class MyPdfPageEvent extends PdfPageEventHelper { public void onGenericTag(PdfWriter writer, Document doc, Rectangle rect, String tag) { PdfContentByte canvas = writer.getDirectContent(); canvas.saveState(); canvas.setColorStroke(Color.BLACK); // or whatever // You can also mess with the line's thickness, endcaps, dash style, etc. // Lots of options to play with. canvas.moveTo(rect.getLeft(), rect.getBottom()); canvas.lineTo(rect.getRight(), rect.getTop()); canvas.stroke(); canvas.restoreState(); } }
private void renderTitleFooter(PdfWriter writer, Document document){ String text = RomanList.toRomanLowerCase(writer.getPageNumber()); cb_.moveTo(document.left(),PAGEMARGIN_BOTTOM - 5); cb_.lineTo(document.right(),PAGEMARGIN_BOTTOM - 5); cb_.stroke(); // don't forget !! // footer contents cb_.beginText(); cb_.setFontAndSize(PdfUtils.BASEFONT_MINCHO,size_); cb_.setTextMatrix(footerX_, PAGEMARGIN_BOTTOM/2); cb_.showText(text); cb_.endText(); }
private void renderFooter(PdfWriter writer, Document document){ // footer lines // int num = writer.getPageNumber(); int num = document.getPageNumber(); String text = FOOTERPREFIX + num + FOOTERMIDDLE; float len = PdfUtils.BASEFONT_MINCHO.getWidthPoint(text, size_); cb_.moveTo(document.left(),PAGEMARGIN_BOTTOM - 5); cb_.lineTo(document.right(),PAGEMARGIN_BOTTOM - 5); cb_.stroke(); // don't forget !! // footer contents cb_.beginText(); cb_.setFontAndSize(PdfUtils.BASEFONT_MINCHO,size_); cb_.setTextMatrix(footerX_, PAGEMARGIN_BOTTOM/2); cb_.showText(text); cb_.endText(); // adding template point into direct content. cb_.addTemplate(template_,footerX_+len, PAGEMARGIN_BOTTOM/2); }