Example usage for com.lowagie.text Paragraph Paragraph

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

Introduction

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

Prototype

public Paragraph(Phrase phrase) 

Source Link

Document

Constructs a Paragraph with a certain Phrase.

Usage

From source file:com.geek.tutorial.itext.bookmarks.Outline.java

License:Open Source License

public Outline() throws Exception {

    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("outline.pdf"));
    document.open();// w w w .  j  ava 2 s.c  om

    // Code 1
    document.add(new Chunk("Chapter 1").setLocalDestination("1"));
    document.newPage();

    document.add(new Chunk("Chapter 2").setLocalDestination("2"));
    document.add(new Paragraph(new Chunk("Sub 2.1").setLocalDestination("2.1")));
    document.add(new Paragraph(new Chunk("Sub 2.2").setLocalDestination("2.2")));
    document.newPage();

    document.add(new Chunk("Chapter 3").setLocalDestination("3"));

    // Code 2
    PdfContentByte cb = writer.getDirectContent();
    PdfOutline root = cb.getRootOutline();

    // Code 3
    PdfOutline oline1 = new PdfOutline(root, PdfAction.gotoLocalPage("1", false), "Chapter 1");

    PdfOutline oline2 = new PdfOutline(root, PdfAction.gotoLocalPage("2", false), "Chapter 2");
    oline2.setOpen(false);
    PdfOutline oline2_1 = new PdfOutline(oline2, PdfAction.gotoLocalPage("2.1", false), "Sub 2.1");
    PdfOutline oline2_2 = new PdfOutline(oline2, PdfAction.gotoLocalPage("2.2", false), "Sub 2.2");

    PdfOutline oline3 = new PdfOutline(root, PdfAction.gotoLocalPage("3", false), "Chapter 3");

    document.close();
}

From source file:com.geek.tutorial.itext.image.SimpleImages.java

License:Open Source License

public SimpleImages() throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("SimpleImages.pdf"));
    document.open();/*from  w  w  w  .ja va2s. c  om*/

    // Code 1
    document.add(new Paragraph("Simple Image"));
    com.lowagie.text.Image image = com.lowagie.text.Image.getInstance("mouse.jpg");
    document.add(image);

    // Code 2
    document.add(new Paragraph("\n" + "AWT Image"));
    java.awt.Image awtImg = java.awt.Toolkit.getDefaultToolkit().createImage("square.jpg");
    com.lowagie.text.Image image2 = com.lowagie.text.Image.getInstance(awtImg, null);
    document.add(image2);
    document.newPage();

    // Code 3
    document.add(new Paragraph("Multipages tiff file"));
    RandomAccessFileOrArray ra = new RandomAccessFileOrArray("multipage.tif");
    int pages = TiffImage.getNumberOfPages(ra);
    for (int i = 1; i <= pages; i++) {
        document.add(TiffImage.getTiffImage(ra, i));
    }
    document.newPage();

    // Code 4
    document.add(new Paragraph("Animated Gifs"));
    GifImage img = new GifImage("bee.gif");
    int frame_count = img.getFrameCount();
    for (int i = 1; i <= frame_count; i++) {
        document.add(img.getImage(i));
    }
    document.close();
}

From source file:com.geek.tutorial.itext.table.PDFTable.java

License:Open Source License

public PDFTable() throws Exception {

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("table_example.pdf"));
    document.open();//from  w  w  w .j a  va2  s.  co  m

    PdfPTable table = new PdfPTable(3);
    //table.setTotalWidth(216f);
    //table.setLockedWidth(true);
    PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
    cell.setColspan(3);
    table.addCell(cell);
    table.addCell("1.1");
    table.addCell("2.1");
    table.addCell("3.1");
    table.addCell("1.2");
    table.addCell("2.2");
    table.addCell("3.2");

    document.add(table);

    document.close();

}

From source file:com.geek.tutorial.itext.table.SimplePDFTableColspan.java

License:Open Source License

public SimplePDFTableColspan() throws Exception {

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("SimplePDFTableColspan.pdf"));
    document.open();/*from www  . j a v a2s .c  o  m*/

    PdfPTable table = new PdfPTable(2);

    PdfPCell cell = new PdfPCell(new Paragraph("column span 2"));
    cell.setColspan(2);
    table.addCell(cell);

    table.addCell("1");
    table.addCell("2");
    table.addCell("3");
    table.addCell("4");
    table.addCell("5");
    table.addCell("6");

    document.add(table);
    document.close();
}

From source file:com.gtdfree.test.PDFTestFont.java

License:Open Source License

/**
 * Fonts and encoding./*from  www .  j  a va 2 s .  c o  m*/
 * @param args no arguments needed
 */
public static void main(String[] args) {

    System.out.println("Encodings");

    String[] names = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();

    System.out.println(Arrays.toString(names));

    /*System.out.println("---");
    try {
    System.getProperties().store(System.out, "");
    } catch (IOException e1) {
    e1.printStackTrace();
    }
    System.out.println("---");*/
    //System.out.println(System.getenv());
    //System.out.println("---");

    //String font= System.getProperty("java.home")+"/lib/fonts/LucidaBrightRegular.ttf";
    //String font= "fonts/DejaVuSans.ttf";

    //byte[] ttf= ApplicationHelper.loadResource(font);

    try {
        // step 1
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream("encodingfont.pdf"));
        // step 3
        document.open();
        // step 4
        String all[] = { "Symbol", "ZapfDingbats" };
        Font hex = new Font(Font.HELVETICA, 5);
        for (int z = 0; z < all.length; ++z) {
            String file = all[z];
            document.add(new Paragraph(
                    "Unicode equivalence for the font \"" + file + "\" with the encoding \"" + file + "\"\n"));
            /*char tb[];
            if (z == 0)
               tb = SYMBOL_TABLE;
            else
               tb = DINGBATS_TABLE;*/
            BaseFont bf;
            if (z == 2) {
                bf = BaseFont.createFont(file, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true, null, null);
                ;
            } else {
                bf = BaseFont.createFont(file, file, true);
            }
            Font f = new Font(bf, 12);
            PdfPTable table = new PdfPTable(16);
            table.setWidthPercentage(100);
            table.getDefaultCell().setBorderWidth(1);
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
            for (char k = 0; k < Character.MAX_VALUE; ++k) {
                char c = k;
                if (bf.charExists(c)) {
                    Phrase ph = new Phrase(12, new String(new char[] { c }), f);
                    ph.add(new Phrase(12, "\n" + Integer.toString(c) + "\n" + cst(c), hex));
                    table.addCell(ph);
                } /*
                  else {
                     Phrase ph = new Phrase("\u00a0");
                     ph.add(new Phrase(12, "\n\n" + cst(c), hex));
                     table.addCell(ph);
                  }*/
            }
            document.add(table);
            document.newPage();
        }
        // step 5
        document.close();

        FontFactory.registerDirectories();

        Set<?> s = FontFactory.getRegisteredFonts();

        System.out.println("Fonts: " + s);

        s = FontFactory.getRegisteredFamilies();

        System.out.println("Families: " + s);

        ArrayList<Font> f = new ArrayList<Font>(s.size());

        for (Object name : s) {

            try {
                f.add(FontFactory.getFont(name.toString(), "UTF-8", true, 12, Font.NORMAL, Color.BLACK, true));
            } catch (Exception e) {
                f.add(FontFactory.getFont(name.toString(), "UTF-8", false, 12, Font.NORMAL, Color.BLACK, true));
            }

        }

        Collections.sort(f, new Comparator<Font>() {
            @Override
            public int compare(Font o1, Font o2) {
                return o1.getFamilyname().compareTo(o2.getFamilyname());
            }
        });

        for (Font ff : f) {

            if (ff.getBaseFont() == null) {
                continue;
            }
            System.out.println(ff.getFamilyname() + " " + ff.getBaseFont().isEmbedded());

        }

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

From source file:com.ideaspymes.proyecttemplate.stock.web.ProductoConsultaBean.java

public String createPdf() throws IOException, DocumentException {

    EtiquetaConf conf = etiquetaConfDAO.getEtiquetaConfDefault();

    if (hayParaImprimir() && conf != null) {

        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance()
                .getExternalContext().getResponse();
        response.setContentType("application/x-pdf");
        response.setHeader("Content-Disposition", "attachment; filename=\"etiquetas.pdf\"");

        float ancho = Utilities.millimetersToPoints(conf.getAnchoHoja().floatValue());
        System.out.println("Ancho: " + ancho);
        float largo = Utilities.millimetersToPoints(conf.getLargoHoja().floatValue());
        System.out.println("Alto: " + largo);

        // step 1
        Document document = new Document(new Rectangle(ancho, largo));
        // step 2

        document.setMargins(0f, 0f, 0f, 0f);

        PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
        // step 3
        document.open();//from   w ww.  j a v  a2 s .c  om

        // step 4
        PdfContentByte cb = writer.getDirectContent();

        for (Producto p : getLista()) {
            if (p.getCantidadEtiquetas() > 0) {

                PdfPTable table = new PdfPTable(2);
                table.setWidthPercentage(96);

                Font fontbold = FontFactory.getFont("Times-Roman", 8, Font.NORMAL);

                Chunk productTitle = new Chunk("HC", fontbold);

                Paragraph pTitile = new Paragraph(productTitle);
                pTitile.setAlignment(Element.ALIGN_LEFT);
                pTitile.setLeading(6, 0);

                PdfPCell cellTitle = new PdfPCell();
                cellTitle.setHorizontalAlignment(Element.ALIGN_LEFT);
                cellTitle.setVerticalAlignment(Element.ALIGN_TOP);
                cellTitle.setBorder(Rectangle.NO_BORDER);
                cellTitle.addElement(pTitile);

                Font font2 = FontFactory.getFont("Times-Roman", conf.getTamDescripcion().floatValue(),
                        Font.NORMAL);

                Chunk productName = new Chunk(p.getNombre(), font2);

                Paragraph pName = new Paragraph(productName);
                pName.setAlignment(Element.ALIGN_CENTER);
                //pTitile.setLeading(0, 1);

                cellTitle.addElement(pName);

                table.addCell(cellTitle);

                Barcode39 code39 = new Barcode39();
                code39.setCode(p.getCodigo());
                //code39.setCodeType(Barcode.EAN13);
                code39.setBarHeight(conf.getAltoCodBarra().floatValue());
                //codeEan.setX(0.7f);
                code39.setSize(conf.getTamDescripcion().floatValue());
                //code39.setAltText("HC - " + p.getNombre());

                PdfPCell cellBarcode = new PdfPCell();
                cellBarcode.setHorizontalAlignment(Element.ALIGN_CENTER);
                cellBarcode.setBorder(Rectangle.NO_BORDER);
                cellBarcode.addElement(code39.createImageWithBarcode(cb, null, Color.BLACK));

                table.addCell(cellBarcode);

                for (int i = 0; i < p.getCantidadEtiquetas(); i++) {
                    document.add(table);
                    document.newPage();
                }
            }
        }

        // step 5
        document.close();

        response.getOutputStream().flush();
        response.getOutputStream().close();
        FacesContext.getCurrentInstance().responseComplete();
    } else {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_ERROR, "No hay nada que imprimir", ""));
    }
    return null;
}

From source file:com.ikon.util.PDFUtils.java

License:Open Source License

public static void generateSample(int paragraphs, OutputStream os) throws DocumentException {
    LoremIpsum li = new LoremIpsum();
    Document doc = new Document(PageSize.A4, 25.0F, 25.0F, 25.0F, 25.0F);
    PdfWriter.getInstance(doc, os);/*w  ww  .ja v  a  2 s. co m*/
    doc.open();

    for (int i = 0; i < paragraphs; i++) {
        doc.add(new Paragraph(li.getParagraphs()));
    }

    doc.close();
}

From source file:com.infinity.controller.PDFView.java

@Override
protected void buildPdfDocument(Map<String, Object> map, Document dcmnt, PdfWriter writer,
        HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {

    List words = (List) map.get("wordList");
    for (Object word : words) {
        dcmnt.add(new Paragraph((String) word));
    }/*from www  .ja  va  2s  . c om*/
}

From source file:com.isdemu.controller.DepreciacionController.java

@RequestMapping(value = "/ExcelReporteInvPersona2", method = RequestMethod.GET)
@ResponseBody/*  ww  w  .j a v a 2s .  com*/

public void getRptAsig2(HttpServletResponse response)
        throws JRException, IOException, SQLException, ClassNotFoundException {
    Document document = new Document();
    try {
        response.setContentType("application/pdf");
        PdfWriter.getInstance(document, response.getOutputStream());
        document.open();
        document.add(new Paragraph("howtodoinjava.com"));
        document.add(new Paragraph(new Date().toString()));
        //Add more content here
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("response :" + response);
    document.close();
}

From source file:com.jhkt.playgroundArena.examples.generic.modelView.PDFView.java

License:Apache License

@Override
protected void buildPdfDocument(Map<String, Object> model, Document doc, PdfWriter writer,
        HttpServletRequest request, HttpServletResponse response) throws Exception {

    @SuppressWarnings(value = "unchecked")
    List<Object> data = (List<Object>) model.get(PDF_DATA_ENTRY_KEY);

    for (Object dataEntry : data) {
        doc.add(new Paragraph(dataEntry.toString()));
    }//from w  ww. ja v  a2  s.  c o m

}