Example usage for com.lowagie.text Document Document

List of usage examples for com.lowagie.text Document Document

Introduction

In this page you can find the example usage for com.lowagie.text Document Document.

Prototype


public Document() 

Source Link

Document

Constructs a new Document -object.

Usage

From source file:EmailWithPdf.java

/**
 * Writes the content of a PDF file (using iText API)
 * to the {@link OutputStream}.//ww  w .  j  a  v  a 2s .c  om
 * @param outputStream {@link OutputStream}.
 * @throws Exception
 */
public void writePdf(OutputStream outputStream) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, outputStream);

    document.open();

    document.addTitle("Test PDF");
    document.addSubject("Testing email PDF");
    document.addKeywords("iText, email");
    document.addAuthor("Jee Vang");
    document.addCreator("Jee Vang");

    Paragraph paragraph = new Paragraph();
    paragraph.add(new Chunk("hello!"));
    paragraph.add(new Chunk("hello!"));
    paragraph.add(new Chunk("hello!"));
    paragraph.add(new Chunk("hello!"));
    paragraph.add(new Chunk("hello!"));
    paragraph.add(new Chunk("hello!"));
    paragraph.add(new Chunk("hello!"));
    paragraph.add(new Chunk("hello!"));
    paragraph.add(new Chunk("hello!"));
    document.add(paragraph);

    document.close();
}

From source file:FirstPdf.java

public static void main(String[] args) {
    try {/*from   w  w  w. j a  v a  2 s. com*/
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(FILE));
        document.open();
        addMetaData(document);
        addTitlePage(document);
        addContent(document);
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:displayStructureAsPDFTable.java

License:Open Source License

public displayStructureAsPDFTable(boolean withH, int width, int height, double scale, int ncol, boolean props,
        boolean doColor, String odir) {
    this.withH = withH;
    this.width = width;
    this.height = height;
    this.scale = scale;
    this.odir = odir;
    this.props = props;
    this.ncol = ncol;
    this.doColor = doColor;

    suffix = ".pdf";
    oformat = "PDF";

    decimalFormat = new DecimalFormat("0.000E00");
    this.frame = new JFrame();

    try {/*from  ww w  .j a va2 s  . co m*/
        File file = new File(odir + "/output" + this.suffix);
        document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));

        float[] widths = new float[ncol];
        for (int i = 0; i < ncol; i += 2) {
            if (props) {
                widths[i] = 3f;
                widths[i + 1] = 2f;
            } else {
                widths[i] = 1.5f;
                widths[i + 1] = 3f;
            }
        }
        table = new PdfPTable(widths);
        document.open();
    } catch (Exception exc) {
        exc.printStackTrace();
    }
}

From source file:AppletViewer.java

License:Open Source License

/**
 * Metodo que crea un PDF a partir del archivo TIF con sus anotaciones
 * @param docid//  w  w  w .j  a  va 2 s  .  c  o m
 */
public void convertDocument(String docid) {
    try {
        File dir = new File(descargados);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        dir = new File(transferencias);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        // CARGAR TIF CON ANOTACIONES
        String tmpFileName = "TMP" + String.valueOf(System.currentTimeMillis()) + ".tif";
        OutputStream output = new FileOutputStream(descargados + File.separator + tmpFileName);
        myGenDocViewer.exportDocument(output, true, false, "image/tiff");
        output.close();

        // CONVERTIR TIF A PDF
        RandomAccessFileOrArray myTiffFile = new RandomAccessFileOrArray(
                descargados + File.separator + tmpFileName);
        int numberOfPages = TiffImage.getNumberOfPages(myTiffFile);
        Document TifftoPDF = new Document();
        TifftoPDF.setMargins(0, 0, 0, 0);
        PdfWriter.getInstance(TifftoPDF,
                new FileOutputStream(transferencias + File.separator + type + "-AN.pdf"));
        TifftoPDF.open();
        for (int i = 1; i <= numberOfPages; i++) {
            Image tempImage = TiffImage.getTiffImage(myTiffFile, i);
            float dpiX = tempImage.getDpiX() == 0 ? 200 : tempImage.getDpiX();
            float dpiY = tempImage.getDpiY() == 0 ? 200 : tempImage.getDpiY();
            float factorX = 72 / dpiX;
            float factorY = 72 / dpiY;
            tempImage.scaleAbsolute(factorX * tempImage.getWidth(), factorY * tempImage.getHeight());
            TifftoPDF.add(tempImage);
        }
        myTiffFile.close();
        TifftoPDF.close();

        // BORRAR EL TIFF
        File file = new File(descargados + File.separator + tmpFileName);
        if (file.delete()) {
            System.out.println(file.getName() + " eliminado");
        } else {
            file.deleteOnExit();
            ;
            System.out.println("Eliminacion fallo");
        }

        System.out.println("Fin de la creacion del PDF con anotaciones");

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:ITextHelloWorld.java

License:Open Source License

public ITextHelloWorld() throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
    document.open();/*www.j  a v  a  2  s. c  o m*/
    document.add(new Paragraph("Hello World"));
    document.close();

}

From source file:airportpainter.util.AirportPainter.java

License:Open Source License

public static void writeAirportImage(Airport airport, String filename, String backgroundColor_,
        boolean drawObjectsInAirport, boolean drawObjectsInMap, boolean drawTerrainInMap) {
    //Image image = new Image( Display.getCurrent(),400, 400);

    String pdfName = filename + ".pdf";
    /*String pngName1=filename+"_airport.png";
    String pngName2=filename+"_map.png";*/

    backgroundColor = backgroundColor_;//from  ww  w  .jav a2  s  .  c  o  m

    final AirportPainter ap = new AirportPainter(airport);

    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gs = ge.getDefaultScreenDevice();
    GraphicsConfiguration gc = gs.getDefaultConfiguration();

    width = 520;
    height = ((int) PageSize.A4.height()) - 150 - 30 * airport.getRunwayCount() - 100;
    //int height=740;

    //BufferedImage bimage = gc.createCompatibleImage(width, height, Transparency.TRANSLUCENT);

    //Graphics2D g2d = bimage.createGraphics();
    Document document = new Document();

    PdfWriter writer;
    try {
        try {

            writer = PdfWriter.getInstance(document, new FileOutputStream(pdfName));
            document.open();
            g2d = writer.getDirectContent().createGraphicsShapes(PageSize.A4.width(), PageSize.A4.height());
            ap.drawAirport(g2d, 0, 0, width, height - 30, drawObjectsInAirport);
            ap.createSheets1(airport, writer, document, pdfName, drawObjectsInMap, drawTerrainInMap);
        } catch (DocumentException ex) {
            Logger.getLogger(AirportPainter.class.getName()).log(Level.SEVERE, null, ex);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(AirportPainter.class.getName()).log(Level.SEVERE, null, ex);
    }

    /*
    if (! backgroundColor.equals("")) {
            
    g2d.setColor(Color.decode("0x"+backgroundColor));
    Polygon p = new Polygon();
    p.addPoint(0, 0);
    p.addPoint(width, 0);
    p.addPoint(width, height);
    p.addPoint(0, height);
    p.addPoint(0, 0);
    g2d.fillPolygon(p);
            
    }
            
    System.out.print("Creating:"+pngName1+" ... ");
    */
    /*
    File outputfile = new File(pngName1);
    try {
    ImageIO.write(bimage, "png", outputfile);
    } catch(Exception e) {
    System.out.println(e);
    }
            
    */

    /*// doing PDF now
    Document document = new Document();
    try {
            
            
            
    System.out.println(pdfName+" OK");
    PdfWriter.getInstance(document, new FileOutputStream(pdfName));
            
    // step 3: we open the document
    document.open();
            
    // step 4: we add some paragraphs to the document
    document.add(
        new Paragraph(airport.getId()+" - "+airport.getName())
    );
            
    Image im;
    try {
        im = Image.getInstance(filename);
            
        document.add(im);
            
         // step 5: we close the document
        document.close();
            
    } catch (BadElementException ex) {
        Logger.getLogger(AirportPainter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (MalformedURLException ex) {
        Logger.getLogger(AirportPainter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(AirportPainter.class.getName()).log(Level.SEVERE, null, ex);
    }
            
            
            
            
    } catch (FileNotFoundException ex) {
    Logger.getLogger(AirportPainter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
    Logger.getLogger(AirportPainter.class.getName()).log(Level.SEVERE, null, ex);
    }
    */
    /*if (! keepPng) {
    outputfile.delete();
    }*/

}

From source file:at.htlpinkafeld.beans.UserDetailsBean.java

public void createPDF() {
        try {/*from  w ww  .  ja v a2  s. c  o m*/
            FacesContext context = FacesContext.getCurrentInstance();
            Document document = new Document();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PdfWriter.getInstance(document, baos);

            if (!document.isOpen()) {
                document.open();
            }

            preProcessPDF(document);

            PdfPTable pdfTable = exportPDFTable();
            document.add(pdfTable);

            postProcessPDF(document);

            //Keep modifying your pdf file (add pages and more)
            document.close();
            String fileName = "Monatszeiten";

            writePDFToResponse(context.getExternalContext(), baos, fileName);

            context.responseComplete();
        } catch (DocumentException ex) {
            Logger.getLogger(UserDetailsBean.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

From source file:at.reppeitsolutions.formbuilder.components.pdf.renderkit.PdfResponseWriter.java

License:Open Source License

public void startDocumentInternal(OutputStream outputStream) throws IOException {
    document = new Document();

    try {/*ww  w.  ja va2s . c  o  m*/
        PdfWriter.getInstance(document, outputStream);
    } catch (DocumentException ex) {
        throw new IOException(ex);
    }
    document.open();
    document.newPage();
}

From source file:at.tugraz.sss.serv.SSFileU.java

License:Apache License

public static void writePDFFromText(final String pdfFilePath, final String textFilePath) throws Exception {

    OutputStream out = null;//  w  ww .  j  a  v a 2 s  . c  o m
    BufferedReader br = null;

    try {

        out = openOrCreateFileWithPathForWrite(pdfFilePath);

        final Document document = new Document();
        final PdfWriter writer = PdfWriter.getInstance(document, out);
        String line;

        document.open();
        writer.setPageEmpty(true);
        document.newPage();
        writer.setPageEmpty(true);

        br = new BufferedReader(new FileReader(new File(textFilePath)));

        while ((line = br.readLine()) != null) {
            document.add(new Paragraph(line));
        }

        document.close();

    } catch (Exception error) {

        if (out != null) {
            out.close();
        }

        if (br != null) {
            br.close();
        }
    }
}

From source file:at.tugraz.sss.serv.SSFileU.java

License:Apache License

public static void writePDFFromDoc(final String docFilePath, final String pdfFilePath) throws Exception {

    final Document document = new Document();
    final POIFSFileSystem fs = new POIFSFileSystem(openFileForRead(docFilePath));
    final HWPFDocument word = new HWPFDocument(fs);
    final WordExtractor we = new WordExtractor(word);
    final OutputStream out = openOrCreateFileWithPathForWrite(pdfFilePath);
    final PdfWriter writer = PdfWriter.getInstance(document, out);
    final Range range = word.getRange();

    document.open();//  w ww  .java  2s .co m
    writer.setPageEmpty(true);
    document.newPage();
    writer.setPageEmpty(true);

    String[] paragraphs = we.getParagraphText();

    for (int i = 0; i < paragraphs.length; i++) {

        org.apache.poi.hwpf.usermodel.Paragraph pr = range.getParagraph(i);
        // CharacterRun run = pr.getCharacterRun(i);
        // run.setBold(true);
        // run.setCapitalized(true);
        // run.setItalic(true);
        paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", "");
        System.out.println("Length:" + paragraphs[i].length());
        System.out.println("Paragraph" + i + ": " + paragraphs[i].toString());

        // add the paragraph to the document
        document.add(new Paragraph(paragraphs[i]));
    }

    document.close();
}