List of usage examples for org.apache.pdfbox.pdmodel PDDocument PDDocument
public PDDocument()
From source file:com.jnd.sonar.analysisreport.AnalysisReportHelper.java
License:Open Source License
/** * create the second sample document from the PDF file format specification. * @param reportDataMap //from w w w .j a va 2 s. co m * * @param file The file to write the PDF to. * @param message The message to write in the file. * * @throws IOException If there is an error writing the data. * @throws COSVisitorException If there is an error writing the PDF. */ public String createMetricReport(Map<String, String> reportDataMap) throws IOException, COSVisitorException { // the document PDDocument doc = null; StringBuilder strRow = new StringBuilder(); String reportname = null; java.util.Random R = new java.util.Random(); try { doc = new PDDocument(); System.out.println("Creating PDF Line 264"); PDPage page = new PDPage(); doc.addPage(page); PDFont font = PDType1Font.HELVETICA_BOLD; PDPageContentStream contentStream = new PDPageContentStream(doc, page); contentStream.beginText(); contentStream.setFont(font, 12); contentStream.moveTextPositionByAmount(100, 700 - 0); contentStream.drawString(" Sonar Analysis Report "); contentStream.endText(); contentStream.beginText(); contentStream.setFont(font, 12); contentStream.moveTextPositionByAmount(100, 700 - 20); contentStream.drawString("==================================================== "); contentStream.endText(); System.out.println("Print Entries from Analysis Data Map."); int i = 2; for (Map.Entry<String, String> entry : reportDataMap.entrySet()) { contentStream.beginText(); contentStream.setFont(font, 12); contentStream.moveTextPositionByAmount(100, 700 - (i * 20)); contentStream.drawString(i + ") " + entry.getKey() + " = " + entry.getValue()); contentStream.endText(); //strRow.append(i + ") " + entry.getKey() + " = " + entry.getValue() + "\n "); i++; } //System.out.println(strRow); contentStream.close(); System.out.println("Done Writing Text.Save Doc."); reportname = "sonarreport" + java.util.Calendar.getInstance().getTimeInMillis() + "_" + R.nextInt() + ".pdf"; doc.save(reportname); System.out.println("Report Created.Exit to email sending. name=>" + reportname); } finally { if (doc != null) { doc.close(); } } return reportname; }
From source file:com.mycompany.mavenproject4.NewMain.java
/** * @param args the command line arguments *//* ww w.j av a 2 s .co m*/ public static void main(String[] args) { PDDocument doc; doc = new PDDocument(); doc.addPage(new PDPage()); try { doc.save("Empty PDF.pdf"); doc.close(); } catch (Exception io) { System.out.println(io); } }
From source file:com.pasi.pdfbox.Main.java
License:Apache License
public static void main(String[] args) throws Exception { System.setProperty("sun.java2d.cmm", "sun.java2d.cmm.kcms.KcmsServiceProvider"); String file = args != null && args.length > 0 ? args[0] : "/Users/bean/Desktop/Patient_BP_Report-v0.08.pdf"; PatientBloodPressureReport report = createPatientBloodPressureReport(); PDDocument document = new PDDocument(); DocWriter writer = new PBPRptDocWriter(document, report); writer.write(file, "all", "readonly"); /*// w ww .java 2s . com PDDocument doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); try { PDPageContentStream contents = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true); float pageHeight = page.getMediaBox().getHeight() - 20; PDText text = new PDText(20, pageHeight, "The Apache PDFBox library is an open source Java tool for working with PDF documents. This project allows creation of new PDF documents, manipulation of existing documents and the ability to extract content from documents. Apache PDFBox also includes several command line utilities. Apache PDFBox is published under the Apache License v2.0."); float height = writeText(contents, text); PDText title = new PDText(20, pageHeight - height - 20, "Bar Chart Example"); writeText(contents, title); JFreeChart chart = createBarChart(); BufferedImage image = chart.createBufferedImage(600, 400); PDImageXObject pdImage = LosslessFactory.createFromImage(doc, image); float scale = 1f; //contents.drawImage(pdImage, 20, 500, pdImage.getWidth() * scale, pdImage.getHeight() * scale); contents.close(); doc.save(new File("/Users/bean/Desktop/Patient_BP_Report-v0.08.pdf")); doc.close(); } catch (IOException e) { e.printStackTrace(); } */ }
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);//from w w w . j ava2 s .c om // 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.planbase.pdf.layoutmanager.PdfLayoutMgr.java
License:Apache License
private PdfLayoutMgr(PDColorSpace cs) throws IOException { doc = new PDDocument(); colorSpace = cs; }
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;/* ww w. ja va 2 s . co m*/ // 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.santaanna.friendlyreader.pdfstod.pdfstod3.ReplaceStringStreamEngine.java
License:Apache License
public void sparaTokens(List intokens) { listTokens(intokens);/*ww w.j av a 2 s .c o m*/ // if (true) return; try { SkrivUt(4, ">>> Fre spara tokens i sparaTokens."); SkrivUt(4, "Tok len: " + intokens.size()); if (intokens == null) SkrivUt(4, "Token == null."); if (doc == null) { SkrivUt(4, "doc == null"); doc = new PDDocument(); } if (page1 == null) SkrivUt(4, "page1 == null"); PDStream updatedStream = new PDStream(doc); // SkrivUt(4, "1."); OutputStream out = updatedStream.createOutputStream(); // SkrivUt(4, "2."); ContentStreamWriter tokenWriter = new ContentStreamWriter(out); // SkrivUt(4, "3."); tokenWriter.writeTokens(intokens); // Denna rad felar => Sparande felar. // SkrivUt(4, "4."); page1.setContents(updatedStream); SkrivUt(4, ">>> Efter spara tokens i sparaTokens."); /* doc.save( outputFile ); // Skriv tillbaka resultatet till fil! Tas bort senare! if( doc != null ) { doc.close(); } * */ } catch (java.io.IOException ioe) { SkrivUt(0, "IOException i sparaTokens."); } /* catch (org.apache.pdfbox.exceptions.COSVisitorException cve) { // Denna exception skall bara vara aktiverad vid save och close! SkrivUt(4, "COSVisitorException i sparaTokens."); } */ // finally // { //} }
From source file:com.synopsys.integration.blackduck.report.pdf.RiskReportPdfWriter.java
License:Apache License
public File createPDFReportFile(final File outputDirectory, final ReportData report) throws RiskReportException { final IntegrationEscapeUtil escapeUtil = new IntegrationEscapeUtil(); final String escapedProjectName = escapeUtil.escapeForUri(report.getProjectName()); final String escapedProjectVersionName = escapeUtil.escapeForUri(report.getProjectVersion()); final File pdfFile = new File(outputDirectory, escapedProjectName + "_" + escapedProjectVersionName + "_BlackDuck_RiskReport.pdf"); if (pdfFile.exists()) { pdfFile.delete();//from w w w.j a v a 2 s . c o m } final PDDocument document = new PDDocument(); document.getDocumentInformation().setAuthor("Black Duck Software"); document.getDocumentInformation().setCreator("Integrations"); document.getDocumentInformation().setSubject("Hub Risk Report"); try (PDFBoxManager pdfManager = new PDFBoxManager(pdfFile, document)) { this.pdfManager = pdfManager; final PDRectangle pageBox = pdfManager.currentPage.getMediaBox(); final float pageWidth = pageBox.getWidth(); final float pageHeight = pageBox.getHeight(); final PDRectangle headerRectangle = writeHeader(pageWidth, pageHeight); final PDRectangle bottomOfProjectInfoRectangle = writeProjectInformation(pageWidth, headerRectangle.getLowerLeftY(), report); final PDRectangle bottomOfSummaryTableRectangle = writeSummaryTables(pageWidth, bottomOfProjectInfoRectangle.getLowerLeftY(), report); final PDRectangle bottomOfComponentTableRectangle = writeComponentTable(pageWidth, bottomOfSummaryTableRectangle.getLowerLeftY(), report); return pdfFile; } catch (final IOException | URISyntaxException e) { final String errorString = "Couldn't create the report: "; logger.trace(errorString + e.getMessage(), e); throw new RiskReportException(errorString + e.getMessage(), e); } }
From source file:com.synopsys.integration.blackduck.service.model.pdf.RiskReportPdfWriter.java
License:Apache License
public File createPDFReportFile(final File outputDirectory, final ReportData report) throws RiskReportException { final IntegrationEscapeUtil escapeUtil = new IntegrationEscapeUtil(); final String escapedProjectName = escapeUtil.escapeForUri(report.getProjectName()); final String escapedProjectVersionName = escapeUtil.escapeForUri(report.getProjectVersion()); final File pdfFile = new File(outputDirectory, escapedProjectName + "_" + escapedProjectVersionName + "_BlackDuck_RiskReport.pdf"); if (pdfFile.exists()) { pdfFile.delete();/*from w w w.j av a2s. c o m*/ } final PDDocument document = new PDDocument(); document.getDocumentInformation().setAuthor("Black Duck Software"); document.getDocumentInformation().setCreator("Integrations"); document.getDocumentInformation().setSubject("Black Duck Risk Report"); try (PDFBoxManager pdfManager = new PDFBoxManager(pdfFile, document)) { this.pdfManager = pdfManager; final PDRectangle pageBox = pdfManager.currentPage.getMediaBox(); final float pageWidth = pageBox.getWidth(); final float pageHeight = pageBox.getHeight(); final PDRectangle headerRectangle = writeHeader(pageWidth, pageHeight); final PDRectangle bottomOfProjectInfoRectangle = writeProjectInformation(pageWidth, headerRectangle.getLowerLeftY(), report); final PDRectangle bottomOfSummaryTableRectangle = writeSummaryTables(pageWidth, bottomOfProjectInfoRectangle.getLowerLeftY(), report); final PDRectangle bottomOfComponentTableRectangle = writeComponentTable(pageWidth, bottomOfSummaryTableRectangle.getLowerLeftY(), report); return pdfFile; } catch (final IOException | URISyntaxException e) { final String errorString = "Couldn't create the report: "; logger.trace(errorString + e.getMessage(), e); throw new RiskReportException(errorString + e.getMessage(), e); } }
From source file:com.truckzoo.test.pdf.SuperimposePage.java
License:Apache License
public static void main(String[] args) throws IOException { /*if (args.length != 2) {//from ww w . j av a 2 s .c o m System.err.println("usage: " + SuperimposePage.class.getName() + " <source-pdf> <dest-pdf>"); System.exit(1); }*/ String sourcePath = args[0]; String destPath = args[1]; PDDocument sourceDoc = null; try { // load the source PDF sourceDoc = PDDocument.load(new File(sourcePath)); int sourcePage = 1; // create a new PDF and add a blank page PDDocument doc = new PDDocument(); PDPage page = new PDPage(); doc.addPage(page); // write some sample text to the new page PDPageContentStream contents = new PDPageContentStream(doc, page); contents.beginText(); contents.setFont(PDType1Font.HELVETICA_BOLD, 12); contents.newLineAtOffset(2, PDRectangle.LETTER.getHeight() - 12); contents.showText("Sample text"); contents.endText(); // Create a Form XObject from the source document using LayerUtility LayerUtility layerUtility = new LayerUtility(doc); PDFormXObject form = layerUtility.importPageAsForm(sourceDoc, sourcePage - 1); // draw the full form contents.drawForm(form); // draw a scaled form contents.saveGraphicsState(); Matrix matrix = Matrix.getScaleInstance(0.5f, 0.5f); contents.transform(matrix); contents.drawForm(form); contents.restoreGraphicsState(); // draw a scaled and rotated form contents.saveGraphicsState(); matrix.rotate(1.8 * Math.PI); // radians contents.transform(matrix); contents.drawForm(form); contents.restoreGraphicsState(); contents.close(); doc.save(destPath); doc.close(); } finally { if (sourceDoc != null) { sourceDoc.close(); } } }