PdfWriter writer = ...; PdfContentByte cb = writer.getDirectContent(); cb.saveState(); cb.setColorStroke(Color.black); cb.rectangle(x,y,x1,y1); cb.stroke(); cb.restoreState();
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(); }
class RoundRectangle implements PdfPCellEvent { public void cellLayout(PdfPCell cell, Rectangle rect, PdfContentByte[] canvas) { PdfContentByte cb = canvas[PdfPTable.LINECANVAS]; cb.roundRectangle( rect.getLeft() + 1.5f, rect.getBottom() + 1.5f, rect.getWidth() - 3, rect.getHeight() - 3, 4); cb.stroke(); } }
PdfContentByte canvas = writer.getDirectContent(); canvas.moveTo(36, 36); canvas.lineTo(559, 36); canvas.stroke();
public class MyTableEvent implements PdfPTableEvent { 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 y1 = height[0]; float y2 = height[height.length - 1]; PdfContentByte cb = canvas[PdfPTable.LINECANVAS]; cb.rectangle(x1, y1, x2 - x1, y2 - y1); cb.stroke(); cb.resetRGBColorStroke(); } }
public class BorderEvent implements PdfPTableEvent { public void tableLayout(PdfPTable table, float[][] widths, float[] heights, int headerRows, int rowStart, PdfContentByte[] canvases) { float width[] = widths[0]; float x1 = width[0]; float x2 = width[width.length - 1]; float y1 = heights[0]; float y2 = heights[heights.length - 1]; PdfContentByte cb = canvases[PdfPTable.LINECANVAS]; cb.rectangle(x1, y1, x2 - x1, y2 - y1); cb.stroke(); cb.resetRGBColorStroke(); } }
class DottedCell implements PdfPCellEvent { @Override public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { PdfContentByte canvas = canvases[PdfPTable.LINECANVAS]; canvas.setLineDash(3f, 3f); canvas.rectangle(position.getLeft(), position.getBottom(), position.getWidth(), position.getHeight()); canvas.stroke(); } }
private static void rect(PdfWriter writer) { PdfContentByte cb = writer.getDirectContent(); try { cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false), 24); cb.rectangle(140f,140f,280f,420f); cb.stroke(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public class DrawRectangle extends PdfPageEventHelper { @Override public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) { PdfContentByte canvas = writer.getDirectContent(); canvas.rectangle(rect.getLeft(), rect.getBottom(), rect.getWidth(), rect.getHeight()); canvas.stroke(); canvas.restoreState(); } }
public class MyBorder implements PdfPCellEvent { public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { float x1 = position.getLeft() + 2; float x2 = position.getRight() - 2; float y1 = position.getTop() - 2; float y2 = position.getBottom() + 2; PdfContentByte canvas = canvases[PdfPTable.LINECANVAS]; canvas.rectangle(x1, y1, x2 - x1, y2 - y1); canvas.stroke(); } }
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(); } }
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();
@Override public void box(int x1, int y1, int width, int height, Color fg, Color bg, int lineWidth, int curve) { if (bg!=null) { //cb.closePath(); cb.setColorFill(bg); pathBox(x1,y1,width,height,curve); cb.fill(); } if (fg!=null) { //cb.closePath(); cb.setColorStroke(fg); cb.setLineWidth(lineWidth/10.0f); pathBox(x1,y1,width,height,curve); cb.stroke(); } }
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(); }
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 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(); }
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(); } }
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); }