List of usage examples for com.lowagie.text.pdf PdfWriter setStrictImageSequence
public void setStrictImageSequence(boolean strictImageSequence)
From source file:classroom.filmfestival_a.Movies07.java
@SuppressWarnings("unchecked") public static void main(String[] args) { // step 1/*w ww. j a v a 2s .com*/ Document document = new Document(); try { // step 2 OutputStream os = new FileOutputStream(RESULT); PdfWriter writer = PdfWriter.getInstance(document, os); writer.setStrictImageSequence(true); // step 3 document.open(); // step 4 Session session = (Session) MySessionFactory.currentSession(); Query q = session.createQuery("from FilmTitle order by title"); java.util.List<FilmTitle> results = q.list(); File f; Image img; Paragraph p; Chunk c; Font bold = new Font(Font.HELVETICA, 12, Font.BOLD); Font italic = new Font(Font.HELVETICA, 12, Font.ITALIC); for (FilmTitle movie : results) { f = new File("resources/classroom/filmposters/" + movie.getFilmId() + ".jpg"); if (f.exists()) { img = Image.getInstance(f.getPath()); document.add(img); } p = new Paragraph(20); c = new Chunk(movie.getTitle(), bold); c.setAnchor("http://cinema.lowagie.com/titel.php?id=" + movie.getFilmId()); p.add(c); c = new Chunk(" (" + movie.getYear() + ") ", italic); p.add(c); c = new Chunk("IMDB"); c.setAnchor("http://www.imdb.com/title/tt" + movie.getImdb()); p.add(c); document.add(p); Set<DirectorName> directors = movie.getDirectorNames(); List list = new List(); for (DirectorName director : directors) { list.add(director.getName()); } document.add(list); } // step 5 document.close(); } catch (IOException e) { LOGGER.error("IOException: ", e); } catch (DocumentException e) { LOGGER.error("DocumentException: ", e); } }
From source file:classroom.filmfestival_a.Movies08.java
@SuppressWarnings("unchecked") public static void main(String[] args) { // step 1/*from w w w .jav a 2s . co m*/ Document document = new Document(); try { // step 2 OutputStream os = new FileOutputStream(RESULT); PdfWriter writer = PdfWriter.getInstance(document, os); writer.setStrictImageSequence(true); // step 3 document.open(); // step 4 Session session = (Session) MySessionFactory.currentSession(); Query q = session.createQuery("from FilmTitle order by title"); java.util.List<FilmTitle> results = q.list(); File f; Image img; Paragraph p; Chunk c; Font bold = new Font(Font.HELVETICA, 12, Font.BOLD); Font italic = new Font(Font.HELVETICA, 12, Font.ITALIC); for (FilmTitle movie : results) { f = new File("resources/classroom/filmposters/" + movie.getFilmId() + ".jpg"); if (f.exists()) { img = Image.getInstance(f.getPath()); img.scaleToFit(72, 144); document.add(img); } p = new Paragraph(20); c = new Chunk(movie.getTitle(), bold); c.setAnchor("http://cinema.lowagie.com/titel.php?id=" + movie.getFilmId()); p.add(c); c = new Chunk(" (" + movie.getYear() + ") ", italic); p.add(c); c = new Chunk("IMDB"); c.setAnchor("http://www.imdb.com/title/tt" + movie.getImdb()); p.add(c); document.add(p); Set<DirectorName> directors = movie.getDirectorNames(); List list = new List(); for (DirectorName director : directors) { list.add(director.getName()); } document.add(list); } // step 5 document.close(); } catch (IOException e) { LOGGER.error("IOException: ", e); } catch (DocumentException e) { LOGGER.error("DocumentException: ", e); } }
From source file:com.amphisoft.epub2pdf.Converter.java
License:Open Source License
public void convert(String epubPath) throws IOException, DocumentException { File epubFile = new File(epubPath); if (!(epubFile.canRead())) { throw new IOException("Could not read " + epubPath); } else {/* w w w.j av a2 s.c o m*/ System.err.println("Converting " + epubFile.getAbsolutePath()); } String epubFilename = epubFile.getName(); String epubFilenameBase = epubFilename.substring(0, epubFilename.length() - 5); String pdfFilename = epubFilenameBase + ".pdf"; File outputFile = new File(outputDir.getAbsolutePath() + File.separator + pdfFilename); epubIn = Epub.fromFile(epubPath); XhtmlHandler.setSourceEpub(epubIn); Opf opf = epubIn.getOpf(); List<String> contentPaths = opf.spineHrefs(); List<File> contentFiles = new ArrayList<File>(); for (String path : contentPaths) { contentFiles.add(new File(epubIn.getContentRoot(), path)); } Ncx ncx = epubIn.getNcx(); List<NavPoint> ncxToc = new ArrayList<NavPoint>(); if (ncx != null) { ncxToc.addAll(ncx.getNavPointsFlat()); } Tree<TocTreeNode> tocTree = TocTreeNode.buildTocTree(ncx); XhtmlHandler.setTocTree(tocTree); Document doc = new Document(); boolean pageSizeOK = doc.setPageSize(pageSize); boolean marginsOK = doc.setMargins(marginLeftPt, marginRightPt, marginTopPt, marginBottomPt); System.err.println("Writing PDF to " + outputFile.getAbsolutePath()); PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(outputFile)); writer.setStrictImageSequence(true); PdfOutline bookmarkRoot = null; if (!(pageSizeOK && marginsOK)) { throw new RuntimeException("Failed to set PDF page size a/o margins"); } int fileCount = contentFiles.size(); printlnerr("Processing " + fileCount + " HTML file(s): "); int currentFile = 0; for (File file : contentFiles) { currentFile++; char progressChar; int mod10 = currentFile % 10; if (mod10 == 5) progressChar = '5'; else if (mod10 == 0) progressChar = '0'; else progressChar = '.'; printerr(progressChar); if (!(doc.isOpen())) { doc.open(); doc.newPage(); bookmarkRoot = writer.getRootOutline(); XhtmlHandler.setBookmarkRoot(bookmarkRoot); } NavPoint fileLevelNP = Ncx.findNavPoint(ncxToc, file.getName()); TreeNode<TocTreeNode> npNode = TocTreeNode.findInTreeByNavPoint(tocTree, fileLevelNP); if (fileLevelNP != null) { doc.newPage(); PdfOutline pdfOutlineParent = bookmarkRoot; if (npNode != null) { TreeNode<TocTreeNode> parent = npNode.getParent(); if (parent != null) { TocTreeNode parentTTN = parent.getValue(); if (parentTTN != null && parentTTN.getPdfOutline() != null) { pdfOutlineParent = parentTTN.getPdfOutline(); } } } PdfDestination here = new PdfDestination(PdfDestination.FIT); PdfOutline pdfTocEntry = new PdfOutline(pdfOutlineParent, here, fileLevelNP.getNavLabelText()); if (npNode != null) { npNode.getValue().setPdfDestination(here); npNode.getValue().setPdfOutline(pdfTocEntry); } } XhtmlHandler.process(file.getCanonicalPath(), doc); } printlnerr(); doc.close(); System.err.println("PDF written to " + outputFile.getAbsolutePath()); epubIn.cleanup(); }
From source file:datasoul.servicelist.ServiceListExporterDocument.java
License:Open Source License
public ServiceListExporterDocument(int type, String filename, boolean exportGuitarTabs) throws FileNotFoundException, DocumentException { document = new Document(); // ensure file do not exist to avoid garbage at the end of the file File f = new File(filename); if (f.exists()) { f.delete();//from w w w.j a v a 2 s . c o m } if (type == TYPE_RTF) { RtfWriter2.getInstance(document, new FileOutputStream(filename)); } else { PdfWriter w = PdfWriter.getInstance(document, new FileOutputStream(filename)); w.setStrictImageSequence(true); } document.open(); document.addCreator("Datasoul " + DatasoulMainForm.getVersion()); document.addCreationDate(); this.exportGuitarTabs = exportGuitarTabs; if (exportGuitarTabs) { guitarTabs = new LinkedList<Paragraph>(); } }
From source file:de.intranda.test_ics.ImageHelper.java
License:Apache License
@SuppressWarnings("unused") private void addPage(File imageFile, PdfWriter pdfWriter, Document pdfDocument, float shrinkRatio, float rotationDegree) throws DocumentException, IOException { float pointsPerInch = 200.0f; Image pageImage = null;//from w w w . ja va 2 s.co m float pageImageHeight = 0, pageImageWidth = 0; boolean lowMemory = (shrinkRatio == 1 ? false : true); URL inputImage = imageFile.toURI().toURL(); pdfWriter.setFullCompression(); pdfWriter.setStrictImageSequence(true); pdfWriter.setLinearPageMode(); LOGGER.debug("Out of memory on loading image for pdf generation"); // ByteArrayOutputStream stream = new ByteArrayOutputStream(); BufferedImage bitmap = ImageIO.read(imageFile); // LOGGER.debug( "Size of temporary image bitmap: Width = " + bitmap.getWidth() + "; Height = " + bitmap.getHeight()); LOGGER.debug("Reading file " + imageFile.getAbsolutePath()); pageImage = Image.getInstance(bitmap, null, false); bitmap.flush(); // stream.close(); pageImage.setRotationDegrees(-rotationDegree); LOGGER.debug("Image dimensions: Width = " + pageImage.getWidth() + "; Height = " + pageImage.getHeight()); pageImageHeight = pageImage.getHeight(); pageImageWidth = pageImage.getWidth(); pageImage.setAbsolutePosition(0, 0); // Rectangle pageRect = new Rectangle(pageImageWidth/shrinkRatio, pageImageHeight/shrinkRatio); com.lowagie.text.Rectangle pageRect = new com.lowagie.text.Rectangle(pageImageWidth, pageImageHeight); LOGGER.debug("Creating rectangle: Width = " + pageRect.getWidth() + "; Height = " + pageRect.getHeight()); pdfDocument.setPageSize(pageRect); if (pdfDocument.isOpen()) { pdfDocument.newPage(); pdfWriter.getDirectContent().addImage(pageImage); } else { pdfDocument.open(); pdfWriter.getDirectContent().addImage(pageImage); } pdfWriter.flush(); System.gc(); }
From source file:fr.univlorraine.mondossierweb.controllers.CalendrierController.java
License:Apache License
/** * /* ww w . ja v a2 s.c om*/ * @return le fichier pdf du calendrier des examens. */ public com.vaadin.server.Resource exportPdf() { String nomFichier = applicationContext.getMessage("pdf.calendrier.title", null, Locale.getDefault()) + " " + MainUI.getCurrent().getEtudiant().getNom().replace('.', ' ').replace(' ', '_') + ".pdf"; nomFichier = nomFichier.replaceAll(" ", "_"); StreamResource.StreamSource source = new StreamResource.StreamSource() { private static final long serialVersionUID = 1L; @Override public InputStream getStream() { try { ByteArrayOutputStream baosPDF = new ByteArrayOutputStream(OUTPUTSTREAM_SIZE); PdfWriter docWriter = null; Document document = configureDocument(MARGE_PDF); docWriter = PdfWriter.getInstance(document, baosPDF); docWriter.setEncryption(null, null, PdfWriter.AllowPrinting, PdfWriter.ENCRYPTION_AES_128); docWriter.setStrictImageSequence(true); creerPdfCalendrier(document, MainUI.getCurrent().getEtudiant()); docWriter.close(); baosPDF.close(); //Creation de l'export byte[] bytes = baosPDF.toByteArray(); return new ByteArrayInputStream(bytes); } catch (DocumentException e) { LOG.error("Erreur la gnration du calendrier des examens : DocumentException ", e); return null; } catch (IOException e) { LOG.error("Erreur la gnration du calendrier des examens : IOException ", e); return null; } } }; // Cration de la ressource StreamResource resource = new StreamResource(source, nomFichier); resource.setMIMEType("application/force-download;charset=UTF-8"); resource.setCacheTime(0); return resource; }
From source file:fr.univlorraine.mondossierweb.controllers.InscriptionController.java
License:Apache License
/** * /*from w w w . j ava2 s.c o m*/ * @return le fichier pdf. */ public com.vaadin.server.Resource exportPdf(Inscription inscription) { // verifie les autorisations if (!etudiantController.proposerCertificat(inscription, MainUI.getCurrent().getEtudiant())) { return null; } String nomFichier = applicationContext.getMessage("pdf.certificat.title", null, Locale.getDefault()) + "_" + inscription.getCod_etp() + "_" + inscription.getCod_anu().replace('/', '-') + "_" + MainUI.getCurrent().getEtudiant().getNom().replace('.', ' ').replace(' ', '_') + ".pdf"; nomFichier = nomFichier.replaceAll(" ", "_"); StreamResource.StreamSource source = new StreamResource.StreamSource() { private static final long serialVersionUID = 1L; @Override public InputStream getStream() { try { ByteArrayOutputStream baosPDF = new ByteArrayOutputStream(OUTPUTSTREAM_SIZE); PdfWriter docWriter = null; Document document = configureDocument(); docWriter = PdfWriter.getInstance(document, baosPDF); docWriter.setEncryption(null, null, PdfWriter.AllowPrinting, PdfWriter.ENCRYPTION_AES_128); docWriter.setStrictImageSequence(true); creerPdfCertificatScolarite(document, MainUI.getCurrent().getEtudiant(), inscription); docWriter.close(); baosPDF.close(); //Creation de l'export byte[] bytes = baosPDF.toByteArray(); return new ByteArrayInputStream(bytes); } catch (DocumentException e) { LOG.error("Erreur la gnration du certificat : DocumentException ", e); return null; } catch (IOException e) { LOG.error("Erreur la gnration du certificat : IOException ", e); return null; } } }; // Cration de la ressource StreamResource resource = new StreamResource(source, nomFichier); resource.setMIMEType("application/force-download;charset=UTF-8"); resource.setCacheTime(0); return resource; }
From source file:fr.univlorraine.mondossierweb.controllers.ListeInscritsController.java
License:Apache License
/** * Retourne le trombinoscope en pdf//from ww w . ja v a 2s .co m * @param linscrits * @param listecodind * @return */ public InputStream getPdfStream(List<Inscrit> linscrits, List<String> listecodind, String libObj, String annee) { LOG.debug("generation pdf : " + libObj + " " + annee + " " + linscrits.size() + " " + listecodind.size()); try { ByteArrayOutputStream baosPDF = new ByteArrayOutputStream(OUTPUTSTREAM_SIZE); PdfWriter docWriter = null; Document document = configureDocument(MARGE_PDF); docWriter = PdfWriter.getInstance(document, baosPDF); docWriter.setEncryption(null, null, PdfWriter.AllowPrinting, PdfWriter.ENCRYPTION_AES_128); docWriter.setStrictImageSequence(true); creerPdfTrombinoscope(document, linscrits, listecodind, libObj, annee); docWriter.close(); baosPDF.close(); //Creation de l'export byte[] bytes = baosPDF.toByteArray(); return new ByteArrayInputStream(bytes); } catch (DocumentException e) { LOG.error("Erreur la gnration du trombinoscope : DocumentException ", e); return null; } catch (IOException e) { LOG.error("Erreur la gnration du trombinoscope : IOException ", e); return null; } }
From source file:fr.univlorraine.mondossierweb.controllers.NoteController.java
License:Apache License
/** * // ww w. j ava2 s. c o m * @return le fichier pdf du rsum des notes. */ public com.vaadin.server.Resource exportPdfResume() { String nomFichier = applicationContext.getMessage("pdf.notes.title", null, Locale.getDefault()) + " " + MainUI.getCurrent().getEtudiant().getNom().replace('.', ' ').replace(' ', '_') + ".pdf"; nomFichier = nomFichier.replaceAll(" ", "_"); StreamResource.StreamSource source = new StreamResource.StreamSource() { private static final long serialVersionUID = 1L; @Override public InputStream getStream() { try { ByteArrayOutputStream baosPDF = new ByteArrayOutputStream(OUTPUTSTREAM_SIZE); PdfWriter docWriter = null; Document document = configureDocument(MARGE_PDF); docWriter = PdfWriter.getInstance(document, baosPDF); docWriter.setEncryption(null, null, PdfWriter.AllowPrinting, PdfWriter.ENCRYPTION_AES_128); docWriter.setStrictImageSequence(true); if (configController.isInsertionFiligranePdfNotes()) { docWriter.setPageEvent(new Watermark()); } creerPdfResume(document, MainUI.getCurrent().getEtudiant()); docWriter.close(); baosPDF.close(); //Creation de l'export byte[] bytes = baosPDF.toByteArray(); return new ByteArrayInputStream(bytes); } catch (DocumentException e) { LOG.error("Erreur la gnration du rsum des notes : DocumentException ", e); return null; } catch (IOException e) { LOG.error("Erreur la gnration du rsum des notes : IOException ", e); return null; } } }; // Cration de la ressource StreamResource resource = new StreamResource(source, nomFichier); resource.setMIMEType("application/force-download;charset=UTF-8"); resource.setCacheTime(0); return resource; }
From source file:fr.univlorraine.mondossierweb.controllers.NoteController.java
License:Apache License
/** * /* w ww . j a v a2 s .c om*/ * @return le fichier pdf du detail des notes. */ public com.vaadin.server.Resource exportPdfDetail(Etape etape) { String nomFichier = applicationContext.getMessage("pdf.detail.title", null, Locale.getDefault()) + " " + MainUI.getCurrent().getEtudiant().getNom().replace('.', ' ').replace(' ', '_') + ".pdf"; nomFichier = nomFichier.replaceAll(" ", "_"); StreamResource.StreamSource source = new StreamResource.StreamSource() { private static final long serialVersionUID = 1L; @Override public InputStream getStream() { try { ByteArrayOutputStream baosPDF = new ByteArrayOutputStream(OUTPUTSTREAM_SIZE); PdfWriter docWriter = null; Document document = configureDocument(MARGE_PDF); docWriter = PdfWriter.getInstance(document, baosPDF); docWriter.setEncryption(null, null, PdfWriter.AllowPrinting, PdfWriter.ENCRYPTION_AES_128); docWriter.setStrictImageSequence(true); if (configController.isInsertionFiligranePdfNotes()) { docWriter.setPageEvent(new Watermark()); } creerPdfDetail(document, MainUI.getCurrent().getEtudiant(), etape); docWriter.close(); baosPDF.close(); //Creation de l'export byte[] bytes = baosPDF.toByteArray(); return new ByteArrayInputStream(bytes); } catch (DocumentException e) { LOG.error("Erreur la gnration du dtail des notes : DocumentException ", e); return null; } catch (IOException e) { LOG.error("Erreur la gnration du dtail des notes : IOException ", e); return null; } } }; // Cration de la ressource StreamResource resource = new StreamResource(source, nomFichier); resource.getStream().setParameter("Content-Disposition", "attachment; filename=" + nomFichier); //resource.setMIMEType("application/unknow"); resource.setMIMEType("application/force-download;charset=UTF-8"); resource.setCacheTime(0); return resource; }