List of usage examples for org.apache.pdfbox.pdmodel PDPage getMediaBox
public PDRectangle getMediaBox()
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();// ww w .j a v a 2s. 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.j a v a 2s . c o m*/ 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 w ww .j a va 2 s. c o m positionHeight += height; }
From source file:com.joowon.returnA.classifier.Classifier.java
License:Open Source License
@Deprecated public String getTestName() throws IOException { double[] bodyPosition = new PdfPageDivider(destinationParentPath + "/image_" + 1 + ".png").divide() .findHeadLine();//from w ww. j a v a 2s .co m PDPage page = document.getPage(0); int width = (int) page.getMediaBox().getWidth(); int height = (int) page.getMediaBox().getHeight(); int endY = (int) (height * bodyPosition[1]); String text = new PdfTextExtractor(page).addRegion(0, 0, width, endY).extract().toString(); String testName = HeadlineParser.parseTestName(text); String testType = HeadlineParser.parseTestType(text); if (testType.length() != 0) testName += " " + testType; return testName; }
From source file:com.joowon.returnA.classifier.Classifier.java
License:Open Source License
public String getBodyText() throws IOException { int numberOfPages = document.getNumberOfPages(); String text = ""; for (int i = 1; i <= numberOfPages; ++i) { double[][] bodyPosition = new PdfPageDivider(destinationParentPath + "/image_" + i + ".png").divide() .findBody();/* w w w.j a v a2 s . c o m*/ PDPage page = document.getPage(i - 1); for (double[] val : bodyPosition) { text += new PdfTextExtractor(page).addRegion((int) (val[0] * page.getMediaBox().getWidth()), (int) (val[1] * page.getMediaBox().getHeight()), (int) (val[2] * page.getMediaBox().getWidth()), (int) (val[3] * page.getMediaBox().getHeight())).extract(); } } return text; }
From source file:com.joowon.returnA.classifier.EbsBookCrawler.java
License:Open Source License
public void run() throws IOException { File destination = new File("/Users/Joowon/Desktop"); File bookFolder = new File(getClass().getClassLoader().getResource("book").getFile()); for (File book : bookFolder.listFiles()) { String outputName = destination.getPath() + "/" + book.getName().replace(".pdf", ".txt"); PDDocument document = PDDocument.load(book); String text = ""; for (PDPage page : document.getPages()) { text += new PdfTextExtractor(page) .addRegion(0, 0, (int) page.getMediaBox().getWidth(), (int) page.getMediaBox().getHeight()) .extract();//from w w w . ja va 2 s. c o m } new TxtWriter(outputName).write(text); document.close(); } }
From source file:com.PDF.Resume.java
public ByteArrayOutputStream createResume() throws ParseException, IOException, COSVisitorException { System.out.println(imagePath); // Create a document and add a page to it PDDocument document = new PDDocument(); PDPage page1 = new PDPage(PDPage.PAGE_SIZE_A4); // PDPage.PAGE_SIZE_LETTER is also possible PDRectangle rect = page1.getMediaBox(); // rect can be used to get the page width and height document.addPage(page1);/* ww w. j a va 2 s. com*/ // Create a new font object selecting one of the PDF base fonts PDFont fontPlain = PDType1Font.HELVETICA; // Start a new content stream which will "hold" the to be created content PDPageContentStream cos = new PDPageContentStream(document, page1); int line = 0; // Define a text content stream using the selected font, move the cursor and draw some text cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(475, rect.getHeight() - 800 * (++line)); cos.drawString("Pagina " + ++pagenumber); cos.endText(); line--; cos.beginText(); cos.setFont(fontPlain, 24); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 90 * (++line)); cos.drawString("Curriculum Vitae"); cos.endText(); if (user != null) { cos.beginText(); cos.setFont(fontPlain, 24); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 90 * (++line)); cos.drawString(user.getSurname() + ""); cos.endText(); } // add an image try { BufferedImage awtImage = ImageIO.read(new File( "C:\\ICT\\HBO\\Jaar 2\\Project Enterprise Web Apps\\Images\\Info-Support-klein-formaat-JPG.png")); PDXObjectImage ximage = new PDPixelMap(document, awtImage); float scale = 1f; // alter this value to set the image size cos.drawXObject(ximage, 350, 750, ximage.getWidth() * scale, ximage.getHeight() * scale); } catch (FileNotFoundException fnfex) { System.out.println("No image for you"); } // Make sure that the content stream is closed: cos.close(); //second page line = 0; PDPage page2 = new PDPage(PDPage.PAGE_SIZE_A4); document.addPage(page2); cos = new PDPageContentStream(document, page2); try { BufferedImage awtImage = ImageIO.read(new File( "C:\\ICT\\HBO\\Jaar 2\\Project Enterprise Web Apps\\Images\\Info-Support-klein-formaat-JPG.png")); PDXObjectImage ximage = new PDPixelMap(document, awtImage); float scale = 1f; // alter this value to set the image size cos.drawXObject(ximage, 350, 750, ximage.getWidth() * scale, ximage.getHeight() * scale); } catch (FileNotFoundException fnfex) { System.out.println("No image for you"); } cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(475, rect.getHeight() - 800 * (++line)); cos.drawString("Pagina " + ++pagenumber); cos.endText(); line--; cos.beginText(); cos.setFont(fontPlain, 18); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 90 * (++line)); cos.drawString("Personalia"); cos.endText(); line += 4; cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Naam:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getSurname() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Geslacht:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getGender() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Telefoonnummer:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getPersonalPhone() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Emailadres:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getPersonalEmail() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Adres:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getAddress() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Postcode:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getZipCode() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Stad:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getCity() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Provincie:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getRegion() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Functie:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getFunction() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Business Unit:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(user.getBusinessUnitAsString() + ""); cos.endText(); if (!educationList.isEmpty()) { line++; cos.beginText(); cos.setFont(fontPlain, 18); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Opleidingen"); cos.endText(); for (EmployeeHasEducation e : educationList) { cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("School:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(e.getEducation().getSchool() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Richting:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(e.getEducation().getDegree() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Niveau:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(e.getEducation().getGrade() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Begindatum:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(e.getStartDate().substring(0, 10) + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Einddatum:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(e.getEndDate().substring(0, 10) + ""); cos.endText(); } } if (!certificateList.isEmpty()) { line++; cos.beginText(); cos.setFont(fontPlain, 18); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Certificaten"); cos.endText(); for (Certificate c : certificateList) { cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Certificaat naam:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(c.getName() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Certificaat plaats:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(c.getCertificationAuthority() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Licentienummer:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(c.getLicenseNumber() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Startdatum:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(c.getStartDate().substring(0, 10) + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Einddatum:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(c.getExpirationDate().substring(0, 10) + ""); cos.endText(); line++; } line++; } cos.close(); if (!preceedings.isEmpty()) { line = 0; PDPage page3 = new PDPage(PDPage.PAGE_SIZE_A4); document.addPage(page3); cos = new PDPageContentStream(document, page3); try { BufferedImage awtImage = ImageIO.read(new File( "C:\\ICT\\HBO\\Jaar 2\\Project Enterprise Web Apps\\Images\\Info-Support-klein-formaat-JPG.png")); PDXObjectImage ximage = new PDPixelMap(document, awtImage); float scale = 1f; // alter this value to set the image size cos.drawXObject(ximage, 350, 750, ximage.getWidth() * scale, ximage.getHeight() * scale); } catch (FileNotFoundException fnfex) { System.out.println("No image for you"); } cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(475, rect.getHeight() - 800 * (++line)); cos.drawString("Pagina " + ++pagenumber); cos.endText(); line--; cos.beginText(); cos.setFont(fontPlain, 18); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 100 * (++line)); cos.drawString("Projecten"); line += 4; cos.endText(); for (Preceeding p : preceedings) { cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Projectnaam:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(p.getProject().getName() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Bedrijfsnaam:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(p.getProject().getCompany().getName() + " "); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Project rol:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(p.getDescription() + ""); cos.endText(); // } cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Startdatum:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(p.getProject().getStartDate() + ""); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Einddatum:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (line)); cos.drawString(p.getProject().getEndDate() + ""); cos.endText(); line++; } } cos.close(); //Skills of the user if (!skillList.isEmpty()) { line = 0; PDPage page3 = new PDPage(PDPage.PAGE_SIZE_A4); document.addPage(page3); cos = new PDPageContentStream(document, page3); try { BufferedImage awtImage = ImageIO.read(new File( "C:\\ICT\\HBO\\Jaar 2\\Project Enterprise Web Apps\\Images\\Info-Support-klein-formaat-JPG.png")); PDXObjectImage ximage = new PDPixelMap(document, awtImage); float scale = 1f; // alter this value to set the image size cos.drawXObject(ximage, 350, 750, ximage.getWidth() * scale, ximage.getHeight() * scale); } catch (FileNotFoundException fnfex) { System.out.println("No image for you"); } cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(475, rect.getHeight() - 800 * (++line)); cos.drawString("Pagina " + ++pagenumber); cos.endText(); line--; cos.beginText(); cos.setFont(fontPlain, 18); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 100 * (++line)); cos.drawString("Skills"); cos.endText(); line += 4; cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Skill & Rating:"); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(75, rect.getHeight() - 20 * (++line)); cos.drawString("Jaren ervaring:"); cos.endText(); line -= 2; for (SkillRating s : skillList) { cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (++line)); cos.drawString(s.getSkill().getName() + " niveau " + s.getRating()); cos.endText(); cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(0, 120, 201); cos.moveTextPositionByAmount(350, rect.getHeight() - 20 * (++line)); cos.drawString("" + s.getExpYears()); cos.endText(); line++; } } cos.close(); ByteArrayOutputStream output = new ByteArrayOutputStream(); document.save(output); document.close(); return output; }
From source file:com.projectlaver.util.VoucherService.java
License:Open Source License
File generateVoucherPdf(String pdfFilename, File barcodeImageFile, VoucherDTO dto) { // the result File pdfFile = null;/* w ww . j av a2s.c om*/ // the document PDDocument doc = null; try { // Create a new document and add a page to it doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); float pageWidth = page.getMediaBox().getWidth(); float pageHeight = page.getMediaBox().getHeight(); // For some reason this may need to happen after reading the image PDPageContentStream contentStream = new PDPageContentStream(doc, page); /** * Read & draw the client logo */ this.drawImageWithMaxEdge(contentStream, 15f, 629, 150, this.readImage(new URL(dto.getClientLogoUrl()), doc)); /** * Read & draw the barcode image */ BufferedImage bim = ImageIO.read(barcodeImageFile); PDXObjectImage barcodeImage = new PDPixelMap(doc, bim); // inspired by http://stackoverflow.com/a/22318681/535646 float barcodeScale = 1f; // reduce this value if the image is too large this.drawImage(contentStream, 206f, 315f, barcodeImage, barcodeScale); /** * Read & draw the social eq logo image */ float logoScale = 0.15f; // reduce this value if the image is too large this.drawImage(contentStream, 246f, 265f, this.readImage(new URL(this.socialEqLogoUrl), doc), logoScale); /** * Read & draw the client image */ this.drawImageWithMaxEdge(contentStream, 0f, 145, 612, this.readImage(new URL(dto.getClientCampaignImageUrl()), doc)); /** * Add some rectangles to the page to set off the sections */ contentStream.setNonStrokingColor(new Color(0.82f, 0.82f, 0.82f, 0.6f)); // light grey /** * around the merchant logo / title */ contentStream.fillRect(0, pageHeight - 10, pageWidth, 10); // top edge contentStream.fillRect(0, pageHeight - 175, pageWidth, 10); // bottom edge contentStream.fillRect(0, pageHeight - 175, 10, 175); // left edge contentStream.fillRect(170, pageHeight - 175, 10, 175); // right of the logo contentStream.fillRect(pageWidth - 10, pageHeight - 175, 10, 175); // right edge // behind the terms and conditions contentStream.fillRect(0f, 0f, pageWidth, 145f); /** * Handle the text */ // text color is black contentStream.setNonStrokingColor(Color.BLACK); // merchant name this.printNoWrap(page, contentStream, 195, 712, dto.getMerchantName(), 48); // listing title this.printNoWrap(page, contentStream, 195, 662, dto.getVoucherTitle(), 24); // Long description this.printNoWrap(page, contentStream, 30, 582, "Details:", 14); PdfParagraph ld = new PdfParagraph(30, 562, dto.getVoucherDetails(), 14); this.printLeftAlignedWithWrap(contentStream, ld); // Terms and conditions this.printNoWrap(page, contentStream, 30, 115, "Terms & Conditions:", 12); PdfParagraph toc = new PdfParagraph(30, 100, dto.getVoucherTerms(), 10); this.printLeftAlignedWithWrap(contentStream, toc); // Make sure that the content stream is closed: contentStream.close(); String pdfPath = System.getProperty("java.io.tmpdir") + pdfFilename; doc.save(pdfPath); pdfFile = new File(pdfPath); } catch (Exception e) { throw new RuntimeException("Could not create the PDF File.", e); } finally { if (doc != null) { try { doc.close(); } catch (IOException e) { throw new RuntimeException("Could not close the PDF Document.", e); } } } return pdfFile; }
From source file:com.projectlaver.util.VoucherService.java
License:Open Source License
void printCenteredNoWrap(PDPage page, PDPageContentStream contentStream, int offsetX, int offsetY, String text, int fontSize) throws IOException { // Do the calculation to get this centered float lineWidth = TITLE_FONT.getStringWidth(text) / 1000 * fontSize; contentStream.beginText();//from w ww.j a va 2 s . com contentStream.setFont(TITLE_FONT, fontSize); contentStream.moveTextPositionByAmount( offsetX + ((page.getMediaBox().getWidth() - offsetX - lineWidth) / 2), offsetY); contentStream.drawString(text); contentStream.endText(); }
From source file:com.przemo.pdfmanipulate.PDFBuilder.java
private static void renderForm(Formularz form, PDDocument doc) throws IOException { int i = 0;//from w ww . j ava 2 s. co m PDFont f = PDType1Font.HELVETICA; float fontSize = 17.0f; for (FormPage page : form.getPages()) { PDPage pag = doc.getPage(i); PDRectangle pageSize = pag.getMediaBox(); try (PDPageContentStream contentStream = new PDPageContentStream(doc, pag, PDPageContentStream.AppendMode.APPEND, false)) { contentStream.setFont(f, fontSize); contentStream.beginText(); contentStream.setNonStrokingColor(Color.black); for (Pole p : page.getPola()) { int x = p.getPozycjaX(); if (p.isRightAlign()) { x -= f.getStringWidth(p.getWartosc()) / 1000 * fontSize; } contentStream .setTextMatrix(Matrix.getTranslateInstance(x, pageSize.getHeight() - p.getPozycjaY())); contentStream.showText(p.getWartosc()); } contentStream.endText(); } i++; } }