/** * Creates a PDF file: hello_mirrored_margins.pdf * * @param args no arguments needed */ public static void main(String[] args) throws DocumentException, IOException { // step 1 Document document = new Document(); // step 2 PdfWriter.getInstance(document, new FileOutputStream(RESULT)); // setting page size, margins and mirrered margins document.setPageSize(PageSize.A5); document.setMargins(36, 72, 108, 180); document.setMarginMirroringTopBottom(true); // step 3 document.open(); // step 4 document.add( new Paragraph( "The left margin of this odd page is 36pt (0.5 inch); " + "the right margin 72pt (1 inch); " + "the top margin 108pt (1.5 inch); " + "the bottom margin 180pt (2.5 inch).")); Paragraph paragraph = new Paragraph(); paragraph.setAlignment(Element.ALIGN_JUSTIFIED); for (int i = 0; i < 20; i++) { paragraph.add( "Hello World! Hello People! " + "Hello Sky! Hello Sun! Hello Moon! Hello Stars!"); } document.add(paragraph); document.add(new Paragraph("The top margin 180pt (2.5 inch).")); // step 5 document.close(); }
/** * Method to export charts as PDF files using the defined path. * * @param path The filename and absolute path. * @param chart The JFreeChart object. * @param width The width of the PDF file. * @param height The height of the PDF file. * @param mapper The font mapper for the PDF file. * @param title The title of the PDF file. * @throws IOException If writing a PDF file fails. */ @SuppressWarnings("deprecation") public static void exportPdf( String path, JFreeChart chart, int width, int height, FontMapper mapper, String title) throws IOException { File file = new File(path); FileOutputStream pdfStream = new FileOutputStream(file); BufferedOutputStream pdfOutput = new BufferedOutputStream(pdfStream); Rectangle pagesize = new Rectangle(width, height); Document document = new Document(); document.setPageSize(pagesize); document.setMargins(50, 50, 50, 50); document.addAuthor("OMSimulationTool"); document.addSubject(title); try { PdfWriter pdfWriter = PdfWriter.getInstance(document, pdfOutput); document.open(); PdfContentByte contentByte = pdfWriter.getDirectContent(); PdfTemplate template = contentByte.createTemplate(width, height); Graphics2D g2D = template.createGraphics(width, height, mapper); Double r2D = new Rectangle2D.Double(0, 0, width, height); chart.draw(g2D, r2D); g2D.dispose(); contentByte.addTemplate(template, 0, 0); } catch (DocumentException de) { JOptionPane.showMessageDialog( null, "Failed to write PDF document.\n" + de.getMessage(), "Failed", JOptionPane.ERROR_MESSAGE); de.printStackTrace(); } document.close(); }
public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); Rectangle one = new Rectangle(70, 140); Rectangle two = new Rectangle(700, 400); document.setPageSize(one); document.setMargins(2, 2, 2, 2); document.open(); Paragraph p = new Paragraph("Hi"); document.add(p); document.setPageSize(two); document.setMargins(20, 20, 20, 20); document.newPage(); document.add(p); document.close(); }
@Override protected void buildPdfDocument( Map<String, Object> model, Document doc, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub @SuppressWarnings("unchecked") ArrayList<ctUsuario> listaUsuario = (ArrayList<ctUsuario>) model.get("listaUsuario"); // grupo = (String)model.get("grupo"); PdfPTable tablaPDF = new PdfPTable(11); // 11 columns. Font fuenteTabla = new Font(Font.FontFamily.UNDEFINED, 12, Font.BOLD); Font fuenteCelda = new Font(Font.FontFamily.UNDEFINED, 11); tablaPDF.addCell(new Phrase("Nombre", fuenteTabla)); tablaPDF.addCell(new Phrase("Apellido", fuenteTabla)); tablaPDF.addCell(new Phrase("Fecha De Nacimiento", fuenteTabla)); tablaPDF.addCell(new Phrase("Calle", fuenteTabla)); tablaPDF.addCell(new Phrase("Num. Ext", fuenteTabla)); tablaPDF.addCell(new Phrase("Num. Int", fuenteTabla)); tablaPDF.addCell(new Phrase("Colonia", fuenteTabla)); tablaPDF.addCell(new Phrase("CP", fuenteTabla)); tablaPDF.addCell(new Phrase("Municipio", fuenteTabla)); tablaPDF.addCell(new Phrase("Estado", fuenteTabla)); tablaPDF.addCell(new Phrase("Telefono", fuenteTabla)); for (ctUsuario ctUsuario : listaUsuario) { tablaPDF.addCell(new Phrase(ctUsuario.getcNombre(), fuenteCelda)); tablaPDF.addCell(new Phrase(ctUsuario.getcApellidos(), fuenteCelda)); tablaPDF.addCell(new Phrase(ctUsuario.getDtFechaNac(), fuenteCelda)); tablaPDF.addCell(new Phrase(ctUsuario.getcCalle(), fuenteCelda)); tablaPDF.addCell(new Phrase(ctUsuario.getcNumExterior(), fuenteCelda)); tablaPDF.addCell(new Phrase(ctUsuario.getcNumInterior(), fuenteCelda)); tablaPDF.addCell(new Phrase(ctUsuario.getcColonia(), fuenteCelda)); tablaPDF.addCell(new Phrase(ctUsuario.getcCP(), fuenteCelda)); // tablaPDF.addCell(new Phrase(ctUsuario.getcMunicipio(), fuenteCelda)); tablaPDF.addCell(new Phrase(ctUsuario.getcEstado(), fuenteCelda)); // tablaPDF.addCell(new Phrase(ctUsuario.getcTel(), fuenteCelda)); } tablaPDF.setWidthPercentage(100); Rectangle rect = new Rectangle(60, 30, 600, 800); writer.setBoxSize("art", rect); HeaderFooterPageEvent event = new HeaderFooterPageEvent(); writer.setPageEvent(event); doc.setPageSize(PageSize.A4.rotate()); doc.open(); doc.add(tablaPDF); doc.close(); pages = 0; }
public boolean convertToPDF() throws MalformedURLException, IOException { JFileChooser filechooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter(".pdf", "pdf"); filechooser.setFileFilter(filter); int result = filechooser.showSaveDialog(this); if (result == JFileChooser.APPROVE_OPTION) { File saveFile = filechooser.getSelectedFile(); Document pdfDocument = new Document(); PdfPTable table = new PdfPTable(1); com.itextpdf.text.Rectangle r = new com.itextpdf.text.Rectangle(780, 580); Image slideImage = null; PdfWriter pdfWriter; try { pdfWriter = PdfWriter.getInstance( pdfDocument, new FileOutputStream(saveFile.getAbsolutePath() + ".pdf")); pdfWriter.open(); pdfDocument.open(); // pdfDocument.setPageSize(r); for (int i = 0; i < canvas.length; i++) { BufferedImage tempimg = new BufferedImage(720, 540, BufferedImage.TYPE_INT_ARGB); Graphics2D temgraphics = (Graphics2D) tempimg.getGraphics(); temgraphics.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); temgraphics.drawImage(img[i], 0, 0, 720, 540, null); temgraphics.drawImage(canvas[i], 0, 0, 720, 540, null); // slideImage = Image.getInstance(img[i], null); slideImage = com.itextpdf.text.Image.getInstance(tempimg, null); // table.addCell(new PdfPCell(slideImage, true)); // Image imagex = com.itextpdf.text.Image.getInstance(""); pdfDocument.setPageSize(r); pdfDocument.newPage(); pdfDocument.add(slideImage); } pdfDocument.close(); pdfWriter.close(); System.out.println("Powerpoint file converted to PDF successfully"); return true; } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { return false; } return false; }
@Override public void imprimerBsp(Long idBsp, OutputStream stream) throws ServiceException { try { Document doc = new Document(); PdfWriter.getInstance(doc, stream); doc.setPageSize(PageSize.A4.rotate()); doc.open(); Util.produceHeader(doc); BonSortie bs = bonSortieDao.findById(idBsp); produceBspTable(bs, doc); doc.close(); } catch (DocumentException ex) { Logger.getLogger(OrdreSortieServiceImpl.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(OrdreSortieServiceImpl.class.getName()).log(Level.SEVERE, null, ex); } }
@Override public void imprimerOrdreSortie( Long idOrdre, OutputStream stream, Date dateOrdre, int noChapitre, int noOdre, String objet) throws ServiceException { try { OrdreSortie os = ordreSortieDao.findById(idOrdre); Document doc = new Document(); PdfWriter.getInstance(doc, stream); doc.setPageSize(PageSize.A4); doc.open(); Util.produceHeader(doc); produireOrdreSortieBody(os, doc, dateOrdre, noChapitre, noOdre, objet); doc.close(); } catch (DocumentException ex) { Logger.getLogger(OrdreSortieServiceImpl.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(OrdreSortieServiceImpl.class.getName()).log(Level.SEVERE, null, ex); } }
@Override public void createReport() throws DocumentException, MalformedURLException, IOException { document = new Document(); checkDirsExist(); final String fileName = getFileName(); PdfWriter.getInstance(document, new FileOutputStream(fileName)); document.open(); document.setPageSize(new Rectangle(1280, 600)); document.newPage(); Image image = Image.getInstance(report.getImagePath()); image.setAlignment(Image.RIGHT | Image.TEXTWRAP); document.add(image); createApplicationInfo(); createStatistics(); createTable(); document.close(); File imageFile = new File(report.getImagePath()); imageFile.delete(); }
private void startNewPage(Rectangle rectangle) { document.setPageSize(rectangle); startNewPage(); }
/** * @param shiftsToAccount * @param month * @param year * @return */ public static String createAccounting(ShiftInstance[] shiftsToAccount, int month, int year) { // check if personal Data exists if (!PersonalData.getInstance().isDataSet()) { UtilityBox.getInstance() .displayInfoPopup( "Fehlende Daten", "Um die " + "Abrechnung zu erstellen müssen\npersönliche Daten gespeichert sein."); return null; } boolean success = false; String filePath = "Abrechnungen/" + year; // create directory if nessessary UtilityBox.createDirectory(filePath); // change filePath from directory to file filePath = filePath + "/Abrechnung" + UtilityBox.getMonthString(month) + year + ".pdf"; Document accounting = new Document(); ArrayList<ShiftInstance> rd = new ArrayList<ShiftInstance>(); ArrayList<ShiftInstance> ktp = new ArrayList<ShiftInstance>(); ArrayList<ShiftInstance> baby = new ArrayList<ShiftInstance>(); ArrayList<ShiftInstance> breisach = new ArrayList<ShiftInstance>(); ArrayList<ShiftInstance> kiza = new ArrayList<ShiftInstance>(); ArrayList<ShiftInstance> event = new ArrayList<ShiftInstance>(); ArrayList<ShiftInstance> sc = new ArrayList<ShiftInstance>(); ArrayList<ShiftInstance> concert_hall = new ArrayList<ShiftInstance>(); ArrayList<ShiftInstance> kvs = new ArrayList<ShiftInstance>(); ArrayList<ShiftInstance> elw = new ArrayList<ShiftInstance>(); // add shifts to seperate shift lists for (int i = 0; i < shiftsToAccount.length; i++) { switch (shiftsToAccount[i].getType()) { case KTW: ktp.add(shiftsToAccount[i]); break; case RTW: rd.add(shiftsToAccount[i]); break; case HINTERGRUND: rd.add(shiftsToAccount[i]); break; case ELW: elw.add(shiftsToAccount[i]); break; case EVENT: event.add(shiftsToAccount[i]); break; case SC: sc.add(shiftsToAccount[i]); break; case CONCERT_HALL: concert_hall.add(shiftsToAccount[i]); break; case KVS: kvs.add(shiftsToAccount[i]); break; case BREISACH: breisach.add(shiftsToAccount[i]); break; case KIZA: kiza.add(shiftsToAccount[i]); break; default: break; } } ArrayList<ShiftInstance>[] allShifts = (ArrayList<ShiftInstance>[]) (new ArrayList[] {rd, elw, ktp, baby, breisach, kiza, event, sc, concert_hall, kvs}); try { accounting.setPageSize(PageSize.A4); PdfWriter pdfWriter = PdfWriter.getInstance(accounting, new FileOutputStream(filePath)); accounting.open(); for (int i = 0; i < allShifts.length; i++) { if (!allShifts[i].isEmpty()) { int numberOfPages = ((int) (allShifts[i].size() / 13)) + 1; int counter = allShifts[i].size() - 1; for (int j = 1; j <= numberOfPages; j++) { ArrayList<ShiftInstance> tempShiftInstances = new ArrayList<ShiftInstance>(); for (int k = 0; k < 13; k++) { if (counter < 0) { break; } tempShiftInstances.add(allShifts[i].get(counter)); counter--; } accounting.newPage(); success = createSingleAccounting(accounting, pdfWriter, tempShiftInstances, i * 10 + j); } } } // TODO: for JDK7 use Multicatch } catch (Exception e) { // DocumentException | IOException e) { UtilityBox.getInstance() .displayErrorPopup( "Abrechnung", "Fehler beim " + "Erstellen der Abrechnung:\n" + e.getMessage()); filePath = null; } finally { try { accounting.close(); } catch (Exception e) { UtilityBox.getInstance() .displayErrorPopup( "Abrechnung", "Fehler beim " + "Erstellen der Abrechnung:\nDokument nicht geschlossen:\n" + "" + e.getMessage()); filePath = null; } } return filePath; }
private Document newDocument() { Document document = new Document(); document.setPageSize(PageSize.LETTER); document.setMargins(72, 72, 72, 72); return document; }