Example usage for com.itextpdf.text.pdf PdfWriter close

List of usage examples for com.itextpdf.text.pdf PdfWriter close

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfWriter close.

Prototype

@Override
public void close() 

Source Link

Document

Signals that the Document was closed and that no other Elements will be added.

Usage

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 {/*from  ww w .j a  v  a2  s.  co  m*/
        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.pearson.controller.PdfGentrate.java

public static void main(String[] args) {

    try {/*from w  w w . j  a va  2s .  c o  m*/
        /*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.propelics.pdfcreator.PdfcreatorModule.java

License:Open Source License

/**
 * @method generatePDFWithHTML//w  w  w  .j  ava  2 s  .  co  m
 * Generates a PDF with the given file name, based on a HTML file
 * @param {String} filename Name of the PDF file
 * @param {String} html String with the HTML
 * @param {String} [author] Author for metadata
 * Fires a "complete" event when the PDF is generated
 * Fires a "error" event when a error is presented
 */
@Kroll.method(runOnUiThread = true)
public void generatePDFWithHTML(final HashMap args) {
    Log.d(PROXY_NAME, "generatePDFWithHTML()");

    String html = "";
    String author = "";
    String filename = "";
    final float marginPt = 28.35f;//1cm == 28.35pt

    try {
        if (args.containsKey("filename")) {
            filename = (String) args.get("filename");
            Log.d(PROXY_NAME, "filename: " + filename);
        } else
            return;

        if (args.containsKey("html")) {
            html = (String) args.get("html");
            Log.d(PROXY_NAME, "html: " + html);
        } else
            return;

        if (args.containsKey("author")) {
            author = (String) args.get("author");
            Log.d(PROXY_NAME, "author: " + author);
        }

        //create a new document
        Document document = new Document(PageSize.LETTER, marginPt, marginPt, marginPt, 0);
        TiBaseFile file = TiFileFactory.createTitaniumFile(filename, true);

        // Parse to XHTML
        StringWriter xhtmlWriter = new StringWriter();
        Tidy tidy = new Tidy();

        // tidy.setXHTML(true);
        tidy.setXmlOut(true);
        tidy.parse(new StringReader(html), xhtmlWriter);
        String xhtml = xhtmlWriter.toString();

        //get Instance of the PDFWriter
        PdfWriter pdfWriter = PdfWriter.getInstance(document, file.getOutputStream());

        //document header attributes
        document.addAuthor(author);
        document.addCreationDate();
        document.setPageSize(PageSize.LETTER);

        //open document
        document.open();

        // From Stack Overflow lol

        MyFontFactory fontFactory = new MyFontFactory();
        FontFactory.setFontImp(fontFactory);

        // HtmlPipelineContext htmlContext = new HtmlPipelineContext(new CssAppliersImpl(fontFactory));
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);
        htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
        CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true);
        Pipeline<?> pipeline = new CssResolverPipeline(cssResolver,
                new HtmlPipeline(htmlContext, new PdfWriterPipeline(document, pdfWriter)));
        XMLWorker worker = new XMLWorker(pipeline, true);
        XMLParser p = new XMLParser(worker);
        p.parse(new StringReader(xhtml));

        // Finish SO c&P

        // Older code
        /*
        // Font Provider creation
        MyFontFactory fontProvider = new MyFontFactory();
        // fontProvider.register("/DroidSans.ttf");
                
        //get the XMLWorkerHelper Instance
        XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
        //convert to PDF
        worker.parseXHtml(pdfWriter, document, new ByteArrayInputStream(xhtml.getBytes("UTF-8")), null, fontProvider); //Load xhtml
        */

        //close the document
        document.close();
        //close the writer
        pdfWriter.close();

        sendCompleteEvent(filename);

    } catch (Exception e) {
        sendErrorEvent(e);
    }
}

From source file:com.solidmaps.webapp.report.EnableCompanyRequerimentFederalPDF.java

public void generate(LicensePFEntity license, EnableRequerimentTypeFederalEnum type,
         EnableRequerimentSubjectFederalEnum subject, UserEntity user) {

     Document doc = new Document();
     PdfWriter docWriter = null;

     try {/*from ww  w  . j  a  va  2 s.  co m*/

         this.createDocument(doc, docWriter, license);
         this.createTable(doc, type, subject, license, user);

     } catch (DocumentException dex) {
         dex.printStackTrace();
     } catch (Exception ex) {
         ex.printStackTrace();
     } finally {
         if (doc != null) {
             // close the document
             doc.close();
         }
         if (docWriter != null) {
             // close the writer
             docWriter.close();
         }
     }

 }

From source file:com.solidmaps.webapp.report.LicenseCivilIncludeProductPDF.java

License:Open Source License

public String generate(LicensePCEntity license, String type, List<LicensePCProductEntity> listProducts) {

    Document doc = new Document();
    PdfWriter docWriter = null;
    String fileDocPath = "";

    try {/*ww w. j  av a  2 s.  co m*/

        fileDocPath = this.createDocument(doc, docWriter, license, type);
        this.createParagraph(doc, license, type, listProducts);

    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            // close the document
            doc.close();
        }
        if (docWriter != null) {
            // close the writer
            docWriter.close();
        }
    }

    return fileDocPath;

}

From source file:com.solidmaps.webapp.report.MapCivilPdfGenerator.java

License:Open Source License

public String createPDF() {

    logger.info("[map ex generate begin] company: " + company.getIdCompany());

    Document doc = new Document();
    PdfWriter docWriter = null;
    String fileName = "";

    try {/*w w  w  .j a v a  2s. c  o m*/

        fileName = this.createDocument(doc, docWriter);
        this.createParagraph(doc);
        this.createProductsTable(doc);
        this.createSignature(doc);
        this.createInvoiceTable(doc);

    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            // close the document
            doc.close();
        }
        if (docWriter != null) {
            // close the writer
            docWriter.close();
        }
    }

    logger.info("[map ex generate] Filename: " + fileName);

    return fileName;
}

From source file:com.solidmaps.webapp.report.MapExercitoPdfGenerator.java

License:Open Source License

public String createPDF() {

    logger.info("[map ex generate begin] company: " + company.getIdCompany());

    Document doc = new Document();
    PdfWriter docWriter = null;
    String fileName = "";

    try {/*from w  ww.j  av  a2s. co m*/

        fileName = this.createDocument(doc, docWriter);
        this.createParagraph(doc);
        this.createProductsTable(doc);
        this.createFooter(doc);

    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            // close the document
            doc.close();
        }
        if (docWriter != null) {
            // close the writer
            docWriter.close();
        }
    }

    logger.info("[map ex generate] Filename: " + fileName);

    return fileName;
}

From source file:com.solidmaps.webapp.report.RenovationRequireExercitoPDF.java

public void generate(LicenseEXEntity license) {

    Document doc = new Document();
    PdfWriter docWriter = null;

    try {/*from  w  w  w.  j  a va 2 s.c  o m*/

        this.createDocument(doc, docWriter, license);
        this.createParagraph(doc, license);

    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            // close the document
            doc.close();
        }
        if (docWriter != null) {
            // close the writer
            docWriter.close();
        }
    }

}

From source file:com.solidmaps.webapp.report.RequerimentAlterLicenseExercitoPDF.java

/**
 * /*  ww  w.  java  2  s .c om*/
 * @param license 
 *       Licensa vigente do Exrcito
 * @param listCharterType 
 *       Lista de Atividades a incluir
 * @param listProducts
 *       Lista de Produtos a incluir, bem como os limites de Armazenamento
 * @param text
 *       Texto sobre a altees Cadastrais
 * @param type
 *       Tipo de alterao cadastral (Endereo, Responsvel, etc)
 */
public String generate(LicenseEXEntity license, List<CharterTypeEntity> listCharterType,
        List<LicenseExCharterProductEntity> listProducts, String text, String type) {

    Document doc = new Document();
    PdfWriter docWriter = null;
    String fileDocPath = "";

    try {

        fileDocPath = this.createDocument(doc, docWriter, license);
        this.createParagraph(doc, license);
        this.createCharter(doc, license, listCharterType);
        this.createIncludeProduct(doc, license, listProducts);
        this.createAlterLicense(doc, license, text, type);
        this.createSignature(doc, license);

    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            // close the document
            doc.close();
        }
        if (docWriter != null) {
            // close the writer
            docWriter.close();
        }
    }

    return fileDocPath;

}

From source file:com.solidmaps.webapp.report.RequerimentAlterLicenseFederalPDF.java

public String generate(LicensePFEntity license, RequerimentFederalEnum type,
        List<ProductOfficialEntity> listProducts) {

    if (listProducts == null || listProducts.isEmpty()) {
        return null;
    }/*from  w  ww .  j a v a  2  s .co m*/

    Document doc = new Document();
    PdfWriter docWriter = null;
    String fileDocPath = "";

    try {

        fileDocPath = this.createDocument(doc, docWriter, license);
        this.createTable(doc, type, license, listProducts);

    } catch (DocumentException dex) {
        dex.printStackTrace();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (doc != null) {
            // close the document
            doc.close();
        }
        if (docWriter != null) {
            // close the writer
            docWriter.close();
        }
    }

    return fileDocPath;
}