public static void main(String[] args) throws IOException { try (PDDocument doc = new PDDocument()) { PDPage page = new PDPage(); // Create a landscape page // page.setMediaBox(new PDRectangle(PDRectangle.A4.getHeight(), // PDRectangle.A4.getWidth())); doc.addPage(page); // Initialize table float margin = 10; float tableWidth = page.getMediaBox().getWidth() - (2 * margin); float yStartNewPage = page.getMediaBox().getHeight() - (2 * margin); float yStart = yStartNewPage; float bottomMargin = 0; // Create the data List<List> data = new ArrayList<>(); data.add(new ArrayList<>(Arrays.asList("Key", "Value"))); for (int i = 1; i <= 5; i++) { data.add(new ArrayList<>(Arrays.asList(String.valueOf(i), "value:" + i))); } BaseTable dataTable = new BaseTable( yStart, yStartNewPage, bottomMargin, tableWidth, margin, doc, page, true, true); DataTable t = new DataTable(dataTable, page); t.addListToTable(data, DataTable.HASHEADER); dataTable.draw(); File file = new File("box.pdf"); System.out.println("Sample file saved at : " + file.getAbsolutePath()); doc.save(file); } }
/** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // 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); // Create a new font object selecting one of the PDF base fonts PDFont fontPlain = PDType1Font.HELVETICA; PDFont fontBold = PDType1Font.HELVETICA_BOLD; PDFont fontItalic = PDType1Font.HELVETICA_OBLIQUE; PDFont fontMono = PDType1Font.COURIER; // Start a new content stream which will "hold" the to be created content PDPageContentStream cos = new PDPageContentStream(document, page1); int line = 1; // Define a text content stream using the selected font, move the cursor and draw some text cos.beginText(); cos.setFont(fontPlain, 12); cos.setNonStrokingColor(Color.RED); // width is 100; height is 20 lines down everytime cos.moveTextPositionByAmount(100, rect.getHeight() - 20 * (++line)); cos.drawString("eeey boy"); cos.endText(); cos.beginText(); cos.setFont(fontItalic, 12); cos.setNonStrokingColor(Color.GREEN); cos.moveTextPositionByAmount(100, rect.getHeight() - 20 * (++line)); cos.drawString("How do you do?"); cos.endText(); cos.beginText(); cos.setFont(fontBold, 12); cos.setNonStrokingColor(Color.BLACK); cos.moveTextPositionByAmount(100, rect.getHeight() - 20 * (++line)); cos.drawString("Dit duurde veel langer dan nodig"); cos.endText(); cos.beginText(); cos.setFont(fontMono, 12); cos.setNonStrokingColor(Color.BLACK); cos.moveTextPositionByAmount(100, rect.getHeight() - 20 * (++line)); cos.drawString("maar ik was wat dingentjes aan t uitzoeken."); cos.endText(); // Add some gucci into this place try { BufferedImage awtImage = ImageIO.read(new File("Gucci.jpg")); PDXObjectImage ximage = new PDPixelMap(document, awtImage); float scale = 0.5f; // alter this value to set the image size cos.drawXObject(ximage, 100, 400, ximage.getWidth() * scale, ximage.getHeight() * scale); } catch (IIOException Fnfex) { System.out.println("No image for you"); } // Make sure that the content stream is closed: cos.close(); // New page PDPage page2 = new PDPage(PDPage.PAGE_SIZE_A4); document.addPage(page2); cos = new PDPageContentStream(document, page2); // draw a red box in the lower left hand corner cos.setNonStrokingColor(Color.RED); cos.fillRect(10, 10, 100, 100); // add two lines of different widths cos.setLineWidth(1); cos.addLine(200, 250, 400, 250); cos.closeAndStroke(); cos.setLineWidth(5); cos.addLine(200, 300, 400, 300); cos.closeAndStroke(); // close the content stream for page 2 cos.close(); // Save the results and ensure that the document is properly closed: document.save("GUCCI.pdf"); document.close(); }
@SuppressWarnings("deprecation") @Override public void generateReport() throws ReportException { int pageNum = 1; // log.info("Report creation started"); // declare data variables List<WarehousePart> records = null; try { records = gateway.fetchWarehouseAndParts(); } catch (GatewayException e) { throw new ReportException("Error in report generation: " + e.getMessage()); } // prep the report page 1 doc = new PDDocument(); PDPage page1 = new PDPage(); PDRectangle rect = page1.getMediaBox(); doc.addPage(page1); PDPage page2 = new PDPage(); PDRectangle rect2 = page2.getMediaBox(); doc.addPage(page2); // get content stream for page 1 PDPageContentStream content = null; PDFont fontPlain = PDType1Font.HELVETICA; PDFont fontBold = PDType1Font.HELVETICA_BOLD; PDFont fontItalic = PDType1Font.HELVETICA_OBLIQUE; PDFont fontMono = PDType1Font.COURIER; PDFont fontMonoBold = PDType1Font.COURIER_BOLD; page1.setRotation(90); float margin = 20; try { content = new PDPageContentStream(doc, page1); // print header of page 1 content.concatenate2CTM(0, 1, -1, 0, 718, 0); content.setNonStrokingColor(Color.CYAN); content.setStrokingColor(Color.BLACK); float bottomY = rect.getHeight() - margin - 100; float headerEndX = rect.getWidth() - margin * 2; content.addRect(margin, bottomY, headerEndX, 100); content.fillAndStroke(); content.setNonStrokingColor(Color.BLACK); // print report title content.setFont(fontBold, 24); content.beginText(); content.newLineAtOffset(margin + 15, bottomY + 15); content.showText("Warehouse Inventory Summary"); content.endText(); // page Number content.setFont(fontBold, 12); content.beginText(); content.newLineAtOffset(710, 150); content.showText("Page " + pageNum); content.endText(); // Date Date date = new Date(); content.setFont(fontBold, 12); content.beginText(); content.newLineAtOffset(30, 150); content.showText(date.toString()); content.endText(); content.setFont(fontMonoBold, 12); float dataY = 610; // colum layout, this might require tweaking // warehouse Name Part # Part Name Quantity Unit float colX_0 = margin + 15; // warehouse name float colX_1 = colX_0 + 180; // Part number float colX_2 = colX_1 + 100; // Part Name float colX_3 = colX_2 + 180; // quantity float colX_4 = colX_3 + 100; // unit // print the colum texts content.beginText(); content.newLineAtOffset(colX_0, dataY); content.showText("Warehouse Name"); content.endText(); content.beginText(); content.newLineAtOffset(colX_1, dataY); content.showText("Part Number"); content.endText(); content.beginText(); content.newLineAtOffset(colX_2, dataY); content.showText("Part Name"); content.endText(); content.beginText(); content.newLineAtOffset(colX_3, dataY); content.showText("Quantity"); content.endText(); content.beginText(); content.newLineAtOffset(colX_4, dataY); content.showText("Unit"); content.endText(); // print the report rows content.setFont(fontMono, 12); int counter = 1; for (WarehousePart wp : records) { // the offset for the current row float offset = dataY - (counter * (fontMono.getHeight(12) + 15)); Warehouse w = wp.getOwner(); Part p = wp.getPart(); content.beginText(); content.newLineAtOffset(colX_0, offset); content.showText(w.getWarehouseName()); content.endText(); content.beginText(); content.newLineAtOffset(colX_1, offset); content.showText("" + p.getPartNumber()); content.endText(); content.beginText(); content.newLineAtOffset(colX_2, offset); content.showText("" + p.getPartName()); content.endText(); content.beginText(); content.newLineAtOffset(colX_3, offset); content.showText("" + wp.getQuantity()); content.endText(); content.beginText(); content.newLineAtOffset(colX_4, offset); content.showText("" + p.getUnitQuanitity()); content.endText(); counter++; if (counter > 25) { content.close(); break; } } content = new PDPageContentStream(doc, page2); content.concatenate2CTM(0, 1, -1, 0, 718, 0); content.setFont(fontMono, 12); page2.setRotation(90); pageNum = 2; int counter2 = 1; // page Number content.setFont(fontBold, 12); content.beginText(); content.newLineAtOffset(710, 150); content.showText("Page " + pageNum); content.endText(); // Date content.setFont(fontBold, 12); content.beginText(); content.newLineAtOffset(30, 150); content.showText(date.toString()); content.endText(); content.setFont(fontMono, 12); for (WarehousePart wp : records.subList(25, records.size())) { // the offset for the current row float offset = dataY - (counter2 * (fontMono.getHeight(12) + 15)); Warehouse w = wp.getOwner(); Part p = wp.getPart(); content.beginText(); content.newLineAtOffset(colX_0, offset); content.showText(w.getWarehouseName()); content.endText(); content.beginText(); content.newLineAtOffset(colX_1, offset); content.showText("" + p.getPartNumber()); content.endText(); content.beginText(); content.newLineAtOffset(colX_2, offset); content.showText("" + p.getPartName()); content.endText(); content.beginText(); content.newLineAtOffset(colX_3, offset); content.showText("" + wp.getQuantity()); content.endText(); content.beginText(); content.newLineAtOffset(colX_4, offset); content.showText("" + p.getUnitQuanitity()); content.endText(); counter2++; } } catch (IOException e) { throw new ReportException("Error in report generation: " + e.getMessage()); } finally { try { content.close(); } catch (IOException e) { e.printStackTrace(); } } }