List of usage examples for org.apache.pdfbox.pdmodel PDPageContentStream drawString
@Deprecated public void drawString(String text) throws IOException
From source file:Bulletin.Bulletin2.java
private float Text(PDPageContentStream cos, String text, PDFont font, int size, float left, float Y) { try {/*w w w . j a v a 2s . c o m*/ for (String line : text.split("\n")) { cos.beginText(); cos.setFont(fontPlain, Math.round(textScale * size)); cos.moveTextPositionByAmount(left, Y); cos.drawString(line); Y -= this.rowHeight; // System.out.println("line = " + line); cos.endText(); cos.stroke(); } } catch (IOException ex) { Logger.getLogger(Bulletin2.class.getName()).log(Level.SEVERE, null, ex); } return this.rowHeight; }
From source file:ch.eggbacon.app.pdf.PDFCreator.java
License:Open Source License
public void drawHeader(PDPageContentStream contentStream) throws IOException { PDImageXObject img = PDImageXObject.createFromFile("web/img/logo.png", doc); contentStream.drawImage(img, PDFConstants.MARGIN, PDFConstants.PAPER_HEIGHT - 40, 47, 30); contentStream.setFont(PDType1Font.HELVETICA_BOLD, 18); contentStream.beginText();//w w w .j ava2 s. c o m contentStream.moveTextPositionByAmount(PDFConstants.MARGIN + 50, PDFConstants.PAPER_HEIGHT - 30); contentStream.drawString("Egg & Bacon Hotel"); contentStream.endText(); }
From source file:ch.eggbacon.app.pdf.PDFCreator.java
License:Open Source License
public void drawFooter(int pageNr, PDPageContentStream contentStream) throws IOException { contentStream.setFont(PDType1Font.HELVETICA, 12); contentStream.beginText();/*w ww . ja v a 2s .c o m*/ contentStream.moveTextPositionByAmount(PDFConstants.MARGIN, 20); contentStream.drawString("Page " + pageNr); contentStream.endText(); }
From source file:ch.eggbacon.app.pdf.PDFCreator.java
License:Open Source License
public void drawTitlePage(String title, Person[] addr, String[] strs, PDPageContentStream contentStream) throws IOException { float ypos = PDFConstants.PAPER_HEIGHT - PDFConstants.MARGIN; contentStream.beginText();// www . j av a2 s . c o m // Title contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12); contentStream.moveTextPositionByAmount(PDFConstants.MARGIN, ypos); contentStream.drawString(title); contentStream.endText(); ypos -= 20; // Adressen contentStream.setFont(PDType1Font.HELVETICA, 12); for (Person p : addr) { contentStream.beginText(); contentStream.moveTextPositionByAmount(PDFConstants.MARGIN, ypos -= 13); contentStream.drawString(p.getAnrede()); contentStream.endText(); contentStream.beginText(); contentStream.moveTextPositionByAmount(PDFConstants.MARGIN, ypos -= 13); contentStream.drawString(p.getVorname() + " " + p.getNachname()); contentStream.endText(); contentStream.beginText(); contentStream.moveTextPositionByAmount(PDFConstants.MARGIN, ypos -= 13); contentStream.drawString(p.getStrasse()); contentStream.endText(); contentStream.beginText(); contentStream.moveTextPositionByAmount(PDFConstants.MARGIN, ypos -= 13); contentStream.drawString(p.getOrt()); contentStream.endText(); ypos -= 25; } }
From source file:ch.eggbacon.app.pdf.PDFCreator.java
License:Open Source License
public void drawTablePage(PDPageContentStream contentStream, String[][] content) throws IOException { final float y = PDFConstants.PAPER_HEIGHT - PDFConstants.MARGIN; final int margin = PDFConstants.MARGIN; final int rows = content.length; final int cols = content[0].length; final float rowHeight = 20f; final float tableWidth = PDFConstants.PAPER_WIDTH - (2 * margin); final float tableHeight = rowHeight * rows; final float colWidth = tableWidth / (float) cols; final float cellMargin = 5f; //draw the rows float nexty = y; for (int i = 0; i <= rows; i++) { contentStream.drawLine(margin, nexty, margin + tableWidth, nexty); nexty -= rowHeight;/* www. j a v a2s . c o m*/ } //draw the columns float nextx = margin; for (int i = 0; i <= cols; i++) { contentStream.drawLine(nextx, y, nextx, y - tableHeight); nextx += colWidth; } //now add the text contentStream.setFont(PDType1Font.HELVETICA, 12); float textx = margin + cellMargin; float texty = y - 15; for (int i = 0; i < content.length; i++) { for (int j = 0; j < content[i].length; j++) { String text = content[i][j]; contentStream.beginText(); contentStream.moveTextPositionByAmount(textx, texty); contentStream.drawString(text); contentStream.endText(); textx += colWidth; } texty -= rowHeight; textx = margin + cellMargin; } }
From source file:com.foc.vaadin.gui.mswordGenerator.FocMSWordFontProperties.java
License:Apache License
public int getFontHeight(PDPageContentStream contentStream, PDType1Font font, int fontSize, String content) throws IOException { int start = 0; int end = 0;//from w w w .j a va2 s .c om int stringHeight = 10; if (content != null && !content.isEmpty()) { for (int i : possibleWrapPoints(content)) { float width = font.getStringWidth(content.substring(start, i)) / 1000 * fontSize; if (start < end/* && width > paragraphWidth*/) { contentStream.moveTextPositionByAmount(10, stringHeight); contentStream.drawString(toString().trim().substring(start, end)); stringHeight += font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize; start = end; } end = i; } } return stringHeight; }
From source file:com.foc.vaadin.gui.pdfGenerator.FocPDFLabel.java
License:Apache License
public void write() { try {/*from w ww .ja v a 2 s . co m*/ PDPageContentStream contentStream = getPDPageContentStream(); if (value != null) { // contentStream.moveTo(0, 0); contentStream.beginText(); PDType1Font font = PDType1Font.HELVETICA; int fontSize = 10; contentStream.setFont(font, fontSize); // contentStream.moveTextPositionByAmount(0, getHeight()); contentStream.moveTextPositionByAmount(getAbsoluteLeft(), getAbsoluteTop() - getHeight()); contentStream.drawString(value); contentStream.endText(); } } catch (Exception ex) { Globals.logException(ex); } }
From source file:org.dspace.disseminate.CitationDocumentServiceImpl.java
License:BSD License
protected void generateCoverPage(Context context, PDDocument document, PDPage coverPage, Item item) throws IOException { PDPageContentStream contentStream = new PDPageContentStream(document, coverPage); try {/*from w w w . j a v a2 s. com*/ int ypos = 760; int xpos = 30; int xwidth = 550; int ygap = 20; PDFont fontHelvetica = PDType1Font.HELVETICA; PDFont fontHelveticaBold = PDType1Font.HELVETICA_BOLD; PDFont fontHelveticaOblique = PDType1Font.HELVETICA_OBLIQUE; contentStream.setNonStrokingColor(Color.BLACK); String[][] content = { header1 }; drawTable(coverPage, contentStream, ypos, xpos, content, fontHelveticaBold, 11, false); ypos -= (ygap); String[][] content2 = { header2 }; drawTable(coverPage, contentStream, ypos, xpos, content2, fontHelveticaBold, 11, false); ypos -= ygap; contentStream.fillRect(xpos, ypos, xwidth, 1); contentStream.closeAndStroke(); String[][] content3 = { { getOwningCommunity(context, item), getOwningCollection(item) } }; drawTable(coverPage, contentStream, ypos, xpos, content3, fontHelvetica, 9, false); ypos -= ygap; contentStream.fillRect(xpos, ypos, xwidth, 1); contentStream.closeAndStroke(); ypos -= (ygap * 2); for (String field : fields) { field = field.trim(); PDFont font = fontHelvetica; int fontSize = 11; if (field.contains("title")) { fontSize = 26; ypos -= ygap; } else if (field.contains("creator") || field.contains("contributor")) { fontSize = 16; } if (field.equals("_line_")) { contentStream.fillRect(xpos, ypos, xwidth, 1); contentStream.closeAndStroke(); ypos -= (ygap); } else if (StringUtils.isNotEmpty(itemService.getMetadata(item, field))) { ypos = drawStringWordWrap(coverPage, contentStream, itemService.getMetadata(item, field), xpos, ypos, font, fontSize); } if (field.contains("title")) { ypos -= ygap; } } contentStream.beginText(); contentStream.setFont(fontHelveticaOblique, 11); contentStream.moveTextPositionByAmount(xpos, ypos); contentStream.drawString(footer); contentStream.endText(); } finally { contentStream.close(); } }
From source file:org.dspace.disseminate.CitationDocumentServiceImpl.java
License:BSD License
@Override public int drawStringWordWrap(PDPage page, PDPageContentStream contentStream, String text, int startX, int startY, PDFont pdfFont, float fontSize) throws IOException { float leading = 1.5f * fontSize; PDRectangle mediabox = page.getMediaBox(); float margin = 72; float width = mediabox.getWidth() - 2 * margin; List<String> lines = new ArrayList<>(); int lastSpace = -1; while (text.length() > 0) { int spaceIndex = text.indexOf(' ', lastSpace + 1); if (spaceIndex < 0) { lines.add(text);/*from w w w .j a va 2 s . co m*/ text = ""; } else { String subString = text.substring(0, spaceIndex); float size = fontSize * pdfFont.getStringWidth(subString) / 1000; if (size > width) { if (lastSpace < 0) // So we have a word longer than the line... draw it anyways lastSpace = spaceIndex; subString = text.substring(0, lastSpace); lines.add(subString); text = text.substring(lastSpace).trim(); lastSpace = -1; } else { lastSpace = spaceIndex; } } } contentStream.beginText(); contentStream.setFont(pdfFont, fontSize); contentStream.moveTextPositionByAmount(startX, startY); int currentY = startY; for (String line : lines) { contentStream.drawString(line); currentY -= leading; contentStream.moveTextPositionByAmount(0, -leading); } contentStream.endText(); return currentY; }
From source file:org.dspace.disseminate.CitationDocumentServiceImpl.java
License:BSD License
@Override public void drawTable(PDPage page, PDPageContentStream contentStream, float y, float margin, String[][] content, PDFont font, int fontSize, boolean cellBorders) throws IOException { final int rows = content.length; final int cols = content[0].length; final float rowHeight = 20f; final float tableWidth = page.getMediaBox().getWidth() - (2 * margin); final float tableHeight = rowHeight * rows; final float colWidth = tableWidth / (float) cols; final float cellMargin = 5f; if (cellBorders) { //draw the rows float nexty = y; for (int i = 0; i <= rows; i++) { contentStream.drawLine(margin, nexty, margin + tableWidth, nexty); nexty -= rowHeight;/*ww w . j a v a2 s.co m*/ } //draw the columns float nextx = margin; for (int i = 0; i <= cols; i++) { contentStream.drawLine(nextx, y, nextx, y - tableHeight); nextx += colWidth; } } //now add the text contentStream.setFont(font, fontSize); float textx = margin + cellMargin; float texty = y - 15; for (int i = 0; i < content.length; i++) { for (int j = 0; j < content[i].length; j++) { String text = content[i][j]; contentStream.beginText(); contentStream.moveTextPositionByAmount(textx, texty); contentStream.drawString(text); contentStream.endText(); textx += colWidth; } texty -= rowHeight; textx = margin + cellMargin; } }