List of usage examples for com.itextpdf.text.pdf PdfContentByte closePath
public void closePath()
From source file:com.ephesoft.dcma.imagemagick.MultiPageExecutor.java
License:Open Source License
/** * The <code>addImageToPdf</code> method is used to add image to pdf and make it searchable by adding image text in invisible mode * w.r.t parameter 'isPdfSearchable' passed. * /* w w w.ja va 2 s. c o m*/ * @param pdfWriter {@link PdfWriter} writer of pdf in which image has to be added * @param htmlUrl {@link HocrPage} corresponding html file for fetching text and coordinates * @param imageUrl {@link String} url of image to be added in pdf * @param isPdfSearchable true for searchable pdf else otherwise * @param widthOfLine */ private void addImageToPdf(PdfWriter pdfWriter, HocrPage hocrPage, String imageUrl, boolean isPdfSearchable, final int widthOfLine) { if (null != pdfWriter && null != imageUrl && imageUrl.length() > 0) { try { LOGGER.info("Adding image" + imageUrl + " to pdf using iText"); Image pageImage = Image.getInstance(imageUrl); float dotsPerPointX = pageImage.getDpiX() / PDF_RESOLUTION; float dotsPerPointY = pageImage.getDpiY() / PDF_RESOLUTION; PdfContentByte pdfContentByte = pdfWriter.getDirectContent(); pageImage.scaleToFit(pageImage.getWidth() / dotsPerPointX, pageImage.getHeight() / dotsPerPointY); pageImage.setAbsolutePosition(0, 0); // Add image to pdf pdfWriter.getDirectContentUnder().addImage(pageImage); pdfWriter.getDirectContentUnder().add(pdfContentByte); // If pdf is to be made searchable if (isPdfSearchable) { LOGGER.info("Adding invisible text for image: " + imageUrl); float pageImagePixelHeight = pageImage.getHeight(); Font defaultFont = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.BOLD, CMYKColor.BLACK); // Fetch text and coordinates for image to be added Map<String, int[]> textCoordinatesMap = getTextWithCoordinatesMap(hocrPage, widthOfLine); Set<String> ketSet = textCoordinatesMap.keySet(); // Add text at specific location for (String key : ketSet) { int[] coordinates = textCoordinatesMap.get(key); float bboxWidthPt = (coordinates[2] - coordinates[0]) / dotsPerPointX; float bboxHeightPt = (coordinates[3] - coordinates[1]) / dotsPerPointY; pdfContentByte.beginText(); // To make text added as invisible pdfContentByte.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE); pdfContentByte.setLineWidth(Math.round(bboxWidthPt)); // Ceil is used so that minimum font of any text is 1 // For exception of unbalanced beginText() and endText() if (bboxHeightPt > 0.0) { pdfContentByte.setFontAndSize(defaultFont.getBaseFont(), (float) Math.ceil(bboxHeightPt)); } else { pdfContentByte.setFontAndSize(defaultFont.getBaseFont(), 1); } float xCoordinate = (float) (coordinates[0] / dotsPerPointX); float yCoordinate = (float) ((pageImagePixelHeight - coordinates[3]) / dotsPerPointY); pdfContentByte.moveText(xCoordinate, yCoordinate); pdfContentByte.showText(key); pdfContentByte.endText(); } } pdfContentByte.closePath(); } catch (BadElementException badElementException) { LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: " + badElementException.toString()); } catch (DocumentException documentException) { LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: " + documentException.toString()); } catch (MalformedURLException malformedURLException) { LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: " + malformedURLException.toString()); } catch (IOException ioException) { LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: " + ioException.toString()); } } }
From source file:com.ephesoft.dcma.util.PDFUtil.java
License:Open Source License
/** * The <code>closePassedStream</code> method closes the stream passed. * //from w ww . j av a 2s . c om * @param reader {@link PdfReader} * @param document {@link Document} * @param contentByte {@link PdfContentByte} * @param writer {@link PdfWriter} * @param fileInputStream {@link FileInputStream} * @param fileOutputStream {@link FileOutputStream} * @throws IOException {@link} if unable to close input or output stream */ private static void closePassedStream(final PdfReader reader, final Document document, final PdfContentByte contentByte, final PdfWriter writer, final FileInputStream fileInputStream, final FileOutputStream fileOutputStream) throws IOException { if (null != reader) { reader.close(); } if (null != document) { document.close(); } if (null != contentByte) { contentByte.closePath(); } if (null != writer) { writer.close(); } if (null != fileInputStream) { fileInputStream.close(); } if (null != fileOutputStream) { fileOutputStream.flush(); fileOutputStream.close(); } }
From source file:mkl.testarea.itext5.pdfcleanup.StrictPdfCleanUpProcessor.java
License:Open Source License
private void addColoredRectangle(PdfContentByte canvas, PdfCleanUpLocation cleanUpLocation) { Rectangle cleanUpRegion = cleanUpLocation.getRegion(); canvas.saveState();//from w ww . ja v a 2s . co m canvas.setColorFill(cleanUpLocation.getCleanUpColor()); canvas.moveTo(cleanUpRegion.getLeft(), cleanUpRegion.getBottom()); canvas.lineTo(cleanUpRegion.getRight(), cleanUpRegion.getBottom()); canvas.lineTo(cleanUpRegion.getRight(), cleanUpRegion.getTop()); canvas.lineTo(cleanUpRegion.getLeft(), cleanUpRegion.getTop()); canvas.closePath(); canvas.fill(); canvas.restoreState(); }
From source file:org.gephi.preview.plugin.renderers.ArrowRenderer.java
License:Open Source License
public void renderStraight(RenderTarget target, Item item, float x1, float y1, float x2, float y2, float radius, float size, Color color) { Edge edge = (Edge) item.getSource(); PVector direction = new PVector(x2, y2); direction.sub(new PVector(x1, y1)); direction.normalize();// w w w . j a v a 2 s . c om PVector p1 = new PVector(direction.x, direction.y); p1.mult(radius); p1.add(new PVector(x2, y2)); PVector p1r = new PVector(direction.x, direction.y); p1r.mult(radius - size); p1r.add(new PVector(x2, y2)); PVector p2 = new PVector(-direction.y, direction.x); p2.mult(size * BASE_RATIO); p2.add(p1r); PVector p3 = new PVector(direction.y, -direction.x); p3.mult(size * BASE_RATIO); p3.add(p1r); if (target instanceof ProcessingTarget) { PGraphics graphics = ((ProcessingTarget) target).getGraphics(); graphics.noStroke(); graphics.fill(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha()); graphics.triangle(p1.x, p1.y, p2.x, p2.y, p3.x, p3.y); } else if (target instanceof SVGTarget) { SVGTarget svgTarget = (SVGTarget) target; Element arrowElem = svgTarget.createElement("polyline"); arrowElem.setAttribute("points", String.format(Locale.ENGLISH, "%f,%f %f,%f %f,%f", p1.x, p1.y, p2.x, p2.y, p3.x, p3.y)); arrowElem.setAttribute("class", edge.getSource().getNodeData().getId() + " " + edge.getTarget().getNodeData().getId()); arrowElem.setAttribute("fill", svgTarget.toHexString(color)); arrowElem.setAttribute("fill-opacity", (color.getAlpha() / 255f) + ""); arrowElem.setAttribute("stroke", "none"); svgTarget.getTopElement(SVGTarget.TOP_ARROWS).appendChild(arrowElem); } else if (target instanceof PDFTarget) { PDFTarget pdfTarget = (PDFTarget) target; PdfContentByte cb = pdfTarget.getContentByte(); cb.moveTo(p1.x, -p1.y); cb.lineTo(p2.x, -p2.y); cb.lineTo(p3.x, -p3.y); cb.closePath(); cb.setRGBColorFill(color.getRed(), color.getGreen(), color.getBlue()); if (color.getAlpha() < 255) { cb.saveState(); float alpha = color.getAlpha() / 255f; PdfGState gState = new PdfGState(); gState.setFillOpacity(alpha); cb.setGState(gState); } cb.fill(); if (color.getAlpha() < 255) { cb.restoreState(); } } }