Example usage for com.lowagie.text Image getScaledHeight

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

Introduction

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

Prototype

public float getScaledHeight() 

Source Link

Document

Gets the scaled height of the image.

Usage

From source file:br.com.nordestefomento.jrimum.utilix.PDFUtil.java

License:Apache License

/**
 * <p>//w ww  .  java2s. c om
 * Muda um input field para uma imgem com as dimenses e possio do field.
 * </p>
 * 
 * @param stamper
 * @param rect
 * @param image
 * @return rectanglePDF
 * @throws DocumentException
 * 
 * @since 0.2
 */

public static RectanglePDF changeField2Image(PdfStamper stamper, RectanglePDF rect, Image image)
        throws DocumentException {

    // Ajustando o tamanho da imagem de acordo com o tamanho do campo.
    // image.scaleToFit(rect.getWidth(), rect.getHeight());
    image.scaleAbsolute(rect.getWidth(), rect.getHeight());

    // A rotina abaixo tem por objetivo deixar a imagem posicionada no
    // centro
    // do field, tanto na perspectiva horizontal como na vertical.
    // Caso no se queira mais posicionar a imagem no centro do field, basta
    // efetuar a chamada a seguir:
    // "image.setAbsolutePosition
    // (rect.getLowerLeftX(),rect.getLowerLeftY());"
    image.setAbsolutePosition(rect.getLowerLeftX() + (rect.getWidth() - image.getScaledWidth()) / 2,
            rect.getLowerLeftY() + (rect.getHeight() - image.getScaledHeight()) / 2);

    // cb = stamper.getUnderContent(rect.getPage());
    stamper.getOverContent(rect.getPage()).addImage(image);

    return rect;
}

From source file:classroom.newspaper_a.Newspaper06.java

public static void putImage(PdfContentByte canvas, Image img, float llx, float lly, float w, float h)
        throws DocumentException {
    img.scaleToFit(w, h);/*from   w  w w  .  ja  va  2 s .co  m*/
    float offsetX = (w - img.getScaledWidth()) / 2f;
    float offsetY = (h - img.getScaledHeight()) / 2f;
    img.setAbsolutePosition(llx + offsetX, lly + offsetY);
    canvas.addImage(img);
}

From source file:classroom.newspaper_a.Newspaper07.java

public static void putImage(PdfContentByte canvas, Image img, String url, float llx, float lly, float w,
        float h) throws DocumentException {
    img.scaleToFit(w, h);/*from  ww w .j a v  a 2s  .  c o  m*/
    float offsetX = (w - img.getScaledWidth()) / 2f;
    float offsetY = (h - img.getScaledHeight()) / 2f;
    img.setAbsolutePosition(llx + offsetX, lly + offsetY);
    img.setAnnotation(new Annotation(0, 0, 0, 0, url));
    canvas.addImage(img);
}

From source file:classroom.newspaper_b.Newspaper12.java

public static void putImage(PdfContentByte canvas, String path, float[] positions)
        throws DocumentException, IOException {
    float w = positions[3] - positions[1];
    float h = positions[4] - positions[2];
    Image img = Image.getInstance(path);
    img.scaleToFit(w, h);//from w w w  .  j  a  v  a  2 s. c o m
    float offsetX = (w - img.getScaledWidth()) / 2f;
    float offsetY = (h - img.getScaledHeight()) / 2f;
    img.setAbsolutePosition(positions[1] + offsetX, positions[2] + offsetY);
    canvas.addImage(img);
    canvas.rectangle(positions[1], positions[2], w, h);
    canvas.stroke();
}

From source file:com.allinfinance.system.util.PdfUtil.java

License:Open Source License

public static void create(String mchtId, String selMchtId, String path, List<Object[]> list,
        LinkedHashMap<String, List<Object[]>> map, Set<String> set) throws Exception {

    BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

    ///*from  ww  w  .  jav a 2 s.  co  m*/
    Font font17 = new Font(bfChinese, 17, Font.BOLD);
    Font font8 = new Font(bfChinese, 8, Font.NORMAL);
    Font font9 = new Font(bfChinese, 9, Font.NORMAL);
    Font font9Bold = new Font(bfChinese, 9, Font.BOLD);
    Font font8Red = new Font(bfChinese, 8, Font.NORMAL);
    font8Red.setColor(Color.RED);
    Font font8Green = new Font(bfChinese, 8, Font.NORMAL);
    font8Green.setColor(Color.GREEN);

    logger.info("Starting build document...");

    Document document = new Document(PageSize.A4, 36, 36, 36, 36);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();
    LINECANVAS border = new LINECANVAS();

    //
    float[] widths = { 0.1f, 0.35f, 0.35f, 0.1f, 0.1f };
    PdfPTable table = new PdfPTable(widths);
    table.setWidthPercentage(100);
    table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
    table.getDefaultCell().setFixedHeight(12);

    //CELL
    PdfPCell cellMchntId = new PdfPCell(new Paragraph(mchtId, font8));
    cellMchntId.setBorder(PdfPCell.BOTTOM);
    cellMchntId.setHorizontalAlignment(Element.ALIGN_CENTER);

    PdfPCell cellMchntName = new PdfPCell(new Paragraph(InformationUtil.getMchtName(mchtId), font8));
    cellMchntName.setBorder(PdfPCell.BOTTOM);
    cellMchntName.setHorizontalAlignment(Element.ALIGN_CENTER);

    String point = InformationUtil.getCurPaperPoint(selMchtId);
    PdfPCell cellPoint;
    cellPoint = new PdfPCell(new Paragraph(point, font8Red));
    cellPoint.setBorder(PdfPCell.NO_BORDER);
    cellPoint.setHorizontalAlignment(Element.ALIGN_CENTER);

    PdfPCell cellLevel;
    String level = InformationUtil.getCurPaperLevel(selMchtId);
    if (Integer.valueOf(point) >= 60) {
        cellLevel = new PdfPCell(new Paragraph(level, font8Green));
    } else {
        cellLevel = new PdfPCell(new Paragraph(level, font8Red));
    }
    cellLevel.setBorder(PdfPCell.NO_BORDER);
    cellLevel.setHorizontalAlignment(Element.ALIGN_CENTER);

    //
    Image img = Image.getInstance(
            ServletActionContext.getServletContext().getResource("/ext/resources/images/Title_logo.gif"));
    img.scalePercent(70);
    float w = img.getScaledWidth();
    float h = img.getScaledHeight();
    writer.getDirectContent().addImage(img, w, 0, 0, h, 36, PageSize.A4.getHeight() - 36 - h);

    //
    PdfPCell cell = new PdfPCell(new Paragraph("?", font17));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setColspan(5);
    cell.setFixedHeight(h);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setFixedHeight(20);
    cell.setColspan(5);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);

    table.addCell(new Paragraph("?", font8));
    table.addCell(cellMchntId);
    table.addCell(" ");
    table.addCell(new Paragraph(": ", font8));
    table.addCell(cellPoint);

    table.addCell(new Paragraph("??", font8));
    table.addCell(cellMchntName);
    table.addCell(" ");
    table.addCell(new Paragraph(": ", font8));
    table.addCell(cellLevel);

    document.add(table);
    document.add(new Paragraph("\n\n"));

    //?
    PdfPTable t = new PdfPTable(1);

    Iterator<Object[]> it0 = list.iterator();
    int i = 1;
    while (it0.hasNext()) {
        Object[] obj = it0.next();
        PdfPCell c = new PdfPCell();
        c.addElement(new Paragraph("Q" + String.valueOf(i++) + "" + obj[1].toString(), font9Bold));
        List<Object[]> opts = map.get(obj[0].toString());
        String opt = "";
        Iterator<Object[]> it1 = opts.iterator();
        while (it1.hasNext()) {
            Object[] o = it1.next();
            if (set.contains(o[0])) {
                opt += "? ";
                opt += o[1].toString();
                opt += "         ";
            } else {
                opt += " ";
                opt += o[1].toString();
                opt += "         ";
            }
        }
        c.addElement(new Paragraph(opt.trim(), font9));
        c.setBorder(PdfPCell.NO_BORDER);
        if (i - 1 != list.size()) {
            c.setCellEvent(border);
        }
        t.addCell(c);
    }

    PdfPTable oTable = new PdfPTable(1);
    oTable.setWidthPercentage(100);
    PdfPCell ce = new PdfPCell(t);
    ce.setBorderColor(Color.GRAY);
    oTable.addCell(ce);

    document.add(oTable);

    document.close();

    logger.info("Finish build document...");

}

From source file:com.allinfinance.system.util.PdfUtil.java

License:Open Source License

public static void create(String path, List<Object[]> list, LinkedHashMap<String, List<Object[]>> map)
        throws Exception {

    BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

    ///*from   w w  w .j av  a  2s.  c om*/
    Font font17 = new Font(bfChinese, 17, Font.BOLD);
    Font font8 = new Font(bfChinese, 8, Font.NORMAL);
    Font font10 = new Font(bfChinese, 10, Font.NORMAL);
    Font font10Bold = new Font(bfChinese, 10, Font.BOLD);
    Font font8Red = new Font(bfChinese, 8, Font.NORMAL);
    font8Red.setColor(Color.RED);
    Font font8Green = new Font(bfChinese, 8, Font.NORMAL);
    font8Green.setColor(Color.GREEN);

    logger.info("Starting build document...");

    Document document = new Document(PageSize.A4, 36, 36, 36, 36);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();
    LINECANVAS border = new LINECANVAS();

    //
    float[] widths = { 0.1f, 0.35f, 0.55f };
    PdfPTable table = new PdfPTable(widths);
    table.setWidthPercentage(100);
    table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
    table.getDefaultCell().setFixedHeight(12);

    //CELL
    PdfPCell cellMchntId = new PdfPCell(new Paragraph("XXXXXXXXXXXXXXX", font8));
    cellMchntId.setBorder(PdfPCell.BOTTOM);
    cellMchntId.setHorizontalAlignment(Element.ALIGN_CENTER);

    PdfPCell cellMchntName = new PdfPCell(new Paragraph("?", font8));
    cellMchntName.setBorder(PdfPCell.BOTTOM);
    cellMchntName.setHorizontalAlignment(Element.ALIGN_CENTER);

    //
    Image img = Image.getInstance(
            ServletActionContext.getServletContext().getResource("/ext/resources/images/Title_logo.gif"));
    img.scalePercent(70);
    float w = img.getScaledWidth();
    float h = img.getScaledHeight();
    writer.getDirectContent().addImage(img, w, 0, 0, h, 36, PageSize.A4.getHeight() - 36 - h);

    //
    PdfPCell cell = new PdfPCell(new Paragraph("?", font17));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setColspan(3);
    cell.setFixedHeight(h);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setFixedHeight(20);
    cell.setColspan(3);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);

    table.addCell(new Paragraph("?", font8));
    table.addCell(cellMchntId);
    table.addCell(" ");

    table.addCell(new Paragraph("??", font8));
    table.addCell(cellMchntName);
    table.addCell(" ");

    document.add(table);
    document.add(new Paragraph("\n\n"));

    //?
    PdfPTable t = new PdfPTable(1);

    Iterator<Object[]> it0 = list.iterator();
    int i = 1;
    while (it0.hasNext()) {
        Object[] obj = it0.next();
        PdfPCell c = new PdfPCell();
        c.addElement(new Paragraph("Q" + String.valueOf(i++) + "" + obj[1].toString(), font10Bold));
        List<Object[]> opts = map.get(obj[0].toString());
        String opt = "";
        Iterator<Object[]> it1 = opts.iterator();
        while (it1.hasNext()) {
            Object[] o = it1.next();
            opt += " ";
            opt += o[1].toString();
            opt += "         ";
        }
        c.addElement(new Paragraph(opt.trim(), font10));
        c.setBorder(PdfPCell.NO_BORDER);
        if (i - 1 != list.size()) {
            c.setCellEvent(border);
        }
        t.addCell(c);
    }

    PdfPTable oTable = new PdfPTable(1);
    oTable.setWidthPercentage(100);
    PdfPCell ce = new PdfPCell(t);
    ce.setBorderColor(Color.GRAY);
    oTable.addCell(ce);

    document.add(oTable);

    document.close();

    logger.info("Finish build document...");

}

From source file:com.aripd.clms.service.ContractServiceBean.java

@Override
public void generatePdf(ContractEntity contract) {
    String baseFontUrl = "/fonts/Quivira.otf";
    FontFactory.register(baseFontUrl);//w ww  . j  a va  2  s .com

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        BaseFont bf = BaseFont.createFont(baseFontUrl, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        Font font18n = new Font(bf, 18, Font.NORMAL);
        Font font12n = new Font(bf, 12, Font.NORMAL);
        Font font8n = new Font(bf, 8, Font.NORMAL);
        Font font8nbu = new Font(bf, 8, Font.BOLD | Font.UNDERLINE);
        Font font8ng = new Font(bf, 8, Font.NORMAL, Color.DARK_GRAY);
        Font font6n = new Font(bf, 6, Font.NORMAL);

        Document document = new Document(PageSize.A4);
        PdfWriter writer = PdfWriter.getInstance(document, output);
        document.open();
        addMetaData(document);
        addTitlePage(document, contract);
        Image imgBlue = Image.getInstance(1, 1, 3, 8, new byte[] { (byte) 0, (byte) 0, (byte) 255, });
        imgBlue.scaleAbsolute(document.getPageSize().getWidth(), 10);
        imgBlue.setAbsolutePosition(0, document.getPageSize().getHeight() - imgBlue.getScaledHeight());
        PdfImage stream = new PdfImage(imgBlue, "", null);
        stream.put(new PdfName("ITXT_SpecialId"), new PdfName("123456789"));
        PdfIndirectObject ref = writer.addToBody(stream);
        imgBlue.setDirectReference(ref.getIndirectReference());
        document.add(imgBlue);

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

        PdfPCell cell = new PdfPCell(new Paragraph(contract.getName(), font18n));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setColspan(2);
        cell.setPadding(5);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("Version: " + contract.getVersion(), font8n));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setColspan(2);
        cell.setPadding(5);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("Review: " + contract.getReview(), font8n));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setColspan(2);
        cell.setPadding(5);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph(contract.getRemark(), font12n));
        cell.setBorder(Rectangle.NO_BORDER);
        cell.setColspan(2);
        cell.setPadding(5);
        table.addCell(cell);
        document.add(table);
        // Start a new page
        document.newPage();

        HTMLWorker htmlWorker = new HTMLWorker(document);
        htmlWorker.parse(new StringReader(contract.getRemark()));
        // Start a new page
        document.newPage();

        document.add(new Paragraph("Review Board", font18n));
        document.add(new LineSeparator(0.5f, 100, null, 0, -5));

        table = new PdfPTable(3);
        table.setWidthPercentage(100);

        cell = new PdfPCell(new Paragraph("Review Board", font18n));
        cell.setColspan(3);
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("Version", font12n));
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("Date", font12n));
        table.addCell(cell);
        cell = new PdfPCell(new Paragraph("Review", font12n));
        table.addCell(cell);
        for (HistoryContractEntity history : historyContractService.listing(contract)) {
            cell = new PdfPCell(new Paragraph(history.getVersion().toString(), font8n));
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph(history.getStartdate().toString(), font8n));
            table.addCell(cell);
            cell = new PdfPCell(new Paragraph(history.getReview(), font8n));
            table.addCell(cell);
        }
        document.add(table);

        document.close();

        FacesContext context = FacesContext.getCurrentInstance();
        HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
        response.reset();
        response.addHeader("Content-Type", "application/force-download");
        String filename = URLEncoder.encode(contract.getName() + ".pdf", "UTF-8");
        //            response.addHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
        response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + filename);
        response.getOutputStream().write(output.toByteArray());
        response.getOutputStream().flush();
        context.responseComplete();
        context.renderResponse();

    } catch (BadPdfFormatException | IOException ex) {
        Logger.getLogger(ContractServiceBean.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(ContractServiceBean.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:com.huateng.system.util.PdfUtil.java

License:Open Source License

public static void create(String mchtId, String selMchtId, String path, List<Object[]> list,
        LinkedHashMap<String, List<Object[]>> map, Set<String> set) throws Exception {

    BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

    ////  w w  w.  j  av  a2s .  c  o  m
    Font font17 = new Font(bfChinese, 17, Font.BOLD);
    Font font8 = new Font(bfChinese, 8, Font.NORMAL);
    Font font9 = new Font(bfChinese, 9, Font.NORMAL);
    Font font9Bold = new Font(bfChinese, 9, Font.BOLD);
    Font font8Red = new Font(bfChinese, 8, Font.NORMAL);
    font8Red.setColor(Color.RED);
    Font font8Green = new Font(bfChinese, 8, Font.NORMAL);
    font8Green.setColor(Color.GREEN);

    logger.info("Starting build document...");

    Document document = new Document(PageSize.A4, 36, 36, 36, 36);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();
    LINECANVAS border = new LINECANVAS();

    //
    float[] widths = { 0.1f, 0.35f, 0.35f, 0.1f, 0.1f };
    PdfPTable table = new PdfPTable(widths);
    table.setWidthPercentage(100);
    table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
    table.getDefaultCell().setFixedHeight(12);

    //CELL
    PdfPCell cellMchntId = new PdfPCell(new Paragraph(mchtId, font8));
    cellMchntId.setBorder(PdfPCell.BOTTOM);
    cellMchntId.setHorizontalAlignment(Element.ALIGN_CENTER);

    PdfPCell cellMchntName = new PdfPCell(new Paragraph(InformationUtil.getMchtName(mchtId), font8));
    cellMchntName.setBorder(PdfPCell.BOTTOM);
    cellMchntName.setHorizontalAlignment(Element.ALIGN_CENTER);

    String point = InformationUtil.getCurPaperPoint(selMchtId);
    PdfPCell cellPoint;
    cellPoint = new PdfPCell(new Paragraph(point, font8Red));
    cellPoint.setBorder(PdfPCell.NO_BORDER);
    cellPoint.setHorizontalAlignment(Element.ALIGN_CENTER);

    PdfPCell cellLevel;
    String level = InformationUtil.getCurPaperLevel(selMchtId);
    if (Integer.valueOf(point) >= 60) {
        cellLevel = new PdfPCell(new Paragraph(level, font8Green));
    } else {
        cellLevel = new PdfPCell(new Paragraph(level, font8Red));
    }
    cellLevel.setBorder(PdfPCell.NO_BORDER);
    cellLevel.setHorizontalAlignment(Element.ALIGN_CENTER);

    //
    Image img = Image.getInstance(
            ServletActionContext.getServletContext().getResource("/ext/resources/images/Title_logo.gif"));
    img.scalePercent(70);
    float w = img.getScaledWidth();
    float h = img.getScaledHeight();
    writer.getDirectContent().addImage(img, w, 0, 0, h, 36, PageSize.A4.getHeight() - 36 - h);

    //
    PdfPCell cell = new PdfPCell(new Paragraph("?", font17));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setColspan(5);
    cell.setFixedHeight(h);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setFixedHeight(20);
    cell.setColspan(5);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);

    table.addCell(new Paragraph("?", font8));
    table.addCell(cellMchntId);
    table.addCell(" ");
    table.addCell(new Paragraph(": ", font8));
    table.addCell(cellPoint);

    table.addCell(new Paragraph("??", font8));
    table.addCell(cellMchntName);
    table.addCell(" ");
    table.addCell(new Paragraph(": ", font8));
    table.addCell(cellLevel);

    document.add(table);
    document.add(new Paragraph("\n\n"));

    //?
    PdfPTable t = new PdfPTable(1);

    Iterator<Object[]> it0 = list.iterator();
    int i = 1;
    while (it0.hasNext()) {
        Object[] obj = it0.next();
        PdfPCell c = new PdfPCell();
        c.addElement(new Paragraph("Q" + String.valueOf(i++) + "" + obj[1].toString(), font9Bold));
        List<Object[]> opts = map.get(obj[0].toString());
        String opt = "";
        Iterator<Object[]> it1 = opts.iterator();
        while (it1.hasNext()) {
            Object[] o = it1.next();
            if (set.contains(o[0])) {
                opt += "? ";
                opt += o[1].toString();
                opt += "         ";
            } else {
                opt += " ";
                opt += o[1].toString();
                opt += "         ";
            }
        }
        c.addElement(new Paragraph(opt.trim(), font9));
        c.setBorder(PdfPCell.NO_BORDER);
        if (i - 1 != list.size()) {
            c.setCellEvent(border);
        }
        t.addCell(c);
    }

    PdfPTable oTable = new PdfPTable(1);
    oTable.setWidthPercentage(100);
    PdfPCell ce = new PdfPCell(t);
    ce.setBorderColor(Color.GRAY);
    oTable.addCell(ce);

    document.add(oTable);

    document.close();

    logger.info("Finish build document...");
}

From source file:com.huateng.system.util.PdfUtil.java

License:Open Source License

public static void create(String path, List<Object[]> list, LinkedHashMap<String, List<Object[]>> map)
        throws Exception {

    BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

    ////from  w w w  . jav  a  2s .co  m
    Font font17 = new Font(bfChinese, 17, Font.BOLD);
    Font font8 = new Font(bfChinese, 8, Font.NORMAL);
    Font font10 = new Font(bfChinese, 10, Font.NORMAL);
    Font font10Bold = new Font(bfChinese, 10, Font.BOLD);
    Font font8Red = new Font(bfChinese, 8, Font.NORMAL);
    font8Red.setColor(Color.RED);
    Font font8Green = new Font(bfChinese, 8, Font.NORMAL);
    font8Green.setColor(Color.GREEN);

    logger.info("Starting build document...");

    Document document = new Document(PageSize.A4, 36, 36, 36, 36);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path));
    document.open();
    LINECANVAS border = new LINECANVAS();

    //
    float[] widths = { 0.1f, 0.35f, 0.55f };
    PdfPTable table = new PdfPTable(widths);
    table.setWidthPercentage(100);
    table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
    table.getDefaultCell().setFixedHeight(12);

    //CELL
    PdfPCell cellMchntId = new PdfPCell(new Paragraph("XXXXXXXXXXXXXXX", font8));
    cellMchntId.setBorder(PdfPCell.BOTTOM);
    cellMchntId.setHorizontalAlignment(Element.ALIGN_CENTER);

    PdfPCell cellMchntName = new PdfPCell(new Paragraph("?", font8));
    cellMchntName.setBorder(PdfPCell.BOTTOM);
    cellMchntName.setHorizontalAlignment(Element.ALIGN_CENTER);

    //
    Image img = Image.getInstance(
            ServletActionContext.getServletContext().getResource("/ext/resources/images/Title_logo.gif"));
    img.scalePercent(70);
    float w = img.getScaledWidth();
    float h = img.getScaledHeight();
    writer.getDirectContent().addImage(img, w, 0, 0, h, 36, PageSize.A4.getHeight() - 36 - h);

    //
    PdfPCell cell = new PdfPCell(new Paragraph("?", font17));
    cell.setHorizontalAlignment(Element.ALIGN_CENTER);
    cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
    cell.setColspan(3);
    cell.setFixedHeight(h);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);

    cell = new PdfPCell();
    cell.setFixedHeight(20);
    cell.setColspan(3);
    cell.setBorder(PdfPCell.NO_BORDER);
    table.addCell(cell);

    table.addCell(new Paragraph("?", font8));
    table.addCell(cellMchntId);
    table.addCell(" ");

    table.addCell(new Paragraph("??", font8));
    table.addCell(cellMchntName);
    table.addCell(" ");

    document.add(table);
    document.add(new Paragraph("\n\n"));

    //?
    PdfPTable t = new PdfPTable(1);

    Iterator<Object[]> it0 = list.iterator();
    int i = 1;
    while (it0.hasNext()) {
        Object[] obj = it0.next();
        PdfPCell c = new PdfPCell();
        c.addElement(new Paragraph("Q" + String.valueOf(i++) + "" + obj[1].toString(), font10Bold));
        List<Object[]> opts = map.get(obj[0].toString());
        String opt = "";
        Iterator<Object[]> it1 = opts.iterator();
        while (it1.hasNext()) {
            Object[] o = it1.next();
            opt += " ";
            opt += o[1].toString();
            opt += "         ";
        }
        c.addElement(new Paragraph(opt.trim(), font10));
        c.setBorder(PdfPCell.NO_BORDER);
        if (i - 1 != list.size()) {
            c.setCellEvent(border);
        }
        t.addCell(c);
    }

    PdfPTable oTable = new PdfPTable(1);
    oTable.setWidthPercentage(100);
    PdfPCell ce = new PdfPCell(t);
    ce.setBorderColor(Color.GRAY);
    oTable.addCell(ce);

    document.add(oTable);

    document.close();

    logger.info("Finish build document...");
}

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

License:Open Source License

/**
 * TIFF to PDF conversion/*from w  w  w  . ja v a 2s. c  o  m*/
 */
public void tiff2pdf(File input, File output) throws ConversionException {
    RandomAccessFileOrArray ra = null;
    Document doc = null;

    try {
        // Open PDF
        doc = new Document();
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(output));
        PdfContentByte cb = writer.getDirectContent();
        doc.open();
        //int pages = 0;

        // Open TIFF
        ra = new RandomAccessFileOrArray(input.getPath());
        int comps = TiffImage.getNumberOfPages(ra);

        for (int c = 0; c < comps; ++c) {
            Image img = TiffImage.getTiffImage(ra, c + 1);

            if (img != null) {
                log.debug("tiff2pdf - page {}", c + 1);

                if (img.getScaledWidth() > 500 || img.getScaledHeight() > 700) {
                    img.scaleToFit(500, 700);
                }

                img.setAbsolutePosition(20, 20);
                //doc.add(new Paragraph("page " + (c + 1)));
                cb.addImage(img);
                doc.newPage();
                //++pages;
            }
        }
    } catch (FileNotFoundException e) {
        throw new ConversionException("File not found: " + e.getMessage(), e);
    } catch (DocumentException e) {
        throw new ConversionException("Document exception: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new ConversionException("IO exception: " + e.getMessage(), e);
    } finally {
        if (ra != null) {
            try {
                ra.close();
            } catch (IOException e) {
                // Ignore
            }
        }

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