List of usage examples for com.lowagie.text.pdf PdfContentByte endText
public void endText()
From source file:classroom.filmfestival_c.Movies19.java
protected static void drawGrid(PdfWriter writer, List<String> places, String day, float left, float column_width, float top, float row_height) { // CANVAS/*from ww w. ja va2s . c o m*/ PdfContentByte directcontent = writer.getDirectContent(); Rectangle art = writer.getBoxSize("art"); // LINES directcontent.setLineWidth(1); float bottom = art.getTop(); // rows int rows = places.size(); for (int i = 0; i <= rows; i++) { directcontent.moveTo(art.getLeft(), art.getTop() - (i * row_height)); directcontent.lineTo(art.getRight(), art.getTop() - (i * row_height)); bottom = art.getTop() - (i * row_height); } // Rectangle directcontent.moveTo(art.getLeft(), art.getTop()); directcontent.lineTo(art.getLeft(), bottom); directcontent.moveTo(left, art.getTop()); directcontent.lineTo(left, bottom); directcontent.moveTo(art.getLeft() + (COLUMNS * column_width), art.getTop()); directcontent.lineTo(art.getLeft() + (COLUMNS * column_width), bottom); directcontent.stroke(); // columns directcontent.saveState(); directcontent.setLineWidth(0.3f); directcontent.setColorStroke(SILVER); directcontent.setLineDash(3, 1); for (int i = 2; i < COLUMNS; i++) { directcontent.moveTo(art.getLeft() + (i * column_width), art.getTop()); directcontent.lineTo(art.getLeft() + (i * column_width), bottom); } directcontent.stroke(); directcontent.restoreState(); // TEXT // date directcontent.beginText(); directcontent.setFontAndSize(FONT, 12); directcontent.showTextAligned(Element.ALIGN_RIGHT, day, art.getLeft() - 5, art.getTop(), 90); directcontent.endText(); // time for (int i = 1; i < COLUMNS; i++) { directcontent.beginText(); directcontent.setFontAndSize(FONT, 8); directcontent.showTextAligned(Element.ALIGN_LEFT, TIME[i - 1], art.getLeft() + (i * column_width) + 5, top + 5, 90); directcontent.endText(); } // places for (int i = 0; i < rows; i++) { directcontent.beginText(); directcontent.setFontAndSize(FONT, 12); directcontent.showTextAligned(Element.ALIGN_CENTER, places.get(i), art.getLeft() + 16, art.getTop() - ((i + 0.5f) * row_height), 90); directcontent.endText(); } }
From source file:classroom.filmfestival_c.Movies19.java
protected static void drawMovie(PdfWriter writer, FestivalScreening screening, List<String> places, float minute, float left, float top, float row_height) throws DocumentException { FilmTitle movie = screening.getFilmTitle(); // defining the rectangle int place = places.indexOf(screening.getId().getPlace()); float llx, urx, lly, ury; llx = left + minute * getMinutesAfter930(screening); urx = llx + minute * (screening.getShortfilm() + movie.getDuration()); ury = top - place * row_height;//from ww w .j a v a2 s . c o m lly = top - ((place + 1) * row_height); // FOREGROUND PdfContentByte foreground = writer.getDirectContent(); // rectangle foreground.setColorStroke(BLACK); foreground.setLineWidth(1); foreground.moveTo(urx, lly); foreground.lineTo(llx, lly); foreground.lineTo(llx, ury); foreground.lineTo(urx, ury); foreground.closePathStroke(); // title ColumnText ct = new ColumnText(foreground); ct.setSimpleColumn(new Phrase(movie.getTitle(), SMALLEST), llx, lly, urx, ury, 14, Element.ALIGN_CENTER); ct.go(); // BACKGROUND PdfContentByte background = writer.getDirectContentUnder(); // draw background background.setColorFill(getColor(movie)); background.moveTo(urx, lly); background.lineTo(llx, lly); background.lineTo(llx, ury); background.lineTo(urx, ury); background.fill(); // draw a white P for press screenings if (screening.getPress() == 1) { background.saveState(); background.beginText(); background.setFontAndSize(FONT, 24); background.setColorFill(WHITE); background.showTextAligned(Element.ALIGN_CENTER, "P", (llx + urx) / 2f, (lly + ury) / 2f - 12, 0); background.endText(); background.restoreState(); } // draw shortfilm background.setColorStroke(WHITE); if (screening.getShortfilm() > 0) { background.moveTo(llx + (minute * screening.getShortfilm()), lly); background.lineTo(llx + (minute * screening.getShortfilm()), ury); background.stroke(); } }
From source file:classroom.filmfestival_c.Movies22.java
public static void main(String[] args) { // step 1/*from w w w .j a v a 2 s . c om*/ 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/*from ww w . j a va 2 s . com*/ 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.HelloWorld06.java
public static void main(String[] args) { // step 1/*w w w .j ava 2s . c om*/ 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.setTextMatrix(36, 788); cb.setFontAndSize(bf, 12); cb.showText("Hello World"); cb.endText(); } 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.HelloWorld07.java
public static void main(String[] args) { // step 1//from w w w . ja v a2s. c o 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.beginText(); cb.setFontAndSize(bf, 12); cb.showTextAligned(Element.ALIGN_LEFT, "Hello World", 36, 788, 0); cb.endText(); } 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;/* w w w . j av a 2 s .c om*/ // 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;//from w w w . ja va 2 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:classroom.newspaper_a.Newspaper03.java
public static void putText(PdfContentByte canvas, String p, BaseFont bf, float llx, float lly, float urx, float ury) throws DocumentException { float x = (llx + urx) / 2f + 5; float y = (lly + ury) / 2f - 5; float w = urx - llx; float h = ury - lly; float angle = (float) (180 * Math.atan(h / w) / Math.PI); canvas.beginText();//from w w w . j a v a 2 s .com canvas.setFontAndSize(bf, 10); canvas.showTextAligned(Element.ALIGN_CENTER, p, x, y, angle); canvas.endText(); }
From source file:com.actelion.research.spiritapp.print.CagePrinterPDF.java
License:Open Source License
public static void printCages(List<Container> cages, boolean printGroupsTreatments, boolean printTreatmentDesc, boolean whiteBackground) throws Exception { final int margin = 15; File f = File.createTempFile("cages__", ".pdf"); Document doc = new Document(PageSize.A4.rotate()); Image maleImg, femaleImg, nosexImg; try {// w w w .j ava2 s. c om maleImg = Image.getInstance(CagePrinterPDF.class.getResource("male.png")); femaleImg = Image.getInstance(CagePrinterPDF.class.getResource("female.png")); nosexImg = Image.getInstance(CagePrinterPDF.class.getResource("nosex.png")); } catch (Exception e) { throw new Exception("Could not find images in the classpath: " + e); } FileOutputStream os = new FileOutputStream(f); PdfWriter writer = PdfWriter.getInstance(doc, os); doc.open(); PdfContentByte canvas = writer.getDirectContentUnder(); float tileW = (doc.getPageSize().getWidth()) / 4; float tileH = (doc.getPageSize().getHeight()) / 2; for (int i = 0; i < cages.size(); i++) { Container cage = cages.get(i); Study study = cage.getStudy(); Set<Group> groups = cage.getGroups(); Group group = cage.getGroup(); Set<Biosample> animals = new TreeSet<>(Biosample.COMPARATOR_NAME); animals.addAll(cage.getBiosamples()); //Find the treatments applied to this group Set<NamedTreatment> allTreatments = new LinkedHashSet<>(); for (Group gr : groups) { allTreatments.addAll(gr.getAllTreatments(-1)); } //Draw if (i % 8 == 0) { if (i > 0) doc.newPage(); drawCageSeparation(doc, canvas); } int col = (i % 8) % 4; int row = (i % 8) / 4; float x = margin + tileW * col; float x2 = x + tileW - margin; float baseY = tileH * row; float y; //Display Sex canvas.moveTo(tileW * col - 50, doc.getPageSize().getHeight() - (tileH * row + 50)); Image img = null; String sex = getMetadata(animals, "Sex"); if (study.isBlindAll()) { img = nosexImg; } else if (sex.equals("M")) { img = maleImg; } else if (sex.equals("F")) { img = femaleImg; } else if (sex.length() > 0) { img = nosexImg; } if (img != null) { img.scaleToFit(20, 20); img.setAbsolutePosition(tileW * (col + 1) - 33, doc.getPageSize().getHeight() - (tileH * row + 12 + margin)); doc.add(img); } if (group != null && group.getColor() != null && (study != null && !study.isBlind()) && !whiteBackground) { Color c = group.getColor(); canvas.saveState(); canvas.setRGBColorFill(c.getRed() / 3 + 170, c.getGreen() / 3 + 170, c.getBlue() / 3 + 170); canvas.rectangle(x - margin + 1, doc.getPageSize().getHeight() - baseY - tileH + 1, tileW - 2, tileH - 2); canvas.fill(); canvas.restoreState(); } Color treatmentColor = Color.BLACK; if (allTreatments.size() > 0 && !study.isBlind() && printGroupsTreatments) { int offset = 0; for (NamedTreatment t : allTreatments) { if (t.getColor() != null) { if (allTreatments.size() == 1) treatmentColor = new Color(t.getColor().getRed() / 2, t.getColor().getGreen() / 2, t.getColor().getBlue() / 2); canvas.saveState(); canvas.setColorStroke(Color.BLACK); canvas.setRGBColorFill(t.getColor().getRed(), t.getColor().getGreen(), t.getColor().getBlue()); y = baseY + 42 + 15 + 86 + 13 + 22 + 14 + 23 + 28; canvas.rectangle(x + 20 + offset * 5, doc.getPageSize().getHeight() - y - (offset % 2) * 4, 22, 22); canvas.fillStroke(); canvas.restoreState(); offset++; } } } canvas.beginText(); canvas.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA_BOLD, "Cp1252", false), 16f); canvas.showTextAligned(Element.ALIGN_LEFT, cage.getContainerId(), x, doc.getPageSize().getHeight() - (baseY + 23), 0); canvas.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA_OBLIQUE, "Cp1252", false), 11f); y = 42 + baseY; canvas.showTextAligned(Element.ALIGN_LEFT, "Type: ", x, doc.getPageSize().getHeight() - y, 0); y += 15; canvas.showTextAligned(Element.ALIGN_LEFT, "Animals_ID: ", x, doc.getPageSize().getHeight() - y, 0); y += 86; canvas.showTextAligned(Element.ALIGN_LEFT, "Delivery date: ", x, doc.getPageSize().getHeight() - y, 0); y += 13; canvas.showTextAligned(Element.ALIGN_LEFT, "PO Number: ", x, doc.getPageSize().getHeight() - y, 0); y += 22; canvas.showTextAligned(Element.ALIGN_LEFT, "Study: ", x, doc.getPageSize().getHeight() - y, 0); y += 14; if (study != null && !study.isBlind() && printGroupsTreatments) canvas.showTextAligned(Element.ALIGN_LEFT, "Group: ", x, doc.getPageSize().getHeight() - y, 0); y += 23; if (study != null && !study.isBlind() && printGroupsTreatments) canvas.showTextAligned(Element.ALIGN_LEFT, "Treatment: ", x, doc.getPageSize().getHeight() - y, 0); y += 50; canvas.showTextAligned(Element.ALIGN_LEFT, "License: ", x, doc.getPageSize().getHeight() - y, 0); y += 13; canvas.showTextAligned(Element.ALIGN_LEFT, "Experimenter: ", x, doc.getPageSize().getHeight() - y, 0); canvas.endText(); y = 42 + baseY; print(canvas, study.isBlindAll() ? "Blinded" : getMetadata(animals, "Type"), x + 65, doc.getPageSize().getHeight() - y, x2, 11, 11, FontFactory.HELVETICA, Color.BLACK, 11f); y += 15; if (animals.size() <= 6) { int n = 0; for (Biosample animal : animals) { print(canvas, animal.getSampleId(), x + 75, doc.getPageSize().getHeight() - y - 12 * n, x2 - 50, 12, 12, FontFactory.HELVETICA, Color.BLACK, 11f); if (animal.getSampleName() != null && animal.getSampleName().length() > 0) { print(canvas, "[ " + animal.getSampleName() + " ]", x2 - 60, doc.getPageSize().getHeight() - y - 12 * n, x2, 12, 12, FontFactory.HELVETICA_BOLD, Color.BLACK, 11f); } n++; } } else { int nPerRow = animals.size() / 2; int n = 0; for (Biosample animal : animals) { int nx = n / nPerRow; int ny = n % nPerRow; print(canvas, animal.getSampleId(), x + nx * (x2 - x) / 2, doc.getPageSize().getHeight() - y - 12 * ny - 12, x + (1 + nx) * (x2 - x) / 2, 12, 12, FontFactory.HELVETICA, Color.BLACK, 10f); if (animal.getSampleName() != null && animal.getSampleName().length() > 0) { print(canvas, "[ " + animal.getSampleName() + " ]", x + (1 + nx) * (x2 - x) / 2 - 35, doc.getPageSize().getHeight() - y - 12 * ny - 12, x + (1 + nx) * (x2 - x) / 2, 12, 12, FontFactory.HELVETICA_BOLD, Color.BLACK, 10f); } n++; } } y += 86; print(canvas, getMetadata(animals, "Delivery Date"), x + 75, doc.getPageSize().getHeight() - y, x2, 0, 10, FontFactory.HELVETICA, Color.BLACK, 10f); y += 13; print(canvas, getMetadata(animals, "PO Number"), x + 75, doc.getPageSize().getHeight() - y, x2, 0, 10, FontFactory.HELVETICA, Color.BLACK, 10f); y += 22; print(canvas, study.getStudyIdAndInternalId(), x + 40, doc.getPageSize().getHeight() - y, x2, 0, 11, BaseFont.HELVETICA_BOLD, Color.BLACK, 11f); y += 14; if (!study.isBlind() && printGroupsTreatments) print(canvas, getGroups(animals), x + 40, doc.getPageSize().getHeight() - y, x2, 22, 11, BaseFont.HELVETICA_BOLD, Color.BLACK, 11f); y += 23; if (!study.isBlind() && printGroupsTreatments) print(canvas, getTreatments(animals, printTreatmentDesc), x + 62, doc.getPageSize().getHeight() - y, x2, 50, printTreatmentDesc ? 9 : 12, FontFactory.HELVETICA, treatmentColor, printTreatmentDesc ? 9f : 10f); y += 50; print(canvas, study.getMetadataMap().get("LICENSENO"), x + 74, doc.getPageSize().getHeight() - y, x2, 15, 10, FontFactory.HELVETICA, Color.BLACK, 10f); y += 13; print(canvas, study.getMetadataMap().get("EXPERIMENTER"), x + 74, doc.getPageSize().getHeight() - y, x2, 20, 10, FontFactory.HELVETICA, Color.BLACK, 10f); } doc.close(); os.close(); Desktop.getDesktop().open(f); try { Thread.sleep(500); } catch (Exception e) { } }