Example usage for com.itextpdf.text Document Document

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

Introduction

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

Prototype


public Document() 

Source Link

Document

Constructs a new Document -object.

Usage

From source file:com.mycompany.mavenproject2.ItemHistoryController.java

@FXML
public void OnPrintButtonAction(ActionEvent a) {
    Document document = new Document();
    //JavaPdfHelloWorld jp=new JavaPdfHelloWorld();

    try {//from   ww w.  jav  a2s  . c  o  m
        PdfWriter.getInstance(document, new FileOutputStream("javaHello.pdf"));

        document.open();
        document.add(new Paragraph("A Hello World PDF document."));

        document.close();

        final File file = new File(
                "C:/Users/Third Ev/Documents/NetBeansProjects/FinalDemo/src/finaldemo/Controller/javaHello.pdf");

        f4.getHostServices().showDocument(file.toURI().toString());

        // no need to close PDFwriter?
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

}

From source file:com.mycompany.mavenproject2.JavaPdfHelloWorld.java

public static void main(String[] args) {
    Document document = new Document();
    try {//from   w  ww .j a  v  a  2 s.  c o m
        PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));

        document.open();
        document.add(new Paragraph("A Hello World PDF document."));
        document.close(); // no need to close PDFwriter?

    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:com.mycompany.mavenproject2.VirtualkeyController.java

@FXML
public void handlePDFButtonAction(ActionEvent a) throws IOException, DocumentException {

    Document document = new Document();
    FileOutputStream f2 = new FileOutputStream(FILE1);
    PdfWriter.getInstance(document, f2);
    document.open();/*from w  w w .  jav  a  2  s .c om*/
    addMetaData(document);
    addTitlePage(document);
    //      addContent(document);
    // document.close();
    /* PdfReader reader = new PdfReader("C:/Users/Third Ev/Downloads/my.pdf");
    System.out.println("This PDF has "+reader.getNumberOfPages()+" pages.");
    String page = PdfTextExtractor.getTextFromPage(reader, 2);
    System.out.println("Page Content:\n\n"+page+"\n\n");
    System.out.println("Is this document tampered: "+reader.isTampered());
    System.out.println("Is this document encrypted: "+reader.isEncrypted());*/
    /* PdfDecoder pdf = new PdfDecoder();
     pdf.openPdfFile("C:/Users/Third Ev/Downloads/my.pdf");*/
    /*  hostServices = getHostServices();
        hostServices.showDocument("C:/Users/Third Ev/Documents/NetBeansProjects/FinalDemo/src/finaldemo/Controller/iText.pdf");*/
    document.close();
    f2.close();
    final File file = new File("/home/drashti/MyDemo.pdf");

    f4.getHostServices().showDocument(file.toURI().toString());

}

From source file:com.nerdcastle.nazmul.filetest.MainActivity.java

private void createPdf() {
    try {/*from  w  w w . j  a v a  2s.  co  m*/
        Document document = new Document();
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/created_pdf");
        myDir.mkdirs();
        String fname = "FirstPdf.pdf";
        File file = new File(myDir, fname);
        //FileOutputStream out = new FileOutputStream(file);
        PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        addMetaData(document);
        addTitlePage(document);
        addContent(document);
        document.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.nlbhub.nlb.domain.export.hypertext.document.PDFDocument.java

License:Open Source License

public PDFDocument(String encoding, String lineSeparator) {
    m_document = new Document();
}

From source file:com.norbsoft.pdfconverter.helpers.PDFHelper.java

License:Open Source License

public int generate(String saveUrl, SerializableBitmap sign, Form form, ArrayList<String> photos,
        ArrayList<String> workers) {
    try {/* w  ww.  j  av a  2s.c  om*/
        Document document = new Document();

        url = saveUrl;
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(url));
        document.setMargins(20, 20, 20, 20);
        document.open();

        Bitmap inputLogo = BitmapFactory.decodeStream(context.getAssets().open("logo.jpg"));
        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
        inputLogo.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
        Image logo = Image.getInstance(outstream.toByteArray());

        Bitmap inputQR = BitmapFactory.decodeStream(context.getAssets().open("qr.jpg"));
        outstream = new ByteArrayOutputStream();
        inputQR.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
        Image qr = Image.getInstance(outstream.toByteArray());

        outstream = new ByteArrayOutputStream();
        sign.getImage().compress(Bitmap.CompressFormat.JPEG, 100, outstream);
        Image sgn = Image.getInstance(outstream.toByteArray());

        qr.scaleAbsolute(90f, 90f);
        qr.setAbsolutePosition(340f, 705f);

        logo.scaleAbsolute(150f, 50f);
        logo.setAbsolutePosition(50f, 750f);

        sgn.scaleAbsolute(130f, 30f);
        sgn.setAbsolutePosition(25f, 75f);

        Paragraph p = new Paragraph("\r\n\r\n\r\nMPWiK S.A.\r\n50-421 Wrocaw\r\nul.Na Grobli 14-16", normal);
        p.setIndentationLeft(420f);
        document.add(p);

        p = new Paragraph("RAPORT WYMIANY WODOMIERZA", bold);
        p.setAlignment(Element.ALIGN_CENTER);
        p.setSpacingBefore(60f);
        document.add(p);

        document.add(table(form));

        p = new Paragraph(form.getInformations(), normal);
        p.setAlignment(Element.ALIGN_LEFT);
        p.setIndentationLeft(50f);
        p.setSpacingBefore(20f);
        document.add(p);

        document.add(signTable(form, sgn, workers));

        document.add(logo);
        document.add(qr);

        if (photos != null && photos.size() > 0) {
            document.newPage();
            for (int i = 0; i < photos.size(); i++) {
                try {
                    if (photos.get(i) != null && !photos.get(i).equals("")) {
                        Log.d(TAG, photos.get(i));
                        Bitmap temp = PictureHelper.bitmapFromUrl(photos.get(i));
                        outstream = new ByteArrayOutputStream();
                        temp.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
                        Image photo = Image.getInstance(outstream.toByteArray());
                        photo.scaleToFit(document.getPageSize().getWidth() - 50,
                                document.getPageSize().getHeight());
                        document.add(photo);
                    }
                } catch (Exception e) {

                }
            }
        }
        document.close();
        writer.close();
        return 1;
    } catch (FileNotFoundException e) {
        Log.e(TAG, e.getMessage());
        return 0;
    } catch (DocumentException e) {
        Log.e(TAG, e.getMessage());
        return 0;
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
        return 0;
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
        return 0;
    }
}

From source file:com.pdf.GetPdf.java

private void convertToPdf(String url, ServletOutputStream out) {

    try {//  w  w w  .j ava2 s. c o m
        String contentType = new URL(url).openConnection().getContentType();

        Document document = new Document();
        PdfWriter.getInstance(document, out);
        document.open();
        if (contentType.equalsIgnoreCase("application/vnd.ms-excel")) {
            addXls(document, url, "xls");

        } else if (contentType
                .equalsIgnoreCase("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
            addXls(document, url, "xlsx");
        } else if (contentType.equalsIgnoreCase("application/msword")) {
            docConvert(document, url, "doc");
        } else if (contentType
                .equalsIgnoreCase("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
            docConvert(document, url, "docx");
        } else if (contentType.equalsIgnoreCase("image/tiff")) {
            addTif(document, url);
        } else if (contentType.equalsIgnoreCase("image/gif")) {
            addGif(document, url);
        } else {
            Image image = Image.getInstance(url);
            image.scaleToFit(550, 800);
            document.add(image);

        }
        document.close();
    }

    catch (MalformedURLException ex) {
        Logger.getLogger(GetPdf.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(GetPdf.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(GetPdf.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.pdg.ticket.utils.FirstPdf.java

public void createPdfFile() {
    Document document = new Document();
    try {/*  w  ww. j  a  v  a 2  s  .c  o  m*/
        PdfWriter.getInstance(document, new FileOutputStream(getOutputMediaFile(1)));
        document.open();
        //addMetaData(document);
        //addTitlePage(document);
        addContent(document);
        document.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.pearson.controller.PdfGentrate.java

public static void main(String[] args) {

    try {/*from ww  w. j  av  a2s.c  om*/
        /*Document document = new Document(PageSize.
        A4, 50, 50, 50, 50);
        PdfWriter writer = PdfWriter.getInstance
        (document, new FileOutputStream("C:\\my.pdf"));
        document.open();               
        // create a chunk object using chunk class  of itext library.
        Chunk underlined = new Chunk("Welcome to Pearson Q-Service Portal : ");   
        // set the distance between text and line.
        underlined.setTextRise(8.0f);      
        // set the width of the line, 'y' position,  color and design of the line
        underlined.setUnderline(new Color(0x00, 0x00, 0xFF),0.0f, 0.2f, 3.0f, 0.0f, PdfContentByte.LINE_CAP_PROJECTING_SQUARE);      // finally add object to the document.
        document.add(underlined);
        document.add(new Paragraph("Hi username",
        FontFactory.getFont(FontFactory.COURIER, 14, 
        Font.BOLD, new Color(255, 150, 200))));;
                 
                
             document.add(new Paragraph("Tiltle",catFont));
             document.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
               smallBold));
             document.add(new Paragraph("This document is a preliminary version and not subject to your license agreement or any other agreement with vogella.com ;-).",
               redFont));
                     
             PdfPTable table = new PdfPTable(3); // 3 columns.   
            table.setWidthPercentage(100); //Width 100%     
            table.setSpacingBefore(10f); //Space before table    
             table.setSpacingAfter(10f); //Space after table         //Set Column widths     
            float[] columnWidths = {1f, 1f, 1f};     
            table.setWidths(columnWidths);    
            PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));     
            cell1.setBorderColor(BaseColor.BLUE);     
            cell1.setPaddingLeft(10);     
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);     
            cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);    
            PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));    
          cell2.setBorderColor(BaseColor.GREEN);    
          cell2.setPaddingLeft(10);    
          cell2.setHorizontalAlignment(Element.ALIGN_CENTER);      
            cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);      
            PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));   
            cell3.setBorderColor(BaseColor.RED);      
            cell3.setPaddingLeft(10);      
            cell3.setHorizontalAlignment(Element.ALIGN_CENTER);    
          cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);   
            //To avoid having the cell border and the content overlap, if you are having thick cell borders     
            //cell1.setUserBorderPadding(true);     
            //cell2.setUserBorderPadding(true);   
            //cell3.setUserBorderPadding(true);   
            table.addCell(cell1);   
          table.addCell(cell2);   
          table.addCell(cell3);     
             document.add(table);     
             document.close();     
             writer.close();
                    
                  
                
        document.close();
        } 
        catch (Exception e2) {
        System.out.println(e2.getMessage());
        }
        }*/

        Document document = new Document();

        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("AddTableExample.pdf"));
        document.open();
        PdfPTable table = new PdfPTable(3); // 3 columns.      
        table.setWidthPercentage(100); //Width 100%       
        table.setSpacingBefore(10f); //Space before table    
        table.setSpacingAfter(10f); //Space after table         //Set Column widths     
        float[] columnWidths = { 1f, 1f, 1f };
        table.setWidths(columnWidths);
        PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
        cell1.setBorderColor(BaseColor.BLUE);
        cell1.setPaddingLeft(10);
        cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
        PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
        cell2.setBorderColor(BaseColor.GREEN);
        cell2.setPaddingLeft(10);
        cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
        PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));
        cell3.setBorderColor(BaseColor.RED);
        cell3.setPaddingLeft(10);
        cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
        //To avoid having the cell border and the content overlap, if you are having thick cell borders        //cell1.setUserBorderPadding(true);        //cell2.setUserBorderPadding(true);   
        //cell3.setUserBorderPadding(true);    
        table.addCell(cell1);
        table.addCell(cell2);
        table.addCell(cell3);
        document.add(table);
        document.close();
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.photon.phresco.framework.docs.impl.DocConvertor.java

License:Apache License

/**
 * @param fileUrl/*from ww w. ja v  a2 s .  c om*/
 * @return
 * @throws FileNotFoundException
 * @throws IOException
 * @throws DocumentException
 */
private static PdfInput convertPdf(String fileUrl) throws IOException, DocumentException {
    if (isDebugEnabled) {
        S_LOGGER.debug("Entering Method DocConvertor.convertPdf(String fileUrl)");
    }
    PdfReader reader = new PdfReader(new FileInputStream(fileUrl));
    int numberOfPages = reader.getNumberOfPages();
    Document doc = new Document();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    PdfCopy copy = new PdfCopy(doc, os);
    doc.open();

    //page number in PDF starts at 1
    for (int i = 1; i <= numberOfPages; i++) {
        copy.addPage(copy.getImportedPage(reader, i));
    }
    doc.close();
    ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
    os.close();
    PdfInput input = new PdfInput();
    input.setInputStream(new FileInputStream(fileUrl));
    return input;
}