List of usage examples for com.lowagie.text Element ALIGN_LEFT
int ALIGN_LEFT
To view the source code for com.lowagie.text Element ALIGN_LEFT.
Click Source Link
From source file:com.develog.utils.report.engine.export.JRPdfExporter.java
License:Open Source License
/** * *///from w w w . j av a2s . com protected void exportReportToStream(OutputStream os) throws JRException { //ByteArrayOutputStream baos = new ByteArrayOutputStream(); document = new Document(new Rectangle(jasperPrint.getPageWidth(), jasperPrint.getPageHeight())); imageTesterDocument = new Document(new Rectangle(10, //jasperPrint.getPageWidth(), 10 //jasperPrint.getPageHeight() )); try { PdfWriter pdfWriter = PdfWriter.getInstance(document, os); pdfWriter.setCloseStream(false); if (isEncrypted) { pdfWriter.setEncryption(is128BitKey, userPassword, ownerPassword, permissions); } document.open(); PdfWriter imageTesterPdfWriter = PdfWriter.getInstance(imageTesterDocument, new ByteArrayOutputStream()); imageTesterDocument.open(); imageTesterDocument.newPage(); imageTesterPdfContentByte = imageTesterPdfWriter.getDirectContent(); imageTesterPdfContentByte.setLiteral("\n"); List pages = jasperPrint.getPages(); if (pages != null && pages.size() > 0) { Chunk chunk = null; ColumnText colText = null; JRPrintPage page = null; for (int i = startPageIndex; i <= endPageIndex; i++) { if (Thread.currentThread().isInterrupted()) { throw new JRException("Current thread interrupted."); } page = (JRPrintPage) pages.get(i); document.newPage(); pdfContentByte = pdfWriter.getDirectContent(); pdfContentByte.setLineCap(2); chunk = new Chunk(" "); chunk.setLocalDestination("JR_PAGE_ANCHOR_" + (i + 1)); colText = new ColumnText(pdfContentByte); colText.setSimpleColumn(new Phrase(chunk), 0, jasperPrint.getPageHeight(), 1, 1, 0, Element.ALIGN_LEFT); colText.go(); /* */ exportPage(page); } } else { document.newPage(); pdfContentByte = pdfWriter.getDirectContent(); pdfContentByte.setLiteral("\n"); } } catch (DocumentException e) { throw new JRException("PDF Document error : " + jasperPrint.getName(), e); } catch (IOException e) { throw new JRException("Error generating PDF report : " + jasperPrint.getName(), e); } finally { document.close(); imageTesterDocument.close(); } //return os.toByteArray(); }
From source file:com.develog.utils.report.engine.export.JRPdfExporter.java
License:Open Source License
/** * *///from w w w . ja va 2s . c o m protected void exportImage(JRPrintImage printImage) throws DocumentException, IOException { pdfContentByte.setRGBColorFill(printImage.getBackcolor().getRed(), printImage.getBackcolor().getGreen(), printImage.getBackcolor().getBlue()); int borderOffset = 0; float borderCorrection = 0f; float lineWidth = 1f; boolean isLineDotted = false; switch (printImage.getPen()) { case JRGraphicElement.PEN_DOTTED: { borderOffset = 0; borderCorrection = 0f; lineWidth = 1f; isLineDotted = true; break; } case JRGraphicElement.PEN_4_POINT: { borderOffset = 2; borderCorrection = 0f; lineWidth = 4f; isLineDotted = false; break; } case JRGraphicElement.PEN_2_POINT: { borderOffset = 1; borderCorrection = 0f; lineWidth = 2f; isLineDotted = false; break; } case JRGraphicElement.PEN_THIN: { borderOffset = 0; borderCorrection = 0.25f; lineWidth = 0.5f; isLineDotted = false; break; } case JRGraphicElement.PEN_NONE: { borderOffset = 0; borderCorrection = 0.5f; lineWidth = 1f; isLineDotted = false; break; } case JRGraphicElement.PEN_1_POINT: default: { borderOffset = 0; borderCorrection = 0f; lineWidth = 1f; isLineDotted = false; break; } } if (printImage.getMode() == JRElement.MODE_OPAQUE) { pdfContentByte.setRGBColorStroke(printImage.getBackcolor().getRed(), printImage.getBackcolor().getGreen(), printImage.getBackcolor().getBlue()); pdfContentByte.setLineWidth(0.1f); pdfContentByte.setLineDash(0f); pdfContentByte.rectangle(printImage.getX() - borderCorrection, jasperPrint.getPageHeight() - printImage.getY() + borderCorrection, printImage.getWidth() + 2 * borderCorrection - 1, -printImage.getHeight() - 2 * borderCorrection + 1); pdfContentByte.fillStroke(); } int availableImageWidth = printImage.getWidth() - 2 * borderOffset; availableImageWidth = (availableImageWidth < 0) ? 0 : availableImageWidth; int availableImageHeight = printImage.getHeight() - 2 * borderOffset; availableImageHeight = (availableImageHeight < 0) ? 0 : availableImageHeight; int xoffset = 0; int yoffset = 0; if (printImage.getImageData() != null && availableImageWidth > 0 && availableImageHeight > 0) { //java.awt.Image awtImage = JRImageLoader.loadImage(printImage.getImageData()); //com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(awtImage, printImage.getBackcolor()); //com.lowagie.text.Image image = com.lowagie.text.Image.getInstance(awtImage, null); com.lowagie.text.Image image = null; float xalignFactor = 0f; switch (printImage.getHorizontalAlignment()) { case JRAlignment.HORIZONTAL_ALIGN_RIGHT: { xalignFactor = 1f; break; } case JRAlignment.HORIZONTAL_ALIGN_CENTER: { xalignFactor = 0.5f; break; } case JRAlignment.HORIZONTAL_ALIGN_LEFT: default: { xalignFactor = 0f; break; } } float yalignFactor = 0f; switch (printImage.getVerticalAlignment()) { case JRAlignment.VERTICAL_ALIGN_BOTTOM: { yalignFactor = 1f; break; } case JRAlignment.VERTICAL_ALIGN_MIDDLE: { yalignFactor = 0.5f; break; } case JRAlignment.VERTICAL_ALIGN_TOP: default: { yalignFactor = 0f; break; } } switch (printImage.getScaleImage()) { case JRImage.SCALE_IMAGE_CLIP: { java.awt.Image awtImage = JRImageLoader.loadImage(printImage.getImageData()); //image = com.lowagie.text.Image.getInstance(awtImage, null); int awtWidth = awtImage.getWidth(null); int awtHeight = awtImage.getHeight(null); xoffset = (int) (xalignFactor * (availableImageWidth - awtWidth)); yoffset = (int) (yalignFactor * (availableImageHeight - awtHeight)); int minWidth = Math.min(awtWidth, availableImageWidth); int minHeight = Math.min(awtHeight, availableImageHeight); BufferedImage bi = new BufferedImage(minWidth, minHeight, BufferedImage.TYPE_INT_RGB); Graphics g = bi.getGraphics(); g.setColor(printImage.getBackcolor()); g.fillRect(0, 0, minWidth, minHeight); g.drawImage(awtImage, (xoffset > 0 ? 0 : xoffset), (yoffset > 0 ? 0 : yoffset), null); xoffset = (xoffset < 0 ? 0 : xoffset); yoffset = (yoffset < 0 ? 0 : yoffset); //awtImage = bi.getSubimage(0, 0, minWidth, minHeight); awtImage = bi; //image = com.lowagie.text.Image.getInstance(awtImage, printImage.getBackcolor()); image = com.lowagie.text.Image.getInstance(awtImage, null); break; } case JRImage.SCALE_IMAGE_FILL_FRAME: { try { image = com.lowagie.text.Image.getInstance(printImage.getImageData()); imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0); } catch (Exception e) { java.awt.Image awtImage = JRImageLoader.loadImage(printImage.getImageData()); image = com.lowagie.text.Image.getInstance(awtImage, null); } image.scaleAbsolute(availableImageWidth, availableImageHeight); break; } case JRImage.SCALE_IMAGE_RETAIN_SHAPE: default: { try { image = com.lowagie.text.Image.getInstance(printImage.getImageData()); imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0); } catch (Exception e) { java.awt.Image awtImage = JRImageLoader.loadImage(printImage.getImageData()); image = com.lowagie.text.Image.getInstance(awtImage, null); } image.scaleToFit(availableImageWidth, availableImageHeight); xoffset = (int) (xalignFactor * (availableImageWidth - image.plainWidth())); yoffset = (int) (yalignFactor * (availableImageHeight - image.plainHeight())); xoffset = (xoffset < 0 ? 0 : xoffset); yoffset = (yoffset < 0 ? 0 : yoffset); break; } } /* image.setAbsolutePosition( printImage.getX() + borderOffset, jasperPrint.getPageHeight() - printImage.getY() - image.scaledHeight() - borderOffset ); pdfContentByte.addImage(image); */ Chunk chunk = new Chunk(image, -0.5f, 0.5f); if (printImage.getAnchorName() != null) { chunk.setLocalDestination(printImage.getAnchorName()); } switch (printImage.getHyperlinkType()) { case JRHyperlink.HYPERLINK_TYPE_REFERENCE: { if (printImage.getHyperlinkReference() != null) { chunk.setAnchor(printImage.getHyperlinkReference()); } break; } case JRHyperlink.HYPERLINK_TYPE_LOCAL_ANCHOR: { if (printImage.getHyperlinkAnchor() != null) { chunk.setLocalGoto(printImage.getHyperlinkAnchor()); } break; } case JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE: { if (printImage.getHyperlinkPage() != null) { chunk.setLocalGoto("JR_PAGE_ANCHOR_" + printImage.getHyperlinkPage().toString()); } break; } case JRHyperlink.HYPERLINK_TYPE_REMOTE_ANCHOR: { if (printImage.getHyperlinkReference() != null && printImage.getHyperlinkAnchor() != null) { chunk.setRemoteGoto(printImage.getHyperlinkReference(), printImage.getHyperlinkAnchor()); } break; } case JRHyperlink.HYPERLINK_TYPE_REMOTE_PAGE: { if (printImage.getHyperlinkReference() != null && printImage.getHyperlinkPage() != null) { chunk.setRemoteGoto(printImage.getHyperlinkReference(), printImage.getHyperlinkPage().intValue()); } break; } case JRHyperlink.HYPERLINK_TYPE_NONE: default: { break; } } ColumnText colText = new ColumnText(pdfContentByte); colText.setSimpleColumn(new Phrase(chunk), printImage.getX() + xoffset + borderOffset, jasperPrint.getPageHeight() - printImage.getY() - image.scaledHeight() - yoffset - borderOffset, printImage.getX() + xoffset + borderOffset + image.scaledWidth(), jasperPrint.getPageHeight() - printImage.getY() - yoffset - borderOffset, image.scaledHeight(), Element.ALIGN_LEFT); colText.go(); } if (printImage.getPen() != JRGraphicElement.PEN_NONE) { pdfContentByte.setRGBColorStroke(printImage.getForecolor().getRed(), printImage.getForecolor().getGreen(), printImage.getForecolor().getBlue()); pdfContentByte.setLineWidth(lineWidth); if (isLineDotted) { pdfContentByte.setLineDash(5f, 3f, 0f); } else { pdfContentByte.setLineDash(0f); } pdfContentByte.rectangle(printImage.getX() - borderCorrection, jasperPrint.getPageHeight() - printImage.getY() + borderCorrection, printImage.getWidth() + 2 * borderCorrection - 1, -printImage.getHeight() - 2 * borderCorrection + 1); pdfContentByte.stroke(); } }
From source file:com.develog.utils.report.engine.export.JRPdfExporter.java
License:Open Source License
/** * *///w w w. j a v a 2 s. c om protected void exportText(JRPrintText text) throws JRException, DocumentException, IOException { JRStyledText styledText = getStyledText(text); if (styledText == null) { return; } int textLength = styledText.length(); int x = text.getX(); int y = text.getY(); int width = text.getWidth(); int height = text.getHeight(); double angle = 0; switch (text.getRotation()) { case JRTextElement.ROTATION_LEFT: { y = text.getY() + text.getHeight(); width = text.getHeight(); height = text.getWidth(); angle = Math.PI / 2; break; } case JRTextElement.ROTATION_RIGHT: { x = text.getX() + text.getWidth(); width = text.getHeight(); height = text.getWidth(); angle = -Math.PI / 2; break; } case JRTextElement.ROTATION_NONE: default: { } } AffineTransform atrans = new AffineTransform(); atrans.rotate(angle, x, jasperPrint.getPageHeight() - y); pdfContentByte.transform(atrans); if (text.getMode() == JRElement.MODE_OPAQUE) { pdfContentByte.setRGBColorStroke(text.getBackcolor().getRed(), text.getBackcolor().getGreen(), text.getBackcolor().getBlue()); pdfContentByte.setRGBColorFill(text.getBackcolor().getRed(), text.getBackcolor().getGreen(), text.getBackcolor().getBlue()); pdfContentByte.setLineWidth(1f); pdfContentByte.setLineDash(0f); pdfContentByte.rectangle(x, jasperPrint.getPageHeight() - y, width - 1, -height + 1); pdfContentByte.fillStroke(); } else { /* pdfContentByte.setRGBColorStroke( text.getForecolor().getRed(), text.getForecolor().getGreen(), text.getForecolor().getBlue() ); pdfContentByte.setLineWidth(0.1f); pdfContentByte.setLineDash(0f); pdfContentByte.rectangle( text.getX(), jasperPrint.getPageHeight() - text.getY(), text.getWidth(), - text.getHeight() ); pdfContentByte.stroke(); */ } if (textLength == 0) { return; } int horizontalAlignment = Element.ALIGN_LEFT; switch (text.getTextAlignment()) { case JRAlignment.HORIZONTAL_ALIGN_LEFT: { horizontalAlignment = Element.ALIGN_LEFT; break; } case JRAlignment.HORIZONTAL_ALIGN_CENTER: { horizontalAlignment = Element.ALIGN_CENTER; break; } case JRAlignment.HORIZONTAL_ALIGN_RIGHT: { horizontalAlignment = Element.ALIGN_RIGHT; break; } case JRAlignment.HORIZONTAL_ALIGN_JUSTIFIED: { horizontalAlignment = Element.ALIGN_JUSTIFIED; break; } default: { horizontalAlignment = Element.ALIGN_LEFT; } } float verticalOffset = 0f; switch (text.getVerticalAlignment()) { case JRTextElement.VERTICAL_ALIGN_TOP: { verticalOffset = 0f; break; } case JRTextElement.VERTICAL_ALIGN_MIDDLE: { verticalOffset = ((float) height - text.getTextHeight()) / 2f; break; } case JRTextElement.VERTICAL_ALIGN_BOTTOM: { verticalOffset = height - text.getTextHeight(); break; } default: { verticalOffset = 0f; } } ColumnText colText = new ColumnText(pdfContentByte); colText.setSimpleColumn(getPhrase(styledText, getHyperlinkInfoChunk(text)), x, jasperPrint.getPageHeight() - y - verticalOffset - text.getLeadingOffset(), //+ text.getLineSpacingFactor() * text.getFont().getSize(), x + width, jasperPrint.getPageHeight() - y - height, 0, //text.getLineSpacingFactor(),// * text.getFont().getSize(), horizontalAlignment); colText.setLeading(0, text.getLineSpacingFactor());// * text.getFont().getSize()); colText.go(); atrans = new AffineTransform(); atrans.rotate(-angle, x, jasperPrint.getPageHeight() - y); pdfContentByte.transform(atrans); }
From source file:com.dlya.facturews.DlyaPdfExporter2.java
License:Open Source License
protected void writePageAnchor(int pageIndex) throws DocumentException { Map<Attribute, Object> attributes = new HashMap<Attribute, Object>(); FontUtil.getInstance(jasperReportsContext).getAttributesWithoutAwtFont(attributes, new JRBasePrintText(jasperPrint.getDefaultStyleProvider())); Font pdfFont = getFont(attributes, getLocale(), false); Chunk chunk = new Chunk(" ", pdfFont); chunk.setLocalDestination(JR_PAGE_ANCHOR_PREFIX + reportIndex + "_" + (pageIndex + 1)); //tagHelper.startPageAnchor(); ColumnText colText = new ColumnText(pdfContentByte); colText.setSimpleColumn(new Phrase(chunk), 0, jasperPrint.getPageHeight(), 1, 1, 0, Element.ALIGN_LEFT); colText.go();/*from w w w . j av a 2 s .co m*/ //tagHelper.endPageAnchor(); }
From source file:com.dlya.facturews.DlyaPdfExporter2.java
License:Open Source License
/** * *//*from w w w . j av a 2 s. co m*/ public void exportImage(JRPrintImage printImage) throws DocumentException, IOException, JRException { if (printImage.getModeValue() == ModeEnum.OPAQUE) { pdfContentByte.setRGBColorFill(printImage.getBackcolor().getRed(), printImage.getBackcolor().getGreen(), printImage.getBackcolor().getBlue()); pdfContentByte.rectangle(printImage.getX() + getOffsetX(), jasperPrint.getPageHeight() - printImage.getY() - getOffsetY(), printImage.getWidth(), -printImage.getHeight()); pdfContentByte.fill(); } int topPadding = printImage.getLineBox().getTopPadding().intValue(); int leftPadding = printImage.getLineBox().getLeftPadding().intValue(); int bottomPadding = printImage.getLineBox().getBottomPadding().intValue(); int rightPadding = printImage.getLineBox().getRightPadding().intValue(); int availableImageWidth = printImage.getWidth() - leftPadding - rightPadding; availableImageWidth = (availableImageWidth < 0) ? 0 : availableImageWidth; int availableImageHeight = printImage.getHeight() - topPadding - bottomPadding; availableImageHeight = (availableImageHeight < 0) ? 0 : availableImageHeight; Renderable renderer = printImage.getRenderable(); if (renderer != null && availableImageWidth > 0 && availableImageHeight > 0) { if (renderer.getTypeValue() == RenderableTypeEnum.IMAGE) { // Image renderers are all asked for their image data at some point. // Better to test and replace the renderer now, in case of lazy load error. renderer = RenderableUtil.getInstance(jasperReportsContext).getOnErrorRendererForImageData(renderer, printImage.getOnErrorTypeValue()); } } else { renderer = null; } if (renderer != null) { int xoffset = 0; int yoffset = 0; Chunk chunk = null; float scaledWidth = availableImageWidth; float scaledHeight = availableImageHeight; if (renderer.getTypeValue() == RenderableTypeEnum.IMAGE) { com.lowagie.text.Image image = null; float xalignFactor = getXAlignFactor(printImage); float yalignFactor = getYAlignFactor(printImage); switch (printImage.getScaleImageValue()) { case CLIP: { // Image load might fail, from given image data. // Better to test and replace the renderer now, in case of lazy load error. renderer = RenderableUtil.getInstance(jasperReportsContext) .getOnErrorRendererForDimension(renderer, printImage.getOnErrorTypeValue()); if (renderer == null) { break; } int normalWidth = availableImageWidth; int normalHeight = availableImageHeight; Dimension2D dimension = renderer.getDimension(jasperReportsContext); if (dimension != null) { normalWidth = (int) dimension.getWidth(); normalHeight = (int) dimension.getHeight(); } xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth)); yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight)); int minWidth = Math.min(normalWidth, availableImageWidth); int minHeight = Math.min(normalHeight, availableImageHeight); BufferedImage bi = new BufferedImage(minWidth, minHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g = bi.createGraphics(); if (printImage.getModeValue() == ModeEnum.OPAQUE) { g.setColor(printImage.getBackcolor()); g.fillRect(0, 0, minWidth, minHeight); } renderer.render(jasperReportsContext, g, new java.awt.Rectangle((xoffset > 0 ? 0 : xoffset), (yoffset > 0 ? 0 : yoffset), normalWidth, normalHeight)); g.dispose(); xoffset = (xoffset < 0 ? 0 : xoffset); yoffset = (yoffset < 0 ? 0 : yoffset); //awtImage = bi.getSubimage(0, 0, minWidth, minHeight); //image = com.lowagie.text.Image.getInstance(awtImage, printImage.getBackcolor()); image = com.lowagie.text.Image.getInstance(bi, null); break; } case FILL_FRAME: { if (printImage.isUsingCache() && loadedImagesMap.containsKey(renderer)) { image = loadedImagesMap.get(renderer); } else { try { image = com.lowagie.text.Image.getInstance(renderer.getImageData(jasperReportsContext)); imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0); } catch (Exception e) { JRImageRenderer tmpRenderer = JRImageRenderer.getOnErrorRendererForImage( jasperReportsContext, JRImageRenderer.getInstance(renderer.getImageData(jasperReportsContext)), printImage.getOnErrorTypeValue()); if (tmpRenderer == null) { break; } java.awt.Image awtImage = tmpRenderer.getImage(jasperReportsContext); image = com.lowagie.text.Image.getInstance(awtImage, null); } if (printImage.isUsingCache()) { loadedImagesMap.put(renderer, image); } } image.scaleAbsolute(availableImageWidth, availableImageHeight); break; } case RETAIN_SHAPE: default: { if (printImage.isUsingCache() && loadedImagesMap.containsKey(renderer)) { image = loadedImagesMap.get(renderer); } else { try { image = com.lowagie.text.Image.getInstance(renderer.getImageData(jasperReportsContext)); imageTesterPdfContentByte.addImage(image, 10, 0, 0, 10, 0, 0); } catch (Exception e) { JRImageRenderer tmpRenderer = JRImageRenderer.getOnErrorRendererForImage( jasperReportsContext, JRImageRenderer.getInstance(renderer.getImageData(jasperReportsContext)), printImage.getOnErrorTypeValue()); if (tmpRenderer == null) { break; } java.awt.Image awtImage = tmpRenderer.getImage(jasperReportsContext); image = com.lowagie.text.Image.getInstance(awtImage, null); } if (printImage.isUsingCache()) { loadedImagesMap.put(renderer, image); } } image.scaleToFit(availableImageWidth, availableImageHeight); xoffset = (int) (xalignFactor * (availableImageWidth - image.plainWidth())); yoffset = (int) (yalignFactor * (availableImageHeight - image.plainHeight())); xoffset = (xoffset < 0 ? 0 : xoffset); yoffset = (yoffset < 0 ? 0 : yoffset); break; } } if (image != null) { chunk = new Chunk(image, 0, 0); scaledWidth = image.scaledWidth(); scaledHeight = image.scaledHeight(); } } else { double normalWidth = availableImageWidth; double normalHeight = availableImageHeight; double displayWidth = availableImageWidth; double displayHeight = availableImageHeight; double ratioX = 1f; double ratioY = 1f; Rectangle2D clip = null; Dimension2D dimension = renderer.getDimension(jasperReportsContext); if (dimension != null) { normalWidth = dimension.getWidth(); normalHeight = dimension.getHeight(); displayWidth = normalWidth; displayHeight = normalHeight; float xalignFactor = getXAlignFactor(printImage); float yalignFactor = getYAlignFactor(printImage); switch (printImage.getScaleImageValue()) { case CLIP: { xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth)); yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight)); clip = new Rectangle2D.Double(-xoffset, -yoffset, availableImageWidth, availableImageHeight); break; } case FILL_FRAME: { ratioX = availableImageWidth / normalWidth; ratioY = availableImageHeight / normalHeight; normalWidth *= ratioX; normalHeight *= ratioY; xoffset = 0; yoffset = 0; break; } case RETAIN_SHAPE: default: { ratioX = availableImageWidth / normalWidth; ratioY = availableImageHeight / normalHeight; ratioX = ratioX < ratioY ? ratioX : ratioY; ratioY = ratioX; normalWidth *= ratioX; normalHeight *= ratioY; xoffset = (int) (xalignFactor * (availableImageWidth - normalWidth)); yoffset = (int) (yalignFactor * (availableImageHeight - normalHeight)); break; } } } PdfTemplate template = pdfContentByte.createTemplate((float) displayWidth, (float) displayHeight); Graphics2D g = forceSvgShapes ? template.createGraphicsShapes((float) displayWidth, (float) displayHeight) : template.createGraphics(availableImageWidth, availableImageHeight, new LocalFontMapper()); if (clip != null) { g.setClip(clip); } if (printImage.getModeValue() == ModeEnum.OPAQUE) { g.setColor(printImage.getBackcolor()); g.fillRect(0, 0, (int) displayWidth, (int) displayHeight); } Rectangle2D rectangle = new Rectangle2D.Double(0, 0, displayWidth, displayHeight); renderer.render(jasperReportsContext, g, rectangle); g.dispose(); pdfContentByte.saveState(); pdfContentByte.addTemplate(template, (float) ratioX, 0f, 0f, (float) ratioY, printImage.getX() + getOffsetX() + xoffset, jasperPrint.getPageHeight() - printImage.getY() - getOffsetY() - (int) normalHeight - yoffset); pdfContentByte.restoreState(); Image image = getPxImage(); image.scaleAbsolute(availableImageWidth, availableImageHeight); chunk = new Chunk(image, 0, 0); } /* image.setAbsolutePosition( printImage.getX() + offsetX + borderOffset, jasperPrint.getPageHeight() - printImage.getY() - offsetY - image.scaledHeight() - borderOffset ); pdfContentByte.addImage(image); */ if (chunk != null) { setAnchor(chunk, printImage, printImage); setHyperlinkInfo(chunk, printImage); //tagHelper.startImage(printImage); ColumnText colText = new ColumnText(pdfContentByte); int upperY = jasperPrint.getPageHeight() - printImage.getY() - topPadding - getOffsetY() - yoffset; int lowerX = printImage.getX() + leftPadding + getOffsetX() + xoffset; colText.setSimpleColumn(new Phrase(chunk), lowerX, upperY - scaledHeight, lowerX + scaledWidth, upperY, scaledHeight, Element.ALIGN_LEFT); colText.go(); //tagHelper.endImage(); } } if (printImage.getLineBox().getTopPen().getLineWidth().floatValue() <= 0f && printImage.getLineBox().getLeftPen().getLineWidth().floatValue() <= 0f && printImage.getLineBox().getBottomPen().getLineWidth().floatValue() <= 0f && printImage.getLineBox().getRightPen().getLineWidth().floatValue() <= 0f) { if (printImage.getLinePen().getLineWidth().floatValue() > 0f) { exportPen(printImage.getLinePen(), printImage); } } else { /* */ exportBox(printImage.getLineBox(), printImage); } }
From source file:com.fcore.base.fileSystem.utils.FileUtil.java
/** * convert txt file to pdf // ww w. ja v a2s . c o m * @param originalPath * @param dirPath */ public static void txt2pdf(String originalPath, String dirPath) { com.lowagie.text.Document document = null; BufferedReader read = null; long old = System.currentTimeMillis(); try { document = new com.lowagie.text.Document(PageSize.A4, 80, 80, 60, 30); PdfWriter.getInstance(document, new FileOutputStream(dirPath)); document.open(); BaseFont bfChinese = BaseFont.createFont(ReadCreatePdf.class.getResource("/") + "/simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font FontChinese = new Font(bfChinese, 18, Font.NORMAL); FileInputStream fstream = new FileInputStream(originalPath); DataInputStream in = new DataInputStream(fstream); read = new BufferedReader(new InputStreamReader(in, "gb2312")); String line = null; while ((line = read.readLine()) != null) { Paragraph t = new Paragraph(line, FontChinese); t.setAlignment(Element.ALIGN_LEFT); t.setLeading(20.0f); document.add(t); } } catch (Exception e) { e.printStackTrace(); } finally { try { read.close(); document.close(); } catch (IOException e) { e.printStackTrace(); } } long now = System.currentTimeMillis(); System.out.println("" + ((now - old) / 1000.0) + "\n\n" + "?:" + dirPath); }
From source file:com.geek.tutorial.itext.acroform.RadioCheckBoxForm.java
License:Open Source License
public RadioCheckBoxForm() throws Exception { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("RadioCheckBoxForm.pdf")); document.open();//from ww w. j a va 2 s . c o m PdfContentByte cb = writer.getDirectContent(); BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); Rectangle rect; // Code 1 create radio button String[] radios = { "Radio1", "Radio2", "Radio3" }; PdfFormField radioField = PdfFormField.createRadioButton(writer, true); radioField.setFieldName("radioField"); radioField.setValueAsName(radios[0]); for (int i = 0; i < radios.length; i++) { rect = new Rectangle(40, 815 - i * 30, 60, 797 - i * 30); addRadioButton(writer, rect, radioField, radios[i], i == 0); cb.beginText(); cb.setFontAndSize(bf, 12); cb.showTextAligned(Element.ALIGN_LEFT, radios[i], 70, 802 - i * 30, 0); cb.endText(); } writer.addAnnotation(radioField); // Code 2 create checkbox button String[] options = { "Check1", "Check2", "Check3" }; for (int i = 0; i < options.length; i++) { rect = new Rectangle(160, 815 - i * 30, 180, 797 - i * 30); addCheckbox(writer, rect, options[i]); cb.beginText(); cb.setFontAndSize(bf, 12); cb.showTextAligned(Element.ALIGN_LEFT, options[i], 190, 802 - i * 30, 0); cb.endText(); } // Code 3 add function and button for showing state writer.addJavaScript( "function showState(){" + "app.alert('Radio:'+ this.getField('radioField').value +'\\n\\n'+" + "'Check1:'+this.getField('Check1').value +'\\n'+" + "'Check2:'+this.getField('Check2').value +'\\n'+" + "'Check3:'+this.getField('Check3').value);" + "}"); PushbuttonField push = new PushbuttonField(writer, new Rectangle(80, 710, 150, 730), "pushAction"); push.setBackgroundColor(Color.LIGHT_GRAY); push.setBorderColor(Color.GRAY); push.setText("Show State"); push.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED); push.setTextColor(Color.BLACK); PdfFormField pushbutton = push.getField(); pushbutton.setAction(PdfAction.javaScript("showState()", writer)); writer.addAnnotation(pushbutton); document.close(); }
From source file:com.geek.tutorial.itext.acroform.TextFieldForm.java
License:Open Source License
public TextFieldForm() throws Exception { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("TextFieldForm.pdf")); document.open();/*from w w w .ja va2 s. c o m*/ PdfPTable table = new PdfPTable(2); table.getDefaultCell().setPadding(5f); // Code 1, will only affect empty field table.setHorizontalAlignment(Element.ALIGN_LEFT); PdfPCell cell; // Code 2, add name TextField table.addCell("Name"); TextField nameField = new TextField(writer, new Rectangle(0, 0, 200, 10), "nameField"); nameField.setBackgroundColor(Color.WHITE); nameField.setBorderColor(Color.BLACK); nameField.setBorderWidth(1); nameField.setBorderStyle(PdfBorderDictionary.STYLE_SOLID); nameField.setText(""); nameField.setAlignment(Element.ALIGN_LEFT); nameField.setOptions(TextField.REQUIRED); cell = new PdfPCell(); cell.setMinimumHeight(10); cell.setCellEvent(new FieldCell(nameField.getTextField(), 200, writer)); table.addCell(cell); // force upper case javascript writer.addJavaScript("var nameField = this.getField('nameField');" + "nameField.setAction('Keystroke'," + "'forceUpperCase()');" + "" + "function forceUpperCase(){" + "if(!event.willCommit)event.change = " + "event.change.toUpperCase();" + "}"); // Code 3, add empty row table.addCell(""); table.addCell(""); // Code 4, add age TextField table.addCell("Age"); TextField ageComb = new TextField(writer, new Rectangle(0, 0, 30, 10), "ageField"); ageComb.setBorderColor(Color.BLACK); ageComb.setBorderWidth(1); ageComb.setBorderStyle(PdfBorderDictionary.STYLE_SOLID); ageComb.setText("12"); ageComb.setAlignment(Element.ALIGN_RIGHT); ageComb.setMaxCharacterLength(2); ageComb.setOptions(TextField.COMB | TextField.DO_NOT_SCROLL); cell = new PdfPCell(); cell.setMinimumHeight(10); cell.setCellEvent(new FieldCell(ageComb.getTextField(), 30, writer)); table.addCell(cell); // validate age javascript writer.addJavaScript("var ageField = this.getField('ageField');" + "ageField.setAction('Validate','checkAge()');" + "function checkAge(){" + "if(event.value < 12){" + "app.alert('Warning! Applicant\\'s age can not" + " be younger than 12.');" + "event.value = 12;" + "}}"); // add empty row table.addCell(""); table.addCell(""); // Code 5, add age TextField table.addCell("Comment"); TextField comment = new TextField(writer, new Rectangle(0, 0, 200, 100), "commentField"); comment.setBorderColor(Color.BLACK); comment.setBorderWidth(1); comment.setBorderStyle(PdfBorderDictionary.STYLE_SOLID); comment.setText(""); comment.setOptions(TextField.MULTILINE | TextField.DO_NOT_SCROLL); cell = new PdfPCell(); cell.setMinimumHeight(100); cell.setCellEvent(new FieldCell(comment.getTextField(), 200, writer)); table.addCell(cell); // check comment characters length javascript writer.addJavaScript("var commentField = " + "this.getField('commentField');" + "commentField" + ".setAction('Keystroke','checkLength()');" + "function checkLength(){" + "if(!event.willCommit && " + "event.value.length > 100){" + "app.alert('Warning! Comment can not " + "be more than 100 characters.');" + "event.change = '';" + "}}"); // add empty row table.addCell(""); table.addCell(""); // Code 6, add submit button PushbuttonField submitBtn = new PushbuttonField(writer, new Rectangle(0, 0, 35, 15), "submitPOST"); submitBtn.setBackgroundColor(Color.GRAY); submitBtn.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED); submitBtn.setText("POST"); submitBtn.setOptions(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT); PdfFormField submitField = submitBtn.getField(); submitField.setAction(PdfAction.createSubmitForm("http://www.geek-tutorials.com/java/itext/submit.php", null, PdfAction.SUBMIT_HTML_FORMAT)); cell = new PdfPCell(); cell.setMinimumHeight(15); cell.setCellEvent(new FieldCell(submitField, 35, writer)); table.addCell(cell); // Code 7, add reset button PushbuttonField resetBtn = new PushbuttonField(writer, new Rectangle(0, 0, 35, 15), "reset"); resetBtn.setBackgroundColor(Color.GRAY); resetBtn.setBorderStyle(PdfBorderDictionary.STYLE_BEVELED); resetBtn.setText("RESET"); resetBtn.setOptions(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT); PdfFormField resetField = resetBtn.getField(); resetField.setAction(PdfAction.createResetForm(null, 0)); cell = new PdfPCell(); cell.setMinimumHeight(15); cell.setCellEvent(new FieldCell(resetField, 35, writer)); table.addCell(cell); document.add(table); document.close(); }
From source file:com.geek.tutorial.itext.table.SimplePDFTableAlignAndWidth.java
License:Open Source License
public SimplePDFTableAlignAndWidth() throws Exception { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("SimplePDFTableAlignAndWidth.pdf")); document.open();//from w w w . j a v a 2s . c om float[] colsWidth = { 1f, 2f }; // Code 1 PdfPTable table = new PdfPTable(colsWidth); table.setWidthPercentage(90); // Code 2 table.setHorizontalAlignment(Element.ALIGN_LEFT); // Code 3 table.addCell("1"); table.addCell("2"); table.addCell("3"); table.addCell("4"); table.addCell("5"); table.addCell("6"); document.add(table); document.close(); }
From source file:com.gp.cong.logisoft.lcl.report.FreightInvoiceLclPdfCreator.java
public void onStartArRedInvoicePage(PdfWriter writer, Document document) { try {// w w w . ja v a 2 s . c o m SystemRulesDAO systemRulesDAO = new SystemRulesDAO(); String companyAddress = systemRulesDAO.getSystemRulesByCode("CompanyAddress"); String companyPhone = systemRulesDAO.getSystemRulesByCode("CompanyPhone"); String companyFax = systemRulesDAO.getSystemRulesByCode("CompanyFax"); PdfPCell cell = new PdfPCell(); PdfPTable headingMainTable = new PdfPTable(1); headingMainTable.setWidthPercentage(100); PdfPTable headingTable = new PdfPTable(1); headingTable.setWidths(new float[] { 100 }); PdfPTable imgTable = new PdfPTable(1); imgTable.setWidthPercentage(100); Image img = null; String logoImage = ""; String brand = this.setBrand(fileNumberId); if (CommonUtils.isNotEmpty(brand)) { if ("ECI".equalsIgnoreCase(brand)) { logoImage = LoadLogisoftProperties.getProperty("application.image.econo.logo"); img = Image.getInstance(realPath + logoImage); img.scalePercent(75); } else if ("OTI".equalsIgnoreCase(brand)) { logoImage = LoadLogisoftProperties.getProperty("application.image.econo.logo"); img = Image.getInstance(realPath + logoImage); img.scalePercent(45); } else { logoImage = LoadLogisoftProperties.getProperty("application.image.logo"); img = Image.getInstance(realPath + logoImage); img.scalePercent(45); } } img.scalePercent(75); PdfPCell logoCell = new PdfPCell(img); logoCell.setBorder(Rectangle.NO_BORDER); logoCell.setHorizontalAlignment(Element.ALIGN_LEFT); logoCell.setVerticalAlignment(Element.ALIGN_LEFT); logoCell.setPaddingLeft(+27); imgTable.addCell(logoCell); PdfPTable addrTable = new PdfPTable(1); addrTable.setWidthPercentage(100); PdfPTable invoiceFacturaTable = new PdfPTable(3); invoiceFacturaTable.setWidthPercentage(100); invoiceFacturaTable.setWidths(new float[] { 40, 20, 40 }); StringBuilder stringBuilder = new StringBuilder(); addrTable.addCell(makeCellCenterNoBorderFclBL("MAILING ADDRESS: " + (CommonUtils.isNotEmpty(companyAddress) ? companyAddress.toUpperCase() : ""))); stringBuilder.append("TEL: "); stringBuilder.append(CommonUtils.isNotEmpty(companyPhone) ? companyPhone : "").append(" / "); stringBuilder.append("FAX: "); stringBuilder.append(CommonUtils.isNotEmpty(companyFax) ? companyFax : ""); addrTable.addCell(makeCellCenterNoBorderFclBL(stringBuilder.toString())); addrTable.addCell(makeCellLeftNoBorderFclBL("")); addrTable.addCell(makeCellLeftNoBorderFclBL("")); invoiceFacturaTable.addCell(makeCellLeftNoBorderFclBL("")); cell = makeCell("INVOICE", Element.ALIGN_CENTER, new Font(Font.HELVETICA, 12, Font.BOLD, Color.RED), 0.06f); invoiceFacturaTable.addCell(cell); invoiceFacturaTable.addCell(makeCellLeftNoBorderFclBL("")); cell = new PdfPCell(); cell.addElement(invoiceFacturaTable); cell.setBorder(0); addrTable.addCell(cell); addrTable.addCell(makeCellLeftNoBorderFclBL("")); addrTable.addCell(makeCellLeftNoBorderFclBL("")); cell = new PdfPCell(); cell.addElement(imgTable); cell.setBorder(0); cell.setPaddingLeft(+150); headingMainTable.addCell(cell); // headingTable.addCell(cell); cell = new PdfPCell(); cell.addElement(addrTable); cell.setBorder(0); headingTable.addCell(cell); cell = makeCellLeftNoBorderFclBL(""); cell.setBorderWidthRight(0.06f); cell.setBorderWidthLeft(0.06f); cell.setBorderWidthTop(0.06f); cell.setBorderWidthBottom(0.0f); cell.addElement(headingTable); headingMainTable.addCell(cell); document.add(headingMainTable); } catch (Exception e) { throw new ExceptionConverter(e); } }