/** * Main method. * * @param args no arguments needed * @throws DocumentException * @throws IOException */ public static void main(String[] args) throws IOException, DocumentException { // step 1 Document document = new Document(PageSize.POSTCARD, 30, 30, 30, 30); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); // step 3 document.open(); // step 4 // Create and add a Paragraph Paragraph p = new Paragraph("Foobar Film Festival", new Font(FontFamily.HELVETICA, 22)); p.setAlignment(Element.ALIGN_CENTER); document.add(p); // Create and add an Image Image img = Image.getInstance(RESOURCE); img.setAbsolutePosition( (PageSize.POSTCARD.getWidth() - img.getScaledWidth()) / 2, (PageSize.POSTCARD.getHeight() - img.getScaledHeight()) / 2); document.add(img); // Now we go to the next page document.newPage(); document.add(p); document.add(img); // Add text on top of the image PdfContentByte over = writer.getDirectContent(); over.saveState(); float sinus = (float) Math.sin(Math.PI / 60); float cosinus = (float) Math.cos(Math.PI / 60); BaseFont bf = BaseFont.createFont(); over.beginText(); over.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE); over.setLineWidth(1.5f); over.setRGBColorStroke(0xFF, 0x00, 0x00); over.setRGBColorFill(0xFF, 0xFF, 0xFF); over.setFontAndSize(bf, 36); over.setTextMatrix(cosinus, sinus, -sinus, cosinus, 50, 324); over.showText("SOLD OUT"); over.endText(); over.restoreState(); // Add a rectangle under the image PdfContentByte under = writer.getDirectContentUnder(); under.saveState(); under.setRGBColorFill(0xFF, 0xD7, 0x00); under.rectangle(5, 5, PageSize.POSTCARD.getWidth() - 10, PageSize.POSTCARD.getHeight() - 10); under.fill(); under.restoreState(); // step 4 document.close(); }
// FIXME: Manage Exceptions private OutputStream marcarReporteProgramaRechazado(final Programa programa, InputStream src) throws IOException, DocumentException { PdfReader reader = null; reader = new PdfReader(src); int n = reader.getNumberOfPages(); ByteArrayOutputStream os = new ByteArrayOutputStream(); PdfStamper stamper = null; stamper = new PdfStamper(reader, os); // text watermark Font f = new Font(Font.FontFamily.HELVETICA, 65); Phrase p = new Phrase("RECHAZADO", f); // transparency PdfGState gs1 = new PdfGState(); gs1.setFillOpacity(0.3f); // properties PdfContentByte over; Rectangle pagesize; float x, y; // loop over every page for (int i = 1; i <= n; i++) { pagesize = reader.getPageSizeWithRotation(i); x = (pagesize.getLeft() + pagesize.getRight()) / 2; y = (pagesize.getTop() + pagesize.getBottom()) / 2; over = stamper.getOverContent(i); over.saveState(); over.setGState(gs1); ColumnText.showTextAligned(over, Element.ALIGN_CENTER, p, x, y, 45); } stamper.close(); reader.close(); return os; }
public void colorRectangle( PdfContentByte canvas, BaseColor color, float x, float y, float width, float height) { canvas.saveState(); canvas.setColorFill(color); canvas.rectangle(x, y, width, height); canvas.fillStroke(); canvas.restoreState(); }
public static void prost(PdfContentByte cb, int x, int y, int x1, int y1) { cb.saveState(); PdfSpotColor color = new PdfSpotColor(RESULT, BaseColor.BLACK); cb.setLineWidth((float) 0.5); cb.setColorStroke(color, (float) 0.5); cb.setFlatness(y1); cb.rectangle(x, y, x1, y1); cb.stroke(); cb.restoreState(); }
public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) { try { PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS]; PdfPatternPainter patternPainter = cb.createPattern(image.getScaledWidth(), image.getScaledHeight()); image.setAbsolutePosition(0, 0); patternPainter.addImage(image); cb.saveState(); cb.setPatternFill(patternPainter); cb.rectangle( position.getLeft(), position.getBottom(), position.getWidth(), position.getHeight()); cb.fill(); cb.restoreState(); } catch (DocumentException e) { throw new ExceptionConverter(e); } }
public void absText( PdfWriter twriter, String text, float ypostex12, float ypostex22, int fontsize) { try { PdfContentByte cb = twriter.getDirectContent(); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.saveState(); cb.beginText(); cb.moveText(ypostex12, ypostex22); cb.setFontAndSize(bf, fontsize); cb.setColorFill(new BaseColor(0, 0, 0)); cb.showText(text); cb.endText(); cb.restoreState(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
public void tableLayout( PdfPTable table, float[][] width, float[] height, int headerRows, int rowStart, PdfContentByte[] canvas) { PdfContentByte background = canvas[PdfPTable.BASECANVAS]; background.saveState(); background.setCMYKColorFill(0x00, 0x00, 0xFF, 0x0F); background.roundRectangle( width[0][0], height[height.length - 1] - 2, width[0][1] - width[0][0] + 6, height[0] - height[height.length - 1] - 4, 4); background.fill(); background.restoreState(); }
@Override // initialization of the header table public void onEndPage(final PdfWriter writer, final Document document) { final PdfPTable header = new PdfPTable(2); final Phrase p = new Phrase(); final Chunk ck = new Chunk(_challengeTitle + "\n" + _reportTitle, _font); p.add(ck); header.getDefaultCell().setBorderWidth(0); header.addCell(p); header.getDefaultCell().setHorizontalAlignment(com.itextpdf.text.Element.ALIGN_RIGHT); header.addCell( new Phrase(new Chunk("Tournament: " + _tournament + "\nDate: " + _formattedDate, _font))); final PdfPCell blankCell = new PdfPCell(); blankCell.setBorder(0); blankCell.setBorderWidthTop(1.0f); blankCell.setColspan(2); header.addCell(blankCell); final PdfContentByte cb = writer.getDirectContent(); cb.saveState(); header.setTotalWidth(document.right() - document.left()); header.writeSelectedRows(0, -1, document.left(), document.getPageSize().getHeight() - 10, cb); cb.restoreState(); }
/** * Creates a PDF document. * * @param filename the path to the new PDF document * @throws DocumentException * @throws IOException * @throws DocumentException * @throws IOException */ public void createPdf(String filename) throws IOException, DocumentException { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); document.open(); PdfContentByte canvas = writer.getDirectContent(); canvas.saveState(); for (int i = 25; i > 0; i--) { canvas.setLineWidth((float) i / 10); canvas.moveTo(50, 806 - (5 * i)); canvas.lineTo(320, 806 - (5 * i)); canvas.stroke(); } canvas.restoreState(); canvas.moveTo(350, 800); canvas.lineTo(350, 750); canvas.moveTo(540, 800); canvas.lineTo(540, 750); canvas.stroke(); canvas.saveState(); canvas.setLineWidth(8); canvas.setLineCap(PdfContentByte.LINE_CAP_BUTT); canvas.moveTo(350, 790); canvas.lineTo(540, 790); canvas.stroke(); canvas.setLineCap(PdfContentByte.LINE_CAP_ROUND); canvas.moveTo(350, 775); canvas.lineTo(540, 775); canvas.stroke(); canvas.setLineCap(PdfContentByte.LINE_CAP_PROJECTING_SQUARE); canvas.moveTo(350, 760); canvas.lineTo(540, 760); canvas.stroke(); canvas.restoreState(); canvas.saveState(); canvas.setLineWidth(8); canvas.setLineJoin(PdfContentByte.LINE_JOIN_MITER); canvas.moveTo(387, 700); canvas.lineTo(402, 730); canvas.lineTo(417, 700); canvas.stroke(); canvas.setLineJoin(PdfContentByte.LINE_JOIN_ROUND); canvas.moveTo(427, 700); canvas.lineTo(442, 730); canvas.lineTo(457, 700); canvas.stroke(); canvas.setLineJoin(PdfContentByte.LINE_JOIN_BEVEL); canvas.moveTo(467, 700); canvas.lineTo(482, 730); canvas.lineTo(497, 700); canvas.stroke(); canvas.restoreState(); canvas.saveState(); canvas.setLineWidth(3); canvas.moveTo(50, 660); canvas.lineTo(320, 660); canvas.stroke(); canvas.setLineDash(6, 0); canvas.moveTo(50, 650); canvas.lineTo(320, 650); canvas.stroke(); canvas.setLineDash(6, 3); canvas.moveTo(50, 640); canvas.lineTo(320, 640); canvas.stroke(); canvas.setLineDash(15, 10, 5); canvas.moveTo(50, 630); canvas.lineTo(320, 630); canvas.stroke(); float[] dash1 = {10, 5, 5, 5, 20}; canvas.setLineDash(dash1, 5); canvas.moveTo(50, 620); canvas.lineTo(320, 620); canvas.stroke(); float[] dash2 = {9, 6, 0, 6}; canvas.setLineCap(PdfContentByte.LINE_CAP_ROUND); canvas.setLineDash(dash2, 0); canvas.moveTo(50, 610); canvas.lineTo(320, 610); canvas.stroke(); canvas.restoreState(); PdfTemplate hooks = canvas.createTemplate(300, 120); hooks.setLineWidth(8); hooks.moveTo(46, 50); hooks.lineTo(65, 80); hooks.lineTo(84, 50); hooks.stroke(); hooks.moveTo(87, 50); hooks.lineTo(105, 80); hooks.lineTo(123, 50); hooks.stroke(); hooks.moveTo(128, 50); hooks.lineTo(145, 80); hooks.lineTo(162, 50); hooks.stroke(); hooks.moveTo(169, 50); hooks.lineTo(185, 80); hooks.lineTo(201, 50); hooks.stroke(); hooks.moveTo(210, 50); hooks.lineTo(225, 80); hooks.lineTo(240, 50); hooks.stroke(); canvas.saveState(); canvas.setMiterLimit(2); canvas.addTemplate(hooks, 300, 600); canvas.restoreState(); canvas.saveState(); canvas.setMiterLimit(2.1f); canvas.addTemplate(hooks, 300, 550); canvas.restoreState(); document.close(); }