Example usage for com.lowagie.text Image MIDDLE

List of usage examples for com.lowagie.text Image MIDDLE

Introduction

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

Prototype

int MIDDLE

To view the source code for com.lowagie.text Image MIDDLE.

Click Source Link

Document

this is a kind of image alignment.

Usage

From source file:de.xirp.report.ReportGenerator.java

License:Open Source License

/**
 * Adds a//  w w w. ja v  a  2  s .  c  o m
 * {@link de.xirp.report.data.ContentPartImage image}
 * item to the PDF {@link com.lowagie.text.Document}.
 * 
 * @param item
 *            The image item to add.
 * @throws IOException
 *             if something went wrong saving the PDF.
 * @throws DocumentException
 *             if something went wrong adding the page.
 * @throws MalformedURLException
 *             if something went wrong saving the PDF.
 * @see de.xirp.report.data.ContentPartImage
 * @see com.lowagie.text.Document
 */
private static void addImage(ContentPartImage item)
        throws MalformedURLException, IOException, DocumentException {

    imageCounter++;

    String path = ""; //$NON-NLS-1$
    File file = new File(item.getPath());
    if (file.exists()) {
        path = item.getPath();
    }

    Image image = Image.getInstance(path);
    image.setAlignment(Image.MIDDLE);
    int maxWidth = 500;
    float plain = image.plainWidth();
    if (plain > maxWidth) {
        float percentage = maxWidth / plain;
        image.scalePercent(percentage * 100);
    }
    document.add(image);
    document.add(getParagraph(I18n.getString("ReportGenerator.report.document.image") //$NON-NLS-1$
            + " " + imageCounter + ": " + item.getShortDescription(), //$NON-NLS-1$ //$NON-NLS-2$
            IMAGE, Element.ALIGN_CENTER));

}

From source file:it.prato.comune.tolomeo.web.TolomeoPrintPDFServlet.java

License:Open Source License

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    LogInterface logger = getLogger(request);

    String titolo = request.getParameter("titolo");
    String descrizione = request.getParameter("descrizione");
    String scala = request.getParameter("scala");
    String mapx = request.getParameter("mapx");
    String mapy = request.getParameter("mapy");
    //creo URL alla mappa
    URL urlMappa = new URL(URLDecoder.decode(request.getParameter("urlMappa"), "UTF-8"));
    URI uriMappa = null;//from w  ww. j  a va  2s. c  o  m
    try {
        uriMappa = new URI(urlMappa.getProtocol(), null, urlMappa.getHost(), urlMappa.getPort(),
                urlMappa.getPath(), urlMappa.getQuery() + "&mode=map&scale=" + scala + "&mapxy=" + mapx + "+"
                        + mapy + "&map_size=500 500",
                null);
        logger.info("URI di stampa mappa: " + uriMappa);
    } catch (URISyntaxException e) {
        logger.error("URI errore di sintassi", e);
    }

    Font BOLD15 = new Font(Font.HELVETICA, 15, Font.BOLD);
    Font FONT12 = new Font(Font.HELVETICA, 12, Font.NORMAL);
    Font FONT10 = new Font(Font.HELVETICA, 10, Font.NORMAL);

    Document doc = new Document();
    ;
    PdfWriter pdf = null;
    Paragraph par = new Paragraph();

    try {

        //creo il PDF
        response.setContentType("application/pdf");
        pdf = PdfWriter.getInstance(doc, response.getOutputStream());

        //attributi file
        doc.addTitle("Mappa di Prato");
        doc.addAuthor("Comune di Prato");
        doc.open();

        //intestazione
        par.setAlignment(Paragraph.ALIGN_CENTER);
        print("Comune di Prato", BOLD15, par, doc);
        print(titolo, FONT10, par, doc);
        print("", FONT10, par, doc);

        //mappa
        Image mappa = Image.getInstance(uriMappa.toURL());
        mappa.setAlignment(Image.MIDDLE);
        mappa.setBorder(Rectangle.BOX);
        mappa.setBorderWidth(1f);
        doc.add(mappa);

    } catch (Exception e) {
        logger.error("Errore durante la creazione del PDF", e);
        printErr(doc);
    } finally {
        doc.close();
        pdf.close();
        response.getOutputStream().close();
    }
}

From source file:nl.knaw.dans.common.lang.pdf.ExtendedHtmlWorker.java

License:Apache License

public void startElement(String tag, HashMap h) {
    if (!tagsSupported.containsKey(tag))
        return;//  www .  j  av a2  s  . c  om
    try {
        style.applyStyle(tag, h);
        String follow = (String) FactoryProperties.followTags.get(tag);
        if (follow != null) {
            HashMap prop = new HashMap();
            prop.put(follow, null);
            cprops.addToChain(follow, prop);
            return;
        }
        FactoryProperties.insertStyle(h);
        if (tag.equals("a")) {
            cprops.addToChain(tag, h);
            if (currentParagraph == null)
                currentParagraph = new Paragraph();
            stack.push(currentParagraph);
            currentParagraph = new Paragraph();
            return;
        }
        if (tag.equals("br")) {
            if (currentParagraph == null)
                currentParagraph = new Paragraph();
            currentParagraph.add(factoryProperties.createChunk("\n", cprops));
            return;
        }
        if (tag.equals("font") || tag.equals("span")) {
            cprops.addToChain(tag, h);
            return;
        }
        if (tag.equals("img")) {
            String src = (String) h.get("src");
            if (src == null)
                return;
            cprops.addToChain(tag, h);
            Image img = null;
            if (interfaceProps != null) {
                ImageProvider ip = (ImageProvider) interfaceProps.get("img_provider");
                if (ip != null)
                    img = ip.getImage(src, h, cprops, document);
                if (img == null) {
                    HashMap images = (HashMap) interfaceProps.get("img_static");
                    if (images != null) {
                        Image tim = (Image) images.get(src);
                        if (tim != null)
                            img = Image.getInstance(tim);
                    } else {
                        if (!src.startsWith("http")) { // relative src references only
                            String baseurl = (String) interfaceProps.get("img_baseurl");
                            if (baseurl != null) {
                                src = baseurl + src;
                                img = Image.getInstance(src);
                            }
                        }
                    }
                }
            }
            if (img == null) {
                if (!src.startsWith("http")) {
                    String path = cprops.getProperty("image_path");
                    if (path == null)
                        path = "";
                    src = new File(path, src).getPath();
                }
                img = Image.getInstance(src);
            }
            String align = (String) h.get("align");
            String width = (String) h.get("width");
            String height = (String) h.get("height");
            String before = cprops.getProperty("before");
            String after = cprops.getProperty("after");
            if (before != null)
                img.setSpacingBefore(Float.parseFloat(before));
            if (after != null)
                img.setSpacingAfter(Float.parseFloat(after));
            float wp = lengthParse(width, (int) img.getWidth());
            float lp = lengthParse(height, (int) img.getHeight());
            if (wp > 0 && lp > 0)
                img.scalePercent(wp > lp ? lp : wp);
            else if (wp > 0)
                img.scalePercent(wp);
            else if (lp > 0)
                img.scalePercent(lp);
            img.setWidthPercentage(0);
            if (align != null) {
                endElement("p");
                int ralign = Image.MIDDLE;
                if (align.equalsIgnoreCase("left"))
                    ralign = Image.LEFT;
                else if (align.equalsIgnoreCase("right"))
                    ralign = Image.RIGHT;
                img.setAlignment(ralign);
                Img i = null;
                boolean skip = false;
                if (interfaceProps != null) {
                    i = (Img) interfaceProps.get("img_interface");
                    if (i != null)
                        skip = i.process(img, h, cprops, document);
                }
                if (!skip)
                    document.add(img);
                cprops.removeChain(tag);
            } else {
                cprops.removeChain(tag);
                if (currentParagraph == null)
                    currentParagraph = FactoryProperties.createParagraph(cprops);
                currentParagraph.add(new Chunk(img, 0, 0));
            }
            return;
        }
        endElement("p");
        if (tag.equals("h1") || tag.equals("h2") || tag.equals("h3") || tag.equals("h4") || tag.equals("h5")
                || tag.equals("h6")) {
            if (!h.containsKey("size")) {
                int v = 7 - Integer.parseInt(tag.substring(1));
                h.put("size", Integer.toString(v));
            }
            cprops.addToChain(tag, h);
            return;
        }
        if (tag.equals("ul")) {
            if (pendingLI)
                endElement("li");
            skipText = true;
            cprops.addToChain(tag, h);
            com.lowagie.text.List list = new com.lowagie.text.List(false, 10);
            list.setListSymbol("\u2022");
            String indent = ((String) h.get("indent"));
            if (indent != null && indent.matches("[0-9]+")) {
                list.setSymbolIndent(Integer.valueOf(indent));
            }
            stack.push(list);
            return;
        }
        if (tag.equals("ol")) {
            if (pendingLI)
                endElement("li");
            skipText = true;
            cprops.addToChain(tag, h);
            com.lowagie.text.List list = new com.lowagie.text.List(true, 10);
            String type = ((String) h.get("type"));
            if (type != null && type.toLowerCase().equals("a")) {
                list.setLettered(true);
                list.setNumbered(false);
                list.setLowercase(type.equals("a"));
            }
            String indent = ((String) h.get("indent"));
            if (indent != null && indent.matches("[0-9]+")) {
                list.setSymbolIndent(Integer.valueOf(indent));
            }
            stack.push(list);
            return;
        }
        if (tag.equals("li")) {
            if (pendingLI)
                endElement("li");
            skipText = false;
            pendingLI = true;
            cprops.addToChain(tag, h);
            stack.push(FactoryProperties.createListItem(cprops));
            return;
        }
        if (tag.equals("div") || tag.equals("body")) {
            cprops.addToChain(tag, h);
            return;
        }
        if (tag.equals("pre")) {
            if (!h.containsKey("face")) {
                h.put("face", "Courier");
            }
            cprops.addToChain(tag, h);
            isPRE = true;
            return;
        }
        if (tag.equals("p")) {
            cprops.addToChain(tag, h);
            currentParagraph = FactoryProperties.createParagraph(h);
            return;
        }
        if (tag.equals("tr")) {
            if (pendingTR)
                endElement("tr");
            skipText = true;
            pendingTR = true;
            cprops.addToChain("tr", h);
            return;
        }
        if (tag.equals("td") || tag.equals("th")) {
            if (pendingTD)
                endElement(tag);
            skipText = false;
            pendingTD = true;
            cprops.addToChain("td", h);
            stack.push(new IncCell(tag, cprops));
            return;
        }
        if (tag.equals("table")) {
            cprops.addToChain("table", h);
            IncTable table = new IncTable(h);
            stack.push(table);
            tableState.push(new boolean[] { pendingTR, pendingTD });
            pendingTR = pendingTD = false;
            skipText = true;
            return;
        }
    } catch (Exception e) {
        throw new ExceptionConverter(e);
    }
}

From source file:org.nuxeo.dam.pdf.export.PDFCreator.java

License:Open Source License

public boolean createPDF(String title, OutputStream out) throws ClientException {
    try {/*w  ww .  ja v  a2s  . c  o m*/
        Document document = new Document();
        PdfWriter.getInstance(document, out);

        document.addTitle(title);
        document.addAuthor(Functions.principalFullName(currentUser));
        document.addCreator(Functions.principalFullName(currentUser));

        document.open();

        document.add(new Paragraph("\n\n\n\n\n\n\n\n\n\n"));
        Font titleFont = new Font(Font.HELVETICA, 36, Font.BOLD);
        Paragraph titleParagraph = new Paragraph(title, titleFont);
        titleParagraph.setAlignment(Element.ALIGN_CENTER);
        document.add(titleParagraph);
        Font authorFont = new Font(Font.HELVETICA, 20);
        Paragraph authorParagraph = new Paragraph("By " + Functions.principalFullName(currentUser), authorFont);
        authorParagraph.setAlignment(Element.ALIGN_CENTER);
        document.add(authorParagraph);

        document.newPage();

        boolean foundOnePicture = false;
        for (DocumentModel doc : docs) {
            if (!doc.hasSchema(PICTURE_SCHEMA)) {
                continue;
            }
            foundOnePicture = true;

            PictureResourceAdapter picture = doc.getAdapter(PictureResourceAdapter.class);
            Blob blob = picture.getPictureFromTitle(ORIGINAL_JPEG_VIEW);
            Rectangle pageSize = document.getPageSize();
            if (blob != null) {
                Paragraph imageTitle = new Paragraph(doc.getTitle(), font);
                imageTitle.setAlignment(Paragraph.ALIGN_CENTER);
                document.add(imageTitle);

                Image image = Image.getInstance(blob.getByteArray());
                image.scaleToFit(pageSize.getWidth() - 20, pageSize.getHeight() - 100);
                image.setAlignment(Image.MIDDLE);
                Paragraph imageParagraph = new Paragraph();
                imageParagraph.add(image);
                imageParagraph.setAlignment(Paragraph.ALIGN_MIDDLE);
                document.add(imageParagraph);

                document.newPage();
            }
        }
        if (foundOnePicture) {
            document.close();
            return true;
        }
    } catch (Exception e) {
        throw ClientException.wrap(e);
    }
    return false;
}

From source file:s2s.report.Report.java

License:GNU General Public License

public void AddImage() throws Exception {
    if (ApplicationConfigurator.VIEW_REPORT_IMAGE == true) {
        //carica immagine logo
        writeTitle("\n");
        Image png = loadImage("LOGO");
        png.setAlignment(Image.MIDDLE);
        m_document.add(png);/*from w w w  .j  ava 2s.  c om*/
        writeTitle("\n");
    }
}

From source file:s2s.report.Report.java

License:GNU General Public License

public void AddScaledImage(float percentScale) throws Exception {
    if (ApplicationConfigurator.VIEW_REPORT_IMAGE == true) {
        //carica immagine logo
        writeTitle("\n");
        Image png = loadImage("LOGO");
        png.scalePercent(percentScale);//  ww w.  j  a va  2  s.com
        png.setAlignment(Image.MIDDLE);
        m_document.add(png);
        writeTitle("\n");
    }
}

From source file:tk.diginspect.main.SoOFSignatories.java

private void FDALetterhead() {
    try {/*  w  ww.  j a  v  a  2 s  . co m*/
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(),
                R.drawable.fda_letterhead);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        Image Img = Image.getInstance(stream.toByteArray());
        Img.setAlignment(Image.MIDDLE);
        Img.scalePercent(40f);
        doc.add(Img);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (BadElementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}