List of usage examples for org.apache.pdfbox.pdmodel PDPageContentStream setNonStrokingColor
@Deprecated public void setNonStrokingColor(float[] components) throws IOException
From source file:com.fileOperations.EmailBodyToPDF.java
/** * Places the text of the email into the PDF * * @param eml EmailBodyPDF/*w w w . j a va 2 s.c o m*/ */ private static void createEmailBody(EmailBodyPDF eml) { PDDocument doc = null; PDPageContentStream contentStream = null; //Fonts used PDFont bodyTitleFont = PDType1Font.TIMES_BOLD; PDFont bodyFont = PDType1Font.TIMES_ROMAN; //Font Sizes float emailHeaderFontSize = 7; float leadingEmailHeader = 1.5f * emailHeaderFontSize; float bodyFontSize = 12; float leadingBody = 1.5f * bodyFontSize; try { //Create Document, Page, Margins. doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false); PDRectangle mediabox = page.getMediaBox(); float margin = 72; float width = mediabox.getWidth() - 2 * margin; float startX = mediabox.getLowerLeftX() + margin; float startY = mediabox.getUpperRightY() - margin; float textYlocation = margin; //Set Line Breaks List<String> sentDateContent = PDFBoxTools.setLineBreaks(eml.getSentDate(), width, emailHeaderFontSize, bodyFont); List<String> recievedDateContent = PDFBoxTools.setLineBreaks(eml.getReceiveDate(), width, emailHeaderFontSize, bodyFont); List<String> toContent = PDFBoxTools.setLineBreaks(eml.getTo(), width, emailHeaderFontSize, bodyFont); List<String> fromContent = PDFBoxTools.setLineBreaks(eml.getFrom(), width, emailHeaderFontSize, bodyFont); List<String> ccContent = PDFBoxTools.setLineBreaks(eml.getCc(), width, emailHeaderFontSize, bodyFont); List<String> bccContent = PDFBoxTools.setLineBreaks(eml.getBcc(), width, emailHeaderFontSize, bodyFont); List<String> attachmentContent = PDFBoxTools.setLineBreaks(eml.getAttachments(), width, emailHeaderFontSize, bodyFont); List<String> subjectContent = PDFBoxTools.setLineBreaks(eml.getSubject(), width, bodyFontSize, bodyFont); List<String> bodyContent = PDFBoxTools.setLineBreaks(eml.getBody(), width, bodyFontSize, bodyFont); //Set Email Header contentStream.beginText(); contentStream.setFont(bodyFont, emailHeaderFontSize); contentStream.setNonStrokingColor(Color.BLACK); contentStream.newLineAtOffset(startX, startY); //Set Date Sent if (!"".equals(eml.getSentDate())) { contentStream.setFont(bodyTitleFont, emailHeaderFontSize); contentStream.showText("Date Sent: "); contentStream.setFont(bodyFont, emailHeaderFontSize); contentStream.newLineAtOffset(0, -leadingEmailHeader); textYlocation += leadingEmailHeader; for (String line : sentDateContent) { if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingEmailHeader)) { contentStream.endText(); contentStream.close(); textYlocation = 0; page = new PDPage(); doc.addPage(page); contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false); contentStream.beginText(); contentStream.setFont(bodyFont, emailHeaderFontSize); contentStream.setNonStrokingColor(Color.BLACK); contentStream.newLineAtOffset(startX, startY); } contentStream.showText(line); contentStream.newLineAtOffset(0, -leadingEmailHeader); textYlocation += leadingEmailHeader; } } //Set Date Received if (!"".equals(eml.getReceiveDate().trim())) { contentStream.setFont(bodyTitleFont, emailHeaderFontSize); contentStream.showText("Date Received: "); contentStream.setFont(bodyFont, emailHeaderFontSize); contentStream.newLineAtOffset(0, -leadingEmailHeader); textYlocation += leadingEmailHeader; for (String line : recievedDateContent) { if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingEmailHeader)) { contentStream.endText(); contentStream.close(); textYlocation = 0; page = new PDPage(); doc.addPage(page); contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false); contentStream.beginText(); contentStream.setFont(bodyFont, emailHeaderFontSize); contentStream.setNonStrokingColor(Color.BLACK); contentStream.newLineAtOffset(startX, startY); } contentStream.showText(line); contentStream.newLineAtOffset(0, -leadingEmailHeader); textYlocation += leadingBody; } } contentStream.newLineAtOffset(0, -leadingBody); //Set From if (!"".equals(eml.getFrom().trim())) { contentStream.setFont(bodyTitleFont, emailHeaderFontSize); contentStream.showText("From: "); contentStream.setFont(bodyFont, emailHeaderFontSize); contentStream.newLineAtOffset(0, -leadingEmailHeader); textYlocation += leadingEmailHeader; for (String line : fromContent) { if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingEmailHeader)) { contentStream.endText(); contentStream.close(); textYlocation = 0; page = new PDPage(); doc.addPage(page); contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false); contentStream.beginText(); contentStream.setFont(bodyFont, emailHeaderFontSize); contentStream.setNonStrokingColor(Color.BLACK); contentStream.newLineAtOffset(startX, startY); } contentStream.showText(line); contentStream.newLineAtOffset(0, -leadingEmailHeader); textYlocation += leadingEmailHeader; } } //Set To if (!"".equals(eml.getTo().trim())) { contentStream.setFont(bodyTitleFont, emailHeaderFontSize); contentStream.showText("To: "); contentStream.setFont(bodyFont, emailHeaderFontSize); contentStream.newLineAtOffset(0, -leadingEmailHeader); textYlocation += leadingEmailHeader; for (String line : toContent) { if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingEmailHeader)) { contentStream.endText(); contentStream.close(); textYlocation = 0; page = new PDPage(); doc.addPage(page); contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false); contentStream.beginText(); contentStream.setFont(bodyFont, emailHeaderFontSize); contentStream.setNonStrokingColor(Color.BLACK); contentStream.newLineAtOffset(startX, startY); } contentStream.showText(line); contentStream.newLineAtOffset(0, -leadingEmailHeader); textYlocation += leadingEmailHeader; } } //Set CC if (!"".equals(eml.getCc().trim())) { contentStream.setFont(bodyTitleFont, emailHeaderFontSize); contentStream.showText("CC: "); contentStream.setFont(bodyFont, emailHeaderFontSize); contentStream.newLineAtOffset(0, -leadingEmailHeader); textYlocation += leadingEmailHeader; for (String line : ccContent) { if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingEmailHeader)) { contentStream.endText(); contentStream.close(); textYlocation = 0; page = new PDPage(); doc.addPage(page); contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false); contentStream.beginText(); contentStream.setFont(bodyFont, emailHeaderFontSize); contentStream.setNonStrokingColor(Color.BLACK); contentStream.newLineAtOffset(startX, startY); } contentStream.showText(line); contentStream.newLineAtOffset(0, -leadingEmailHeader); textYlocation += leadingEmailHeader; } } //Set BCC if (!"".equals(eml.getBcc().trim())) { contentStream.setFont(bodyTitleFont, emailHeaderFontSize); contentStream.showText("BCC: "); contentStream.setFont(bodyFont, emailHeaderFontSize); contentStream.newLineAtOffset(0, -leadingEmailHeader); textYlocation += leadingEmailHeader; for (String line : bccContent) { if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingEmailHeader)) { contentStream.endText(); contentStream.close(); textYlocation = 0; page = new PDPage(); doc.addPage(page); contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false); contentStream.beginText(); contentStream.setFont(bodyFont, emailHeaderFontSize); contentStream.setNonStrokingColor(Color.BLACK); contentStream.newLineAtOffset(startX, startY); } contentStream.showText(line); contentStream.newLineAtOffset(0, -leadingEmailHeader); textYlocation += leadingEmailHeader; } } //Set AttachmentList if (!"".equals(eml.getAttachments().trim())) { contentStream.setFont(bodyTitleFont, emailHeaderFontSize); contentStream.showText("Attachments: "); contentStream.setFont(bodyFont, emailHeaderFontSize); contentStream.newLineAtOffset(0, -leadingEmailHeader); textYlocation += leadingEmailHeader; for (String line : attachmentContent) { if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingEmailHeader)) { contentStream.endText(); contentStream.close(); textYlocation = 0; page = new PDPage(); doc.addPage(page); contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false); contentStream.beginText(); contentStream.setFont(bodyFont, emailHeaderFontSize); contentStream.setNonStrokingColor(Color.BLACK); contentStream.newLineAtOffset(startX, startY); } contentStream.showText(line); contentStream.newLineAtOffset(0, -leadingEmailHeader); textYlocation += leadingEmailHeader; } } //Set Subject if (!"".equals(eml.getSubject().trim())) { contentStream.newLineAtOffset(0, -leadingBody); contentStream.newLineAtOffset(0, -leadingBody); contentStream.setFont(bodyTitleFont, bodyFontSize); contentStream.showText("Subject: "); contentStream.newLineAtOffset(0, -leadingBody); textYlocation += leadingBody; contentStream.setFont(bodyFont, bodyFontSize); for (String line : subjectContent) { if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingEmailHeader)) { contentStream.endText(); contentStream.close(); textYlocation = 0; page = new PDPage(); doc.addPage(page); contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false); contentStream.beginText(); contentStream.setFont(bodyFont, emailHeaderFontSize); contentStream.setNonStrokingColor(Color.BLACK); contentStream.newLineAtOffset(startX, startY); } contentStream.showText(line); contentStream.newLineAtOffset(0, -leadingBody); textYlocation += leadingBody; } } if (!"".equals(eml.getBody().trim())) { // Set Email Body contentStream.newLineAtOffset(0, -leadingBody); contentStream.setFont(bodyTitleFont, bodyFontSize); contentStream.showText("Message: "); contentStream.setFont(bodyFont, bodyFontSize); contentStream.newLineAtOffset(0, -leadingBody); for (String line : bodyContent) { if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingBody)) { contentStream.endText(); contentStream.close(); textYlocation = 0; page = new PDPage(); doc.addPage(page); contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false); contentStream.beginText(); contentStream.setFont(bodyFont, bodyFontSize); contentStream.setNonStrokingColor(Color.BLACK); contentStream.newLineAtOffset(startX, startY); } textYlocation += leadingBody; contentStream.showText(line); contentStream.newLineAtOffset(0, -leadingBody); } contentStream.endText(); } contentStream.close(); doc.save(eml.getFilePath() + eml.getFileName()); } catch (IOException ex) { ExceptionHandler.Handle(ex); } finally { if (doc != null) { try { doc.close(); } catch (IOException ex) { ExceptionHandler.Handle(ex); } } } }
From source file:com.fileOperations.StampPDF.java
/** * This stamps docketed files./*ww w . j a v a 2s. c o m*/ * * @param file String (full file path) * @param docketTime Timestamp * @param dept */ public static void stampDocument(String file, Timestamp docketTime, String dept) { // the document PDDocument doc = null; try { PDFont stampFont = PDType1Font.TIMES_ROMAN; float stampFontSize = 14; String title = PDFBoxTools.HeaderTimeStamp(docketTime) + " " + dept; float titleWidth = stampFont.getStringWidth(title) / 1000 * stampFontSize; float titleHeight = stampFont.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * stampFontSize; int marginTop = 20; doc = PDDocument.load(new File(file)); if (!doc.isEncrypted()) { for (int i = 0; i < doc.getPages().getCount(); i++) { PDPageContentStream contentStream = null; PDPage page = (PDPage) doc.getPages().get(i); contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, true); page.getResources().getFontNames(); contentStream.beginText(); contentStream.setFont(stampFont, stampFontSize); contentStream.setNonStrokingColor(Color.RED); contentStream.newLineAtOffset((page.getMediaBox().getWidth() - titleWidth) / 2, page.getMediaBox().getHeight() - marginTop - titleHeight); contentStream.showText(title); contentStream.endText(); contentStream.close(); } doc.save(file); } } catch (IOException ex) { ExceptionHandler.Handle(ex); } finally { if (doc != null) { try { doc.close(); } catch (IOException ex) { ExceptionHandler.Handle(ex); } } } }
From source file:com.fileOperations.TXTtoPDF.java
/** * Takes the text from the string and insert it into the PDF file * * @param pdfFile String (path + filename) * @param text String (text from document) *///from www . j ava2 s .c o m private static void makePDF(String pdfFile, String text) { PDDocument doc = null; PDPageContentStream contentStream = null; //Fonts used PDFont bodyFont = PDType1Font.TIMES_ROMAN; //Font Sizes float bodyFontSize = 12; float leadingBody = 1.5f * bodyFontSize; try { //Create Document, Page, Margins. doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); contentStream = new PDPageContentStream(doc, page, AppendMode.APPEND, true, false); PDRectangle mediabox = page.getMediaBox(); float margin = 72; float width = mediabox.getWidth() - 2 * margin; float startX = mediabox.getLowerLeftX() + margin; float startY = mediabox.getUpperRightY() - margin; float textYlocation = margin; //Set Line Breaks text = text.replaceAll("[\\p{C}\\p{Z}]", System.getProperty("line.separator")); //strip ZERO WIDTH SPACE List<String> textContent = PDFBoxTools.setLineBreaks(text, width, bodyFontSize, bodyFont); contentStream.beginText(); contentStream.setFont(bodyFont, bodyFontSize); contentStream.setNonStrokingColor(Color.BLACK); contentStream.newLineAtOffset(startX, startY); if (!"".equals(text)) { for (String line : textContent) { if (textYlocation > (mediabox.getHeight() - (margin * 2) - leadingBody)) { contentStream.endText(); contentStream.close(); textYlocation = 0; page = new PDPage(); doc.addPage(page); contentStream = new PDPageContentStream(doc, page, true, true, false); contentStream.beginText(); contentStream.setFont(bodyFont, bodyFontSize); contentStream.setNonStrokingColor(Color.BLACK); contentStream.newLineAtOffset(startX, startY); } textYlocation += leadingBody; contentStream.showText(line); contentStream.newLineAtOffset(0, -leadingBody); } contentStream.endText(); } contentStream.close(); doc.save(pdfFile); } catch (IOException ex) { ExceptionHandler.Handle(ex); } finally { if (doc != null) { try { doc.close(); } catch (IOException ex) { ExceptionHandler.Handle(ex); } } } }
From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java
License:Open Source License
private static void title(String text, PDFont font, int fontSize, PDDocument doc) throws IOException { if (positionHeight + (page.getMediaBox().getHeight() / 6) >= page.getMediaBox().getHeight()) { if (positionTitleWidth > DEFAULT_TITLE_MARGIN_WIDTH) { initNewPage(doc);/*from w ww . java 2s .co m*/ } else { writeToRight(); } } // Jump line if not the first page title. if (positionHeight != DEFAULT_MARGIN_HEIGHT) { positionHeight += (titleSpaceHeight * 2); } PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false); stream.setNonStrokingColor(DEFAULT_TEXT_COLOR); float textWidth = font.getStringWidth(text) / 1000 * fontSize; float textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize; stream.beginText(); stream.setFont(font, fontSize); stream.newLineAtOffset(positionTitleWidth, page.getMediaBox().getHeight() - positionHeight - textHeight); stream.showText(text); stream.endText(); stream.close(); positionHeight += titleSpaceHeight + textHeight; }
From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java
License:Open Source License
private static int centerText(String text, PDFont font, int fontSize, PDPage page, PDDocument doc) throws IOException { PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false); stream.setNonStrokingColor(DEFAULT_TEXT_COLOR); float textWidth = font.getStringWidth(text) / 1000 * fontSize; float textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize; stream.beginText();//from w w w . j a v a2 s. c om stream.setFont(font, fontSize); stream.newLineAtOffset((page.getMediaBox().getWidth() - textWidth) / 2, page.getMediaBox().getHeight() - positionHeight - textHeight); stream.showText(text); stream.endText(); stream.close(); positionHeight += textHeight; return (int) textHeight; }
From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java
License:Open Source License
private static void rightText(String text, PDFont font, int fontSize, PDPage page, PDDocument doc) throws IOException { PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false); stream.setNonStrokingColor(DEFAULT_TEXT_COLOR); float textWidth = font.getStringWidth(text) / 1000 * fontSize; float textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize; stream.beginText();/* ww w. j ava 2 s . c o m*/ stream.setFont(font, fontSize); stream.newLineAtOffset(page.getMediaBox().getWidth() - textWidth - DEFAULT_LOGO_RIGHT_MARGIN_WIDTH, page.getMediaBox().getHeight() - positionHeight - textHeight); stream.showText(text); stream.endText(); stream.close(); positionHeight += textHeight; }
From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java
License:Open Source License
private static void leftText(String text, PDFont font, int fontSize, PDPage page, PDDocument doc) throws IOException { PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false); stream.setNonStrokingColor(DEFAULT_TEXT_COLOR); float textWidth = font.getStringWidth(text) / 1000 * fontSize; float textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * fontSize; stream.beginText();//from ww w .j a v a 2s .c o m stream.setFont(font, fontSize); stream.newLineAtOffset(DEFAULT_LOGO_LEFT_MARGIN_WIDTH, page.getMediaBox().getHeight() - positionHeight - textHeight); stream.showText(text); stream.endText(); stream.close(); positionHeight += textHeight; }
From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java
License:Open Source License
private static void centerImage(PDImageXObject image, PDPage page, PDDocument doc, int height, int width) throws IOException { PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false); stream.setNonStrokingColor(DEFAULT_TEXT_COLOR); stream.drawImage(image, (page.getMediaBox().getWidth() - width) / 2, page.getMediaBox().getHeight() - positionHeight - height, width, height); stream.close();//from www . ja v a 2 s . c o m positionHeight += height; }
From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java
License:Open Source License
private static void leftImage(PDImageXObject image, PDPage page, PDDocument doc, int height, int width) throws IOException { PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false); stream.setNonStrokingColor(DEFAULT_TEXT_COLOR); stream.drawImage(image, DEFAULT_LOGO_LEFT_MARGIN_WIDTH, page.getMediaBox().getHeight() - positionHeight - height, width, height); stream.close();/*from w w w .java2s . c om*/ positionHeight += height; }
From source file:com.github.gujou.deerbelling.sonarqube.service.PdfApplicationGenerator.java
License:Open Source License
private static void rightImage(PDImageXObject image, PDPage page, PDDocument doc, int height, int width) throws IOException { PDPageContentStream stream = new PDPageContentStream(doc, page, AppendMode.APPEND, false); stream.setNonStrokingColor(DEFAULT_TEXT_COLOR); stream.drawImage(image, (page.getMediaBox().getWidth() - width) - DEFAULT_LOGO_RIGHT_MARGIN_WIDTH, page.getMediaBox().getHeight() - positionHeight - height, width, height); stream.close();/*from ww w . jav a 2s .co m*/ positionHeight += height; }