public BaseColor createBaseColor( Color color ) throws CreateColorException{ if( color.getColor().length < 4 && color.getColor().length > 5 ){ throw new CreateColorException( "Incorrect length of color. Should be [cyan,magenta,yellow,black] or [cyan,magenta,yellow,black,tint]"); } if( color.getRef() == null ){ throw new CreateColorException( "You must specify a reference" ); } for( float c : color.getColor() ){ if( c > 1 || c < 0 ){ throw new CreateColorException( "[cyan,magenta,yellow,black] or [cyan,magenta,yellow,black,tint] must be between 0 and 1" ); } } float[] colors = color.getColor(); CMYKColor baseColor = new CMYKColor( colors[0],colors[1],colors[2],colors[3] ); if( color.getColor().length == 4 ){ return baseColor; } return new SpotColor(new PdfSpotColor(color.getRef(), baseColor),colors[4]); }
/** * This method checks whether two colors are approximately equal. As the * sample document only uses CMYK colors, only this comparison has been * implemented yet. */ boolean approximates(BaseColor colorA, BaseColor colorB) { if (colorA == null || colorB == null) return colorA == colorB; if (colorA instanceof CMYKColor && colorB instanceof CMYKColor) { CMYKColor cmykA = (CMYKColor) colorA; CMYKColor cmykB = (CMYKColor) colorB; float c = Math.abs(cmykA.getCyan() - cmykB.getCyan()); float m = Math.abs(cmykA.getMagenta() - cmykB.getMagenta()); float y = Math.abs(cmykA.getYellow() - cmykB.getYellow()); float k = Math.abs(cmykA.getBlack() - cmykB.getBlack()); return c+m+y+k < 0.01; } // TODO: Implement comparison for other color types return false; }
/** * <a href="http://stackoverflow.com/questions/31730278/extract-text-from-pdf-between-two-dividers-with-itextsharp"> * Extract text from PDF between two dividers with ITextSharp * </a> * <br> * <a href="http://www.tjsc.jus.br/institucional/diario/a2015/20150211600.PDF"> * 20150211600.PDF * </a> * <p> * This test applies the {@link DividerAndColorAwareTextExtractionStrategy} to the OP's sample * document, page 346, to demonstrate its use. * </p> */ @Test public void test20150211600_346() throws IOException, DocumentException { InputStream resourceStream = getClass().getResourceAsStream("20150211600.PDF"); BaseColor headerColor = new CMYKColor(0.58f, 0.17f, 0, 0.56f); try { PdfReader reader = new PdfReader(resourceStream); String content = extractAndStore(reader, new File(RESULT_FOLDER, "20150211600.%s.%s.txt").toString(), 346, 346, headerColor); System.out.println("\nText 20150211600.PDF\n************************"); System.out.println(content); System.out.println("************************"); } finally { if (resourceStream != null) resourceStream.close(); } }
/** * 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. * * @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()); } } }
public CMYKColor getBlack(){ return new CMYKColor(0.0f, 0.0f, 0.0f, 1.0f ); }
public Font stepKeywordFont() { if (stepKeywordFont == null) stepKeywordFont = FontFactory.getFont(defaultMonospaceFontname(), 8, Font.BOLD, new CMYKColor(255, 255, 0, 17)); return stepKeywordFont; }
public Font stepDefaultFont() { if (stepDefaultFont == null) stepDefaultFont = FontFactory.getFont(defaultMonospaceFontname(), 8, Font.NORMAL, new CMYKColor(255, 255, 0, 17)); return stepDefaultFont; }
public Font tagsFont() { return FontFactory.getFont(defaultMonospaceFontname(), 8, Font.ITALIC, new CMYKColor(25, 255, 255, 17)); }