List of usage examples for com.lowagie.text.pdf PdfContentByte moveText
public void moveText(float x, float y)
From source file:br.org.archimedes.io.pdf.elements.TextExporter.java
License:Open Source License
public void exportElement(Text text, Object outputObject) throws IOException { PDFWriterHelper helper = (PDFWriterHelper) outputObject; PdfContentByte cb = helper.getPdfContentByte(); Point lowerLeft = text.getLowerLeft(); Point docPoint = helper.modelToDocument(lowerLeft); BaseFont font = null;// ww w . j a v a2 s .c o m try { font = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); } catch (DocumentException e) { // Problems creating the font. This means the current // platform does not support this encoding or font. System.err.println(Messages.TextExporter_FontCreatingError); e.printStackTrace(); } cb.setFontAndSize(font, (float) text.getSize()); cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL); cb.beginText(); cb.moveText((float) docPoint.getX(), (float) docPoint.getY()); double angle = 0; try { angle = Geometrics.calculateAngle(new Point(0, 0), text.getDirection().getPoint()); } catch (NullArgumentException e) { // Shouldn't happen since the text MUST have a direction to exists // and the point 0,0 is valid e.printStackTrace(); } float degreeAngle = (float) (angle * 180 / Math.PI); cb.showTextAligned(PdfContentByte.ALIGN_LEFT, text.getText(), (float) docPoint.getX(), (float) docPoint.getY(), degreeAngle); cb.endText(); }
From source file:classroom.filmfestival_c.Movies22.java
public static void main(String[] args) { // step 1/*from w ww.j av a 2s . com*/ Document document = new Document(new Rectangle(WIDTH, HEIGHT)); try { // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(FORM)); writer.setViewerPreferences(PdfWriter.PageLayoutSinglePage); // step 3 document.open(); // step 4 PdfContentByte cb = writer.getDirectContent(); // top part of the card cb.setColorFill(RED); cb.rectangle(0, mm2pt(57f), WIDTH, HEIGHT - mm2pt(57f)); cb.fill(); cb.setColorFill(BLACK); cb.rectangle(0, mm2pt(61), WIDTH, mm2pt(21f)); cb.fill(); cb.setColorFill(WHITE); Image kubrick = Image.getInstance(KUBRICK); kubrick.scaleToFit(WIDTH, mm2pt(21)); kubrick.setAbsolutePosition(0, mm2pt(61)); cb.addImage(kubrick); Image logo = Image.getInstance(LOGO); logo.scaleToFit(mm2pt(14), mm2pt(14)); logo.setAbsolutePosition(mm2pt(37), mm2pt(65)); cb.addImage(logo); cb.beginText(); cb.setFontAndSize(FONT, 7); cb.showTextAligned(Element.ALIGN_CENTER, "Flanders International Film Festival-Ghent", WIDTH / 2, mm2pt(58.5f), 0); cb.endText(); // bottom part of the card TextField filmfestival = new TextField(writer, new Rectangle(0, 0, WIDTH, mm2pt(9)), "filmfestival"); filmfestival.setBackgroundColor(GRAY); filmfestival.setTextColor(WHITE); filmfestival.setText("www.filmfestival.be"); filmfestival.setFontSize(14); filmfestival.setOptions(TextField.READ_ONLY); filmfestival.setAlignment(Element.ALIGN_CENTER); writer.addAnnotation(filmfestival.getTextField()); cb.beginText(); cb.moveText(mm2pt(1.5f), mm2pt(12)); cb.setFontAndSize(FONT, 21); cb.setColorFill(RED); cb.showText("10"); cb.setColorFill(BLUE); cb.showText("/"); cb.setColorFill(RED); cb.showText("21"); cb.setColorFill(BLUE); cb.showText("OCT"); cb.setColorFill(GREEN); cb.showText("2006"); cb.endText(); cb.setColorStroke(BLUE); cb.moveTo(mm2pt(24.5f), mm2pt(29)); cb.lineTo(mm2pt(24.5f), mm2pt(36)); cb.moveTo(mm2pt(24.5f), mm2pt(48)); cb.lineTo(mm2pt(24.5f), mm2pt(54)); cb.stroke(); // central part of the card PushbuttonField photo = new PushbuttonField(writer, new Rectangle(mm2pt(3), mm2pt(29), mm2pt(24), mm2pt(54)), "photo"); PdfTemplate t1 = cb.createTemplate(mm2pt(21), mm2pt(25)); t1.setColorFill(GRAY); t1.rectangle(0, 0, mm2pt(21), mm2pt(25)); t1.fill(); photo.setTemplate(t1); photo.setLayout(PushbuttonField.LAYOUT_ICON_ONLY); writer.addAnnotation(photo.getField()); TextField type = new TextField(writer, new Rectangle(mm2pt(26), mm2pt(46), WIDTH - mm2pt(1.5f), mm2pt(54)), "type"); type.setTextColor(GRAY); type.setText("TYPE"); type.setFontSize(0); writer.addAnnotation(type.getTextField()); TextField number = new TextField(writer, new Rectangle(mm2pt(26), mm2pt(44), WIDTH - mm2pt(1.5f), mm2pt(48)), "number"); number.setText("N\u00b0 0000000"); number.setFontSize(8); writer.addAnnotation(number.getTextField()); TextField name = new TextField(writer, new Rectangle(mm2pt(26), mm2pt(28), WIDTH - mm2pt(1.5f), mm2pt(40)), "name"); name.setText("Name"); name.setFontSize(8); name.setOptions(TextField.MULTILINE); writer.addAnnotation(name.getTextField()); PushbuttonField barcode = new PushbuttonField(writer, new Rectangle(mm2pt(3), mm2pt(23), WIDTH - mm2pt(3), mm2pt(28)), "barcode"); PdfTemplate t2 = cb.createTemplate(WIDTH - mm2pt(6), mm2pt(5)); t2.setColorFill(GRAY); t2.rectangle(0, 0, WIDTH - mm2pt(6), mm2pt(5)); t2.fill(); barcode.setTemplate(t2); barcode.setLayout(PushbuttonField.LAYOUT_ICON_ONLY); writer.addAnnotation(barcode.getField()); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // step 4 document.close(); }
From source file:classroom.intro.HelloWorld05.java
public static void main(String[] args) { // step 1// w ww.ja v a 2s . co m Document document = new Document(); try { // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); // step 3 document.open(); // step 4 PdfContentByte cb = writer.getDirectContent(); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.saveState(); // q cb.beginText(); // BT cb.moveText(36, 806); // 36 806 Td cb.moveText(0, -18); // 0 -18 Td cb.setFontAndSize(bf, 12); // /F1 12 Tf cb.showText("Hello World"); // (Hello World)Tj cb.endText(); // ET cb.restoreState(); // Q } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } // step 5 document.close(); }
From source file:classroom.intro.HelloWorld08.java
public static void main(String[] args) { Document.compress = false;//from ww w . ja v a2s .co m // step 1 Document document = new Document(); try { // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); // step 3 document.open(); // step 4 PdfContentByte cb = writer.getDirectContent(); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.setFontAndSize(bf, 12); cb.moveText(88.66f, 788); cb.showText("ld"); cb.moveText(-22f, 0); cb.showText("Wor"); cb.moveText(-15.33f, 0); cb.showText("llo"); cb.endText(); PdfTemplate tmp = cb.createTemplate(250, 25); tmp.beginText(); tmp.setFontAndSize(bf, 12); tmp.moveText(0, 7); tmp.showText("He"); tmp.endText(); cb.addTemplate(tmp, 36, 781); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } // step 5 document.close(); }
From source file:classroom.intro.HelloWorld09.java
public static void main(String[] args) { Document.compress = false;/* w w w . j a v a2 s. c om*/ BaseFont bf = null; // step 1 Document document = new Document(); try { // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT1)); // step 3 document.open(); // step 4 PdfContentByte cb = writer.getDirectContent(); bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.setFontAndSize(bf, 12); cb.moveText(88.66f, 788); cb.showText("ld"); cb.moveText(-22f, 0); cb.showText("Wor"); cb.endText(); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } // step 5 document.close(); try { PdfReader reader = new PdfReader(RESULT1); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT2)); PdfContentByte cb1 = stamper.getUnderContent(1); cb1.beginText(); cb1.setFontAndSize(bf, 12); cb1.setTextMatrix(51.33f, 788); cb1.showText("llo"); cb1.endText(); PdfContentByte cb2 = stamper.getOverContent(1); PdfTemplate tmp = cb2.createTemplate(250, 25); tmp.beginText(); tmp.setFontAndSize(bf, 12); tmp.moveText(0, 7); tmp.showText("He"); tmp.endText(); cb2.addTemplate(tmp, 36, 781); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:de.cuseb.bilderbuch.pdf.PdfController.java
License:Open Source License
@RequestMapping(value = "/pdf", method = RequestMethod.GET) public void generatePdf(HttpSession session, HttpServletResponse httpServletResponse) { try {// ww w .j a v a2s .c o m PdfRequest pdfRequest = (PdfRequest) session.getAttribute("pdfRequest"); httpServletResponse.setContentType("application/pdf"); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, httpServletResponse.getOutputStream()); writer.setDefaultColorspace(PdfName.COLORSPACE, PdfName.DEFAULTRGB); //document.addAuthor(pdfRequest.getAuthor()); //document.addTitle(pdfRequest.getTitle()); document.setPageSize( new Rectangle(Utilities.millimetersToPoints(156), Utilities.millimetersToPoints(148))); document.open(); FontFactory.defaultEmbedding = true; FontFactory.register("IndieRock.ttf", "IndieRock"); Font font = FontFactory.getFont("IndieRock"); BaseFont baseFont = font.getBaseFont(); PdfContentByte cb = writer.getDirectContent(); Iterator<PdfPage> pages = pdfRequest.getPages().iterator(); while (pages.hasNext()) { PdfPage page = pages.next(); if (page.getImage() != null) { Image image = Image.getInstance(new URL(page.getImage().getUrl())); image.setDpi(300, 300); image.setAbsolutePosition(0f, 0f); image.scaleAbsolute(document.getPageSize().getWidth(), document.getPageSize().getHeight()); document.add(image); cb.saveState(); cb.beginText(); cb.setColorFill(Color.WHITE); cb.moveText(10f, 10f); cb.setFontAndSize(baseFont, 18); cb.showText(page.getSentence()); cb.endText(); cb.restoreState(); if (pages.hasNext()) { document.newPage(); } } } document.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:mx.randalf.digital.ocr.hocrtopdf.HocrToPdf.java
License:Open Source License
public void hocrToPdf(File fImg, File fHtml, File fPdf) throws IOException, DocumentException, Exception { URL inputHOCRFile = null;//from www. j a v a 2s. c o m FileOutputStream outputPDFStream = null; // The resolution of a PDF file (using iText) is 72pt per inch float pointsPerInch = 72.0f; Source source = null; StartTag pageTag = null; Pattern imagePattern = null; Matcher imageMatcher = null; // Load the image Image pageImage = null; float dotsPerPointX; float dotsPerPointY; float pageImagePixelHeight; Document pdfDocument = null; PdfWriter pdfWriter = null; Font defaultFont = null; PdfContentByte cb = null; Pattern bboxPattern = null; Pattern bboxCoordinatePattern = null; StartTag ocrLineTag = null; try { try { inputHOCRFile = new URL("file://" + fHtml.getAbsolutePath()); } catch (MalformedURLException e) { throw e; } try { outputPDFStream = new FileOutputStream(fPdf); } catch (FileNotFoundException e) { throw e; } // Using the jericho library to parse the HTML file source = new Source(inputHOCRFile); // Find the tag of class ocr_page in order to load the scanned image pageTag = source.findNextStartTag(0, "class", "ocr_page", false); imagePattern = Pattern.compile("image\\s+([^;]+)"); imageMatcher = imagePattern.matcher(pageTag.getElement().getAttributeValue("title")); if (!imageMatcher.find()) { throw new Exception("Could not find a tag of class \"ocr_page\", aborting."); } try { pageImage = Image.getInstance(new URL("file://" + fImg.getAbsolutePath())); } catch (MalformedURLException e) { throw e; } dotsPerPointX = pageImage.getDpiX() / pointsPerInch; dotsPerPointY = pageImage.getDpiY() / pointsPerInch; pageImagePixelHeight = pageImage.getHeight(); pdfDocument = new Document( new Rectangle(pageImage.getWidth() / dotsPerPointX, pageImage.getHeight() / dotsPerPointY)); pdfWriter = PdfWriter.getInstance(pdfDocument, outputPDFStream); pdfDocument.open(); // first define a standard font for our text defaultFont = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.BOLD, CMYKColor.BLACK); // Put the text behind the picture (reverse for debugging) cb = pdfWriter.getDirectContentUnder(); //PdfContentByte cb = pdfWriter.getDirectContent(); 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 bboxPattern = Pattern.compile("bbox(\\s+\\d+){4}"); // This pattern separates the coordinates of the bbox property bboxCoordinatePattern = Pattern.compile("(\\d+)\\s+(\\d+)\\s+(\\d+)\\s+(\\d+)"); // Only tags of the ocr_line class are interesting ocrLineTag = source.findNextStartTag(0, "class", "ocr_line", false); while (ocrLineTag != null) { au.id.jericho.lib.html.Element lineElement = ocrLineTag.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().extractText(); // 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); // TODO: Scale the text width to fit the OCR bbox cb.setFontAndSize(defaultFont.getBaseFont(), Math.round(bboxHeightPt)); cb.moveText((float) (coordinates[0] / dotsPerPointX), (float) ((pageImagePixelHeight - coordinates[3]) / dotsPerPointY)); cb.showText(line); cb.endText(); } ocrLineTag = source.findNextStartTag(ocrLineTag.getEnd(), "class", "ocr_line", false); } } catch (NumberFormatException e) { throw e; } catch (MalformedURLException e) { throw e; } catch (FileNotFoundException e) { throw e; } catch (BadElementException e) { throw e; } catch (IOException e) { throw e; } catch (DocumentException e) { throw e; } catch (Exception e) { throw e; } finally { if (pdfDocument != null) { pdfDocument.close(); } if (outputPDFStream != null) { outputPDFStream.close(); } } }
From source file:uk.org.rbc1b.roms.controller.volunteer.VolunteerBadgePdfView.java
License:Open Source License
/** * Adds the Volunteer's name to the Badge. * * @param content to be added/*from w ww.j a v a2 s . com*/ * @param name concatenated String of forename and surname */ private static void addVolunteerName(PdfContentByte content, String name) throws DocumentException, IOException { content.beginText(); content.moveText(183, 470); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1257, false); content.setFontAndSize(bf, 16); content.setColorFill(Color.BLACK); content.showText(name); content.endText(); }
From source file:uk.org.rbc1b.roms.controller.volunteer.VolunteerBadgePdfView.java
License:Open Source License
/** * Adds the RBC Region's title (e.g. London and the Home Counties) to the * bottom of the badge.//from www.j a v a 2 s . co m * * @param content to be added * @param rbcRegion RBC region */ private static void addRBCRegionFooter(PdfContentByte content) throws DocumentException, IOException { content.beginText(); content.moveText(183, 345); BaseFont bf = BaseFont.createFont(); content.setFontAndSize(bf, 9); content.setColorFill(Color.DARK_GRAY); content.showText(VolunteerBadgePdfView.BADGE_RBC_REGION); content.endText(); }
From source file:uk.org.rbc1b.roms.controller.volunteer.VolunteerBadgePdfView.java
License:Open Source License
/** * Adds a department to the Badge. The badge only shows the first * departmental assignment of a Volunteer, not all their departmental * assignments./*from w w w . ja v a 2 s . c o m*/ * * @param content to be added * @param department of the volunteer */ private static void addDepartment(PdfContentByte content, String department) throws DocumentException, IOException { content.beginText(); content.moveText(183, 453); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1257, false); content.setFontAndSize(bf, 14); content.showText(department); content.endText(); }