List of usage examples for com.itextpdf.text.pdf PdfContentByte setColorFill
public void setColorFill(final BaseColor color)
From source file:adams.flow.transformer.pdfproclet.Circle.java
License:Open Source License
/** * The actual processing of the document. * * @param generator the context// w w w. j av a2s . co m * @param file the file to add * @return true if successfully added * @throws Exception if something goes wrong */ protected boolean doProcess(PDFGenerator generator, File file) throws Exception { PdfContentByte cb; cb = generator.getWriter().getDirectContent(); cb.saveState(); cb.setColorStroke(new BaseColor(m_Color.getRGB())); cb.setColorFill(new BaseColor(m_Color.getRGB())); cb.setLineWidth(m_LineWidth); cb.circle(m_X, m_Y, m_Radius); if (m_Fill) cb.fillStroke(); else cb.stroke(); cb.restoreState(); return true; }
From source file:adams.flow.transformer.pdfproclet.Rectangle.java
License:Open Source License
/** * The actual processing of the document. * * @param generator the context/*from w w w . j av a 2 s . co m*/ * @param file the file to add * @return true if successfully added * @throws Exception if something goes wrong */ protected boolean doProcess(PDFGenerator generator, File file) throws Exception { PdfContentByte cb; cb = generator.getWriter().getDirectContent(); cb.saveState(); cb.setColorStroke(new BaseColor(m_Color.getRGB())); cb.setColorFill(new BaseColor(m_Color.getRGB())); cb.setLineWidth(m_LineWidth); cb.rectangle(m_X, m_Y, m_X + m_Width, m_Y + m_Height); if (m_Fill) cb.fillStroke(); else cb.stroke(); cb.restoreState(); return true; }
From source file:be.roots.taconic.pricingguide.service.PDFServiceImpl.java
License:Open Source License
private byte[] personalize(byte[] pdf, Contact contact, Toc tableOfContents) throws IOException, DocumentException { try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) { final PdfReader reader = new PdfReader(pdf); final PdfStamper stamper = new PdfStamper(reader, bos); // stamp some text on first page PdfContentByte text = stamper.getOverContent(1); text.beginText();/*from w w w. ja va 2s .c om*/ text.setColorFill(iTextUtil.getFontCoverText().getColor()); text.setFontAndSize(iTextUtil.getFontCoverText().getBaseFont(), iTextUtil.getFontCoverText().getSize()); text.showTextAligned(Element.ALIGN_RIGHT, coverTitle1, text.getPdfDocument().getPageSize().getWidth() - 15, 195, 0); text.showTextAligned(Element.ALIGN_RIGHT, coverTitle2, text.getPdfDocument().getPageSize().getWidth() - 15, 175, 0); text.showTextAligned(Element.ALIGN_RIGHT, contact.getCurrency().getTitlePageDescription(), text.getPdfDocument().getPageSize().getWidth() - 15, 80, 0); text.setColorFill(iTextUtil.getFontCoverPricingguide().getColor()); text.setFontAndSize(iTextUtil.getFontCoverPricingguide().getBaseFont(), iTextUtil.getFontCoverPricingguide().getSize()); text.showTextAligned(Element.ALIGN_RIGHT, coverTitle3, text.getPdfDocument().getPageSize().getWidth() - 15, 145, 0); text.setColorFill(iTextUtil.getFontCoverYear().getColor()); text.setFontAndSize(iTextUtil.getFontCoverYear().getBaseFont(), iTextUtil.getFontCoverYear().getSize()); text.showTextAligned(Element.ALIGN_RIGHT, coverTitle4, text.getPdfDocument().getPageSize().getWidth() - 15, 105, 0); text.endText(); // stamp some text on first page of the table of contents page final Image logoImage = iTextUtil.getImageFromByteArray(HttpUtil.readByteArray( pdfTemplate.getLogo().getUrl(), defaultService.getUserName(), defaultService.getPassword())); final PdfContentByte tocContent = stamper.getOverContent(tableOfContents.getFirstPageOfToc()); final float resizeRatio = logoImage.getHeight() / 85; // define the desired height of the log tocContent.addImage(logoImage, logoImage.getWidth() / resizeRatio, 0, 0, logoImage.getHeight() / resizeRatio, 59, 615); text = stamper.getOverContent(tableOfContents.getFirstPageOfToc()); text.beginText(); text.setColorFill(iTextUtil.getFontPersonalization().getColor()); text.setFontAndSize(iTextUtil.getFontPersonalization().getBaseFont(), iTextUtil.getFontPersonalization().getSize()); text.showTextAligned(Element.ALIGN_LEFT, "Prepared for:", 355, 681, 0); text.showTextAligned(Element.ALIGN_LEFT, contact.getFullName(), 355, 662, 0); // set company name if (!StringUtils.isEmpty(contact.getCompany())) { text.showTextAligned(Element.ALIGN_LEFT, contact.getCompany(), 355, 643, 0); text.showTextAligned(Element.ALIGN_LEFT, new SimpleDateFormat("MM-dd-yyyy").format(new Date()), 355, 624, 0); } else { text.showTextAligned(Element.ALIGN_LEFT, new SimpleDateFormat("MM-dd-yyyy").format(new Date()), 355, 643, 0); } text.endText(); final ColumnText ct = new ColumnText(tocContent); ct.setSimpleColumn(new Rectangle(55, 517, iTextUtil.PAGE_SIZE.getWidth() - 45, 575)); final List<Element> elements = HTMLWorker.parseToList(new StringReader(disclaimer), null); final Paragraph p = new Paragraph(); p.setAlignment(Element.ALIGN_JUSTIFIED); for (Element element : elements) { for (Chunk chunk : element.getChunks()) { chunk.setFont(iTextUtil.getFontDisclaimer()); } p.add(element); } ct.addElement(p); ct.go(); stamper.close(); reader.close(); return bos.toByteArray(); } }
From source file:bouttime.report.boutsheet.BoutSheetReport.java
License:Open Source License
/** * Generate a bout sheet report for the given list of bouts. * It is assumed that the list is in the desired order (no sorting is done here). * * @param list//from ww w . j ava 2 s .co m * @return True if the report was generated. */ public boolean generateReport(Dao dao, List<Bout> list) { // step 1: creation of a document-object // rotate to make page landscape Document document = new Document(PageSize.A4.rotate()); try { // step 2: creation of the writer FileOutputStream fos = createOutputFile(); if (fos == null) { return false; } PdfWriter writer = PdfWriter.getInstance(document, fos); writer.setPageEvent(this); // step 3: we open the document document.open(); // step 4: we grab the ContentByte and do some stuff with it PdfContentByte cb = writer.getDirectContent(); BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.EMBEDDED); float pageWidth = cb.getPdfDocument().getPageSize().getWidth(); float midPage = pageWidth / 2; setHeaderString(dao); int count = 0; for (Bout b : list) { boolean rightSide = false; if ((count++ % 2) == 0) { if (count > 2) { // We could put this after Bout 2 is added, but // that could leave a blank last page. document.newPage(); } // Bout 1 (Left side) drawBout(cb, bf, 35, midPage - 35, b); } else { // Bout 2 (Right side) drawBout(cb, bf, midPage + 35, pageWidth - 35, b); rightSide = true; } // Print the watermark, if necessary boolean doWatermark = false; String gClass = b.getGroup().getClassification(); String wmValues = dao.getBoutsheetWatermarkValues(); if ((wmValues != null) && !wmValues.isEmpty()) { String[] tokens = wmValues.split(","); for (String s : tokens) { if (s.trim().equalsIgnoreCase(gClass)) { doWatermark = true; break; } } } if (doWatermark) { int rotation = 45; PdfContentByte ucb = writer.getDirectContentUnder(); BaseFont helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false); ucb.saveState(); ucb.setColorFill(BaseColor.LIGHT_GRAY); ucb.beginText(); ucb.setFontAndSize(helv, 86); float centerWidth = document.getPageSize().getWidth() / 4; if (rightSide) { centerWidth = centerWidth * 3; } ucb.showTextAligned(Element.ALIGN_CENTER, gClass, centerWidth, document.getPageSize().getHeight() / 2, rotation); ucb.endText(); ucb.restoreState(); } } } catch (DocumentException de) { logger.error("Document Exception", de); return false; } catch (IOException ioe) { logger.error("IO Exception", ioe); return false; } // step 5: we close the document document.close(); return true; }
From source file:bouttime.report.bracketsheet.BracketSheetReport.java
License:Open Source License
public static boolean generateReport(Dao dao, List<Group> list, String outputFile, boolean doBoutNumbers, boolean doTimestamp) { if (list.isEmpty()) { return false; }//from www. j a v a 2 s . com // step 1: creation of a document-object Document document = new Document(); try { // step 2: creation of the writer FileOutputStream fos = createOutputFile(outputFile); if (fos == null) { return false; } PdfWriter writer = PdfWriter.getInstance(document, fos); // step 3: we open the document document.open(); // step 4: we grab the ContentByte and do some stuff with it PdfContentByte cb = writer.getDirectContent(); String timestamp = ""; if (doTimestamp) { timestamp = DateFormat.getInstance().format(new Date()); } int rv; int i = 0; int size = list.size(); for (Group g : list) { rv = addBracket(cb, dao, g, doBoutNumbers); if (rv != PAGE_ERROR) { // Print the watermark, if necessary boolean doWatermark = false; String gClass = g.getClassification(); String wmValues = dao.getBracketsheetWatermarkValues(); if ((wmValues != null) && !wmValues.isEmpty()) { String[] tokens = wmValues.split(","); for (String s : tokens) { if (s.trim().equalsIgnoreCase(gClass)) { doWatermark = true; break; } } } int rotation = (rv == PAGE_ROUNDROBIN) ? 45 : 135; if (doWatermark) { PdfContentByte ucb = writer.getDirectContentUnder(); BaseFont helv = BaseFont.createFont("Helvetica", BaseFont.WINANSI, false); ucb.saveState(); ucb.setColorFill(BaseColor.LIGHT_GRAY); ucb.beginText(); ucb.setFontAndSize(helv, 86); ucb.showTextAligned(Element.ALIGN_CENTER, gClass, document.getPageSize().getWidth() / 2, document.getPageSize().getHeight() / 2, rotation); ucb.endText(); ucb.restoreState(); } if (doTimestamp) { rotation -= 45; float width = cb.getPdfWriter().getPageSize().getWidth(); int x = (rv == PAGE_ROUNDROBIN) ? 15 : (int) (width - 15); int y = 15; BracketSheetUtil.drawTimestamp(cb, null, x, y, 10, timestamp, rotation); } // If not doing bout numbers, this is an 'award' type of // bracket. So print an image/logo, if configured. if (!doBoutNumbers && (dao.getBracketsheetAwardImage() != null) && !dao.getBracketsheetAwardImage().isEmpty()) { Image image = Image.getInstance(Image.getInstance(dao.getBracketsheetAwardImage())); image.setRotationDegrees((rv == PAGE_ROUNDROBIN) ? 0 : 90); PositionOnPage positionOnPage = dao.getBracketsheetAwardImagePosition(); if (PositionOnPage.UPPER_RIGHT == positionOnPage) { float x = (rv == PAGE_ROUNDROBIN) ? document.getPageSize().getWidth() - 10 - image.getWidth() : 10; float y = document.getPageSize().getHeight() - 10 - image.getHeight(); image.setAbsolutePosition(x, y); cb.addImage(image); } else if (PositionOnPage.CENTER == positionOnPage) { // put the image in the background, in the middle of the page PdfContentByte ucb = writer.getDirectContentUnder(); float pageX = document.getPageSize().getWidth() / 2; float pageY = document.getPageSize().getHeight() / 2; float imageX = image.getWidth() / 2; float imageY = image.getHeight() / 2; image.setAbsolutePosition(pageX - imageX, pageY - imageY); ucb.addImage(image); } } if (++i < size) { document.newPage(); } } } } catch (DocumentException de) { logger.error("Document Exception", de); return false; } catch (IOException ioe) { logger.error("IO Exception", ioe); return false; } // step 5: we close the document document.close(); return true; }
From source file:com.automaster.autoview.server.servlet.TextStateOperators.java
/** * Creates a PDF document./*from w w w .j a v a2s. c o m*/ * @param filename the path to the new PDF document * @throws DocumentException * @throws IOException */ public void createPdf(String filename) throws IOException, DocumentException { // step 1 Rectangle rect = new Rectangle(595, 842); Document document = new Document(rect, 30, 30, 30, 30); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); // step 3 document.open(); // step 4 //PdfAcroForm canva = writer.getAcroForm(); BaseFont bf = BaseFont.createFont(); //canva.addMultiLineTextField("txtTeste", "texto de teste vamos ver se le quebra a linha", bf, 15, 300, 800, 100, 100); PdfContentByte canvas = writer.getDirectContent(); String text = "AWAY again"; String text1 = "TESTE Rua alameda luis de sousa santos, N 52, castanhal, CEP: 68742-783, ao lado do posto adriano"; canvas.beginText(); // line 1 canvas.setFontAndSize(bf, 16); canvas.moveText(36, 800); canvas.moveTextWithLeading(0, -24); canvas.setTextRise(10); canvas.showText(text); // line 2 canvas.setWordSpacing(20); canvas.newlineShowText(text); // line 3 canvas.setCharacterSpacing(10); canvas.newlineShowText(text); canvas.setWordSpacing(0); canvas.setCharacterSpacing(0); // line 4 canvas.setHorizontalScaling(50); canvas.newlineShowText(text); canvas.setHorizontalScaling(100); // line 5 canvas.newlineShowText(text); canvas.setTextRise(15); canvas.setFontAndSize(bf, 12); canvas.setColorFill(BaseColor.RED); canvas.showText("2"); canvas.setColorFill(GrayColor.GRAYBLACK); // line 6 canvas.setLeading(56); canvas.setMiterLimit(5); canvas.newlineShowText( "Rua alameda luis de sousa santos, N 52, castanhal, CEP: 68742-783, ao lado do posto" + text); canvas.setLeading(24); canvas.newlineText(); Paragraph paragraph = new Paragraph("teste"); PdfWriter pdfWriter = canvas.getPdfWriter(); PdfAcroForm pdfAcroForm = pdfWriter.getAcroForm(); //pdfAcroForm.addMultiLineTextField("txtTeste", "Changing the adriano leading: Rua alameda luis de sousa santos, N 52, castanhal, CEP: 68742-783, ao lado do posto", bf, 12, 30, 650, 550, 750); //pdfAcroForm.addHiddenField("txtTeste", text); PdfFormField pdfFormField = new PdfFormField(pdfWriter, 30, 650, 550, 750, null); pdfFormField.setFieldName("txtTeste"); pdfFormField.setValueAsString(text1); //TextField textField = new TextField(canvas.getPdfWriter(), new Rectangle(30, 650, 550, 750), "txtTest"); //textField.setText(text); pdfAcroForm.addFormField(pdfFormField); // line 7 PdfTextArray array = new PdfTextArray("A"); array.add(120); array.add("W"); array.add(120); array.add("A"); array.add(95); array.add("Y again"); canvas.showText(array); canvas.endText(); canvas.setColorFill(BaseColor.BLUE); canvas.beginText(); canvas.setTextMatrix(360, 770); canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL); canvas.setFontAndSize(bf, 24); canvas.showText(text); canvas.endText(); canvas.beginText(); canvas.setTextMatrix(360, 730); canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE); canvas.setFontAndSize(bf, 24); canvas.showText(text); canvas.endText(); canvas.beginText(); canvas.setTextMatrix(360, 690); canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE); canvas.setFontAndSize(bf, 24); canvas.showText(text); canvas.endText(); canvas.beginText(); canvas.setTextMatrix(360, 650); canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE); canvas.setFontAndSize(bf, 24); canvas.showText(text); canvas.endText(); PdfTemplate template = canvas.createTemplate(200, 36); template.setLineWidth(2); for (int i = 0; i < 6; i++) { template.moveTo(0, i * 6); template.lineTo(200, i * 6); } template.stroke(); canvas.saveState(); canvas.beginText(); canvas.setTextMatrix(360, 610); canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_CLIP); canvas.setFontAndSize(bf, 24); canvas.showText(text); canvas.endText(); canvas.addTemplate(template, 360, 610); canvas.restoreState(); canvas.saveState(); canvas.beginText(); canvas.setTextMatrix(360, 570); canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE_CLIP); canvas.setFontAndSize(bf, 24); canvas.showText(text); canvas.endText(); canvas.addTemplate(template, 360, 570); canvas.restoreState(); canvas.saveState(); canvas.beginText(); canvas.setTextMatrix(360, 530); canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE_CLIP); canvas.setFontAndSize(bf, 24); canvas.showText(text); canvas.endText(); canvas.addTemplate(template, 360, 530); canvas.restoreState(); canvas.saveState(); canvas.beginText(); canvas.setTextMatrix(360, 490); canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_CLIP); canvas.setFontAndSize(bf, 24); canvas.showText(text); canvas.endText(); canvas.addTemplate(template, 360, 490); canvas.restoreState(); // step 5 //document.add((Element) pdfAcroForm); document.close(); }
From source file:com.innoviu.signature.Signature.java
public static void main(String[] args) { boolean isEncrypted = false; boolean isFailed = false; try {// w ww . ja v a 2 s . co m if (args.length < 2) { throw new FileNotFoundException(); } PdfReader reader = new PdfReader(args[0]); isEncrypted = reader.isEncrypted(); String suffix = ".pdf"; if (isEncrypted) { System.out.println("Encrypted"); String[] cmd = { "pdftk", args[0], "output", args[0] + ".pdftk.pdf" }; try { Process proc = Runtime.getRuntime().exec(cmd); proc.waitFor(); } catch (Exception e) { System.out.println("Exception is:" + e); } reader = new PdfReader(args[0] + ".pdftk.pdf"); suffix = ".dec.pdf"; } PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(args[0] + suffix)); PdfContentByte over = stamper.getOverContent(1); String type = args[2]; int xpos = 0; //int xpos = (type == "in") ? 120 : 10; if ("in".equals(type)) { xpos = 0; } else { xpos = 120; } over.setColorFill(BaseColor.WHITE); over.rectangle(xpos + 10, 8, 120, 8); over.fill(); over.beginText(); BaseFont bf_times = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", false); over.setFontAndSize(bf_times, 6); over.setColorFill(BaseColor.BLACK); over.showTextAligned(PdfContentByte.ALIGN_RIGHT, args[1], 120 + xpos, 10, 0); over.endText(); stamper.close(); if (isEncrypted) { File file = new File(args[0] + ".pdftk.pdf"); file.delete(); } } catch (FileNotFoundException e) { isFailed = true; e.printStackTrace(); } catch (DocumentException e) { isFailed = true; e.printStackTrace(); } catch (IOException e) { isFailed = true; e.printStackTrace(); } finally { if (isEncrypted) { maintain(args[0]); } else if (isFailed) { fail(args[0]); } else { } } }
From source file:com.masscustsoft.service.ToPdf.java
License:Open Source License
private void getDirectContent(PdfContentByte cb, Rectangle ps, Map it) throws Exception { BaseColor color = getColor(it, "fillColor"); if (color != null) cb.setColorFill(color); float x = MapUtil.getFloat(it, "x", 0f); float y = MapUtil.getFloat(it, "y", 0f); float w = MapUtil.getFloat(it, "w", 0f); float h = MapUtil.getFloat(it, "h", 0f); float xPer = MapUtil.getFloat(it, "xPer", 0f); float yPer = MapUtil.getFloat(it, "yPer", 0f); float wPer = MapUtil.getFloat(it, "wPer", 0f); float hPer = MapUtil.getFloat(it, "hPer", 0f); String pos = MapUtil.getStr(it, "position", "bottom"); switch (pos) { case "top": y += ps.getHeight();//from w w w.j av a 2s . co m break; case "right": x += ps.getWidth(); break; } float xx = x + ps.getWidth() * xPer / 100f; float yy = y + ps.getWidth() * yPer / 100f; float ww = ps.getWidth() * wPer / 100f + w; float hh = ps.getHeight() * hPer / 100f + h; int font = MapUtil.getInt(it, "fontSize", 8); cb.setFontAndSize(getDefaultFont(), font); cb.beginText(); String cls = MapUtil.getStr(it, "cls", ""); if (cls.equals("image")) { Image img = getImage(it); cb.addImage(img, img.getWidth(), 0, 0, img.getHeight(), xx, yy); } else { String text = LightUtil.macro(MapUtil.getStr(it, "text", ""), '$').toString(); float degree = MapUtil.getFloat(it, "rotateDegree", 0f); boolean kerned = MapUtil.getBool(it, "kerned", false); int align = getAlignment(it, "alignment"); x = xx; y = yy; switch (align) { case Element.ALIGN_CENTER: x = xx + ww / 2; break; case Element.ALIGN_RIGHT: x = xx + ww; break; default: align = Element.ALIGN_LEFT; break; } if (kerned) cb.showTextAlignedKerned(align, text, x, y, degree); else cb.showTextAligned(align, text, x, y, degree); } cb.endText(); }
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);//from w ww . j av a 2 s .c o m 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 BaseFont debugFont(PdfContentByte canvas, EnhancedMap settings) { BaseFont bf = FontFactory.getFont(FontFactory.HELVETICA).getBaseFont(); canvas.setFontAndSize(bf, 8);//ww w .j a v a 2 s .c o m canvas.setColorFill( itextHelper.fromColor(settings.getColorProperty(Color.MAGENTA, ReportConstants.DEBUGCOLOR))); canvas.setColorStroke( itextHelper.fromColor(settings.getColorProperty(Color.MAGENTA, ReportConstants.DEBUGCOLOR))); return bf; }