List of usage examples for com.itextpdf.text.pdf PdfContentByte moveText
public void moveText(final float x, final float y)
From source file:com.automaster.autoview.server.servlet.TextStateOperators.java
/** * Creates a PDF document.//from w ww. j a v a 2s .co 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.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 ww.j av a 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:Controlador.ControladorCrearPase.java
private static void absText(PdfWriter writer, String text, int x, int y) throws DocumentException, IOException { PdfContentByte cb = writer.getDirectContent(); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.saveState();/* w w w . j av a 2s. co m*/ cb.beginText(); cb.moveText(x, y); cb.setFontAndSize(bf, 12); cb.showText(text); cb.endText(); cb.restoreState(); }
From source file:de.jost_net.JVerein.io.FormularAufbereitung.java
License:Open Source License
private void goFormularfeld(PdfContentByte contentByte, Formularfeld feld, Object val) throws DocumentException, IOException { BaseFont bf = null;/*w w w . j a v a2s .com*/ if (feld.getFont().startsWith("FreeSans")) { String filename = "/fonts/FreeSans"; if (feld.getFont().length() > 8) { filename += feld.getFont().substring(9); } bf = BaseFont.createFont(filename + ".ttf", BaseFont.IDENTITY_H, true); } else { bf = BaseFont.createFont(feld.getFont(), BaseFont.CP1250, false); } float x = mm2point(feld.getX().floatValue()); float y = mm2point(feld.getY().floatValue()); if (val == null) { return; } buendig = links; String stringVal = getString(val); stringVal = stringVal.replace("\\n", "\n"); stringVal = stringVal.replaceAll("\r\n", "\n"); String[] ss = stringVal.split("\n"); for (String s : ss) { contentByte.setFontAndSize(bf, feld.getFontsize().floatValue()); contentByte.beginText(); float offset = 0; if (buendig == rechts) { offset = contentByte.getEffectiveStringWidth(s, true); } contentByte.moveText(x - offset, y); contentByte.showText(s); contentByte.endText(); y -= feld.getFontsize().floatValue() + 3; } }
From source file:pdf.PdfCreator.java
public String createPdf(String path, String ingredients, String recipeTitle, String recipeDescription) throws DocumentException, IOException { path += "shoppingList.pdf"; Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path)); document.open();// w w w .j ava 2 s .c o m String[] ings = ingredients.split(";"); PdfContentByte cb = writer.getDirectContent(); BaseFont bf = BaseFont.createFont(); cb.beginText(); Paragraph p = new Paragraph("Ingredients you'll need to buy:", new Font(FontFamily.HELVETICA, 20, Font.BOLD, new BaseColor(38, 165, 154))); p.add(Chunk.NEWLINE); document.add(p); cb.moveText(36, 750); cb.setFontAndSize(bf, 15); cb.endText(); p = new Paragraph(); for (String str : ings) { p.add(" "); p.add(str); p.add(Chunk.NEWLINE); } p.add(Chunk.NEWLINE); p.add(Chunk.NEWLINE); document.add(p); p = new Paragraph(recipeTitle, new Font(FontFamily.HELVETICA, 20, Font.BOLD, new BaseColor(38, 165, 154))); p.add(Chunk.NEWLINE); document.add(p); p = new Paragraph(recipeDescription, new Font(bf, 12)); p.add(Chunk.NEWLINE); document.add(p); document.close(); return path; }
From source file:pl.marcinmilkowski.hocrtopdf.Main.java
License:Open Source License
/** * @param args//from www . j a va 2s .c o m */ public static void main(String[] args) { try { if (args.length < 1 || args[0] == "--help" || args[0] == "-h") { System.out.print("Usage: java pl.marcinmilkowski.hocrtopdf.Main INPUTURL.html OUTPUTURL.pdf\n" + "\n" + "Converts hOCR files into PDF\n" + "\n" + "Example: java pl.marcinmilkowski.hocrtopdf.Main hocr.html output.pdf\n"); if (args.length < 1) System.exit(-1); else System.exit(0); } URL inputHOCRFile = null; FileOutputStream outputPDFStream = null; try { File file = new File(args[0]); inputHOCRFile = file.toURI().toURL(); } catch (MalformedURLException e) { System.out.println("The first parameter has to be a valid file."); System.out.println("We got an error: " + e.getMessage()); System.exit(-1); } try { outputPDFStream = new FileOutputStream(args[1]); } catch (FileNotFoundException e) { System.out.println("The second parameter has to be a valid URL"); System.exit(-1); } // The resolution of a PDF file (using iText) is 72pt per inch float pointsPerInch = 72.0f; // Using the jericho library to parse the HTML file Source source = new Source(inputHOCRFile); int pageCounter = 1; Document pdfDocument = null; PdfWriter pdfWriter = null; PdfContentByte cb = null; RandomAccessFileOrArray ra = null; // Find the tag of class ocr_page in order to load the scanned image StartTag pageTag = source.getNextStartTag(0, "class", OCRPAGE); while (pageTag != null) { int prevPos = pageTag.getEnd(); Pattern imagePattern = Pattern.compile("image\\s+([^;]+)"); Matcher imageMatcher = imagePattern.matcher(pageTag.getElement().getAttributeValue("title")); if (!imageMatcher.find()) { System.out.println("Could not find a tag of class \"ocr_page\", aborting."); System.exit(-1); } // Load the image Image pageImage = null; try { File file = new File(imageMatcher.group(1)); pageImage = Image.getInstance(file.toURI().toURL()); } catch (MalformedURLException e) { System.out.println("Could not load the scanned image from: " + "file://" + imageMatcher.group(1) + ", aborting."); System.exit(-1); } if (pageImage.getOriginalType() == Image.ORIGINAL_TIFF) { // this might // be // multipage // tiff! File file = new File(imageMatcher.group(1)); if (pageCounter == 1 || ra == null) { ra = new RandomAccessFileOrArray(file.toURI().toURL()); } int nPages = TiffImage.getNumberOfPages(ra); if (nPages > 0 && pageCounter <= nPages) { pageImage = TiffImage.getTiffImage(ra, pageCounter); } } int dpiX = pageImage.getDpiX(); if (dpiX == 0) { // for images that don't set the resolution we assume // 300 dpi dpiX = 300; } int dpiY = pageImage.getDpiY(); if (dpiY == 0) { // as above for dpiX dpiY = 300; } float dotsPerPointX = dpiX / pointsPerInch; float dotsPerPointY = dpiY / pointsPerInch; float pageImagePixelHeight = pageImage.getHeight(); if (pdfDocument == null) { pdfDocument = new Document(new Rectangle(pageImage.getWidth() / dotsPerPointX, pageImage.getHeight() / dotsPerPointY)); pdfWriter = PdfWriter.getInstance(pdfDocument, outputPDFStream); pdfDocument.open(); // Put the text behind the picture (reverse for debugging) // cb = pdfWriter.getDirectContentUnder(); cb = pdfWriter.getDirectContent(); } else { pdfDocument.setPageSize(new Rectangle(pageImage.getWidth() / dotsPerPointX, pageImage.getHeight() / dotsPerPointY)); pdfDocument.newPage(); } // first define a standard font for our text BaseFont base = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED); Font defaultFont = new Font(base, 8); // FontFactory.getFont(FontFactory.HELVETICA, 8, Font.BOLD, // CMYKColor.BLACK); cb.setHorizontalScaling(1.0f); pageImage.scaleToFit(pageImage.getWidth() / dotsPerPointX, pageImage.getHeight() / dotsPerPointY); pageImage.setAbsolutePosition(0, 0); // Put the image in front of the text (reverse for debugging) // pdfWriter.getDirectContent().addImage(pageImage); pdfWriter.getDirectContentUnder().addImage(pageImage); // In order to place text behind the recognised text snippets we are // interested in the bbox property Pattern bboxPattern = Pattern.compile("bbox(\\s+\\d+){4}"); // This pattern separates the coordinates of the bbox property Pattern bboxCoordinatePattern = Pattern.compile("(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)"); // Only tags of the ocr_line class are interesting StartTag ocrTag = source.getNextStartTag(prevPos, "class", OCRPAGEORLINE); while (ocrTag != null) { prevPos = ocrTag.getEnd(); if ("ocrx_word".equalsIgnoreCase(ocrTag.getAttributeValue("class"))) { net.htmlparser.jericho.Element lineElement = ocrTag.getElement(); Matcher bboxMatcher = bboxPattern.matcher(lineElement.getAttributeValue("title")); if (bboxMatcher.find()) { // We found a tag of the ocr_line class containing a bbox property Matcher bboxCoordinateMatcher = bboxCoordinatePattern.matcher(bboxMatcher.group()); bboxCoordinateMatcher.find(); int[] coordinates = { Integer.parseInt((bboxCoordinateMatcher.group(1))), Integer.parseInt((bboxCoordinateMatcher.group(2))), Integer.parseInt((bboxCoordinateMatcher.group(3))), Integer.parseInt((bboxCoordinateMatcher.group(4))) }; String line = lineElement.getContent().getTextExtractor().toString(); float bboxWidthPt = (coordinates[2] - coordinates[0]) / dotsPerPointX; float bboxHeightPt = (coordinates[3] - coordinates[1]) / dotsPerPointY; // Put the text into the PDF cb.beginText(); // Comment the next line to debug the PDF output (visible Text) cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE); // height cb.setFontAndSize(defaultFont.getBaseFont(), Math.max(Math.round(bboxHeightPt), 1)); // width cb.setHorizontalScaling(bboxWidthPt / cb.getEffectiveStringWidth(line, false)); cb.moveText((coordinates[0] / dotsPerPointX), ((pageImagePixelHeight - coordinates[3]) / dotsPerPointY)); cb.showText(line); cb.endText(); cb.setHorizontalScaling(1.0f); } } else { if ("ocr_page".equalsIgnoreCase(ocrTag.getAttributeValue("class"))) { pageCounter++; pageTag = ocrTag; break; } } ocrTag = source.getNextStartTag(prevPos, "class", OCRPAGEORLINE); } if (ocrTag == null) { pdfDocument.close(); break; } } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:Report.ItextReport.java
public static void absText(PdfWriter writer, String text, int x, int y) { try {/* w w w .j a v a2s.c om*/ PdfContentByte cb = writer.getDirectContent(); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.saveState(); cb.beginText(); cb.moveText(x, y); cb.setFontAndSize(bf, 12); cb.showText(text); cb.endText(); cb.restoreState(); } catch (DocumentException | IOException e) { e.getMessage(); } }