Example usage for com.lowagie.text.pdf PdfTemplate setTextMatrix

List of usage examples for com.lowagie.text.pdf PdfTemplate setTextMatrix

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfTemplate setTextMatrix.

Prototype

public void setTextMatrix(float x, float y) 

Source Link

Document

Changes the text matrix.

Usage

From source file:is.idega.idegaweb.egov.printing.business.DocumentBusinessBean.java

License:Open Source License

private PdfTemplate createPasswordLetterTemplate(PdfWriter writer) throws Exception {
    IWBundle iwb = getIWApplicationContext().getIWMainApplication()
            .getBundle(is.idega.idegaweb.egov.message.business.MessageConstants.IW_BUNDLE_IDENTIFIER);
    PdfContentByte cb = writer.getDirectContent();

    float tempLength = 511f;
    float tempHeight = getPointsFromMM(40);
    PdfTemplate template = cb.createTemplate(tempLength, tempHeight);

    if (addTemplateHeader()) {
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        String mail_zip = iwb.getProperty("commune.mail_zip");
        String mail_name = iwb.getProperty("commune.mail_name");

        float convLengt = 100f;
        float convHeight = 60f;
        template.rectangle(0f, 0f, convLengt, convHeight);
        template.moveTo(0f, 0f);/*from w  ww.  j  a v a2 s  .co m*/
        template.lineTo(convLengt, convHeight);
        template.moveTo(convLengt, 0f);
        template.lineTo(0f, convHeight);
        template.stroke();
        template.beginText();
        template.setFontAndSize(bf, 11f);
        template.setTextMatrix(5f, 40f);
        template.showText(mail_name);

        template.endText();
        template.beginText();
        template.setFontAndSize(bf, 11f);
        template.setTextMatrix(5f, 25f);
        template.showText(mail_zip);
        template.endText();

        Image porto = Image.getInstance(iwb.getResourcesRealPath() + "/shared/porto_betalt.jpg");
        porto.scaleAbsolute(60f, 60f);

        // Image portoA =Image.getInstance(iwb.getResourcesRealPath()+
        // "/shared/porto_a_logo.jpg");
        // float Awidth = 2.3f*60f;
        // portoA.scaleToFit(Awidth,60f);

        float portoXPos = tempLength - 90f;
        // float portoAXPos = portoXPos-Awidth-5f;
        template.addImage(porto, 60f, 0f, 0f, 60f, portoXPos, 0);
        // template.addImage(portoA,Awidth,0f,0f,60f,portoAXPos,0);
    }

    return template;
}

From source file:joelib2.io.types.PDF.java

License:Open Source License

/**
 *  Writes a molecule with his <tt>PairData</tt> .
 *
 * @param  mol              the molecule with additional data
 * @param  title            the molecule title or <tt>null</tt> if the title
 *      from the molecule should be used
 * @param  writePairData    if <tt>true</tt> then the additional molecule data
 *      is written//w  ww.j a va2  s.co m
 * @param  attribs2write    Description of the Parameter
 * @return                  <tt>true</tt> if the molecule and the data has
 *      been succesfully written.
 * @exception  IOException  Description of the Exception
 */
public boolean write(Molecule mol, String title, boolean writePairData, List attribs2write,
        SMARTSPatternMatcher smarts) throws IOException {
    if (firstMoleculeWritten == false) {
        document.open();
        firstMoleculeWritten = true;
    }

    Dimension d = new Dimension(Mol2Image.instance().getDefaultWidth(),
            Mol2Image.instance().getDefaultHeight());
    RenderingAtoms container = new RenderingAtoms();
    container.add(mol);

    RenderHelper.translateAllPositive(container);
    RenderHelper.scaleMolecule(container, d, 0.8);
    RenderHelper.center(container, d);

    Renderer2D renderer = new Renderer2D();

    //BaseFont helvetica = null;
    try {
        BaseFont.createFont("Helvetica", BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    } catch (DocumentException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    int w = d.width;
    int h = d.height;
    PdfContentByte cb = writer.getDirectContent();
    PdfTemplate tp = cb.createTemplate(w, h);
    Graphics2D g2 = tp.createGraphics(w, h);
    g2.setStroke(new BasicStroke(0.1f));
    tp.setWidth(w);
    tp.setHeight(h);

    g2.setColor(renderer.getRenderer2DModel().getBackColor());
    g2.fillRect(0, 0, d.width, d.height);

    if (smarts != null) {
        renderer.selectSMARTSPatterns(container, smarts);
    }

    renderer.paintMolecule(container, g2);

    g2.dispose();

    ////cb.addTemplate(tp, 72, 720 - h);
    //cb.addTemplate(tp, 12, 720 - h);
    cb.addTemplate(tp, 0, document.getPageSize().height() - h);

    //     Mol2Image.instance().mol2image(mol);
    BaseFont bf = null;

    try {
        bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
    } catch (DocumentException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }

    String string = "";

    //float myBorder = DEFAULT_BORDER;
    //float fontSize = 10;
    //float fontSizeDelta = DEFAULT_FONT_OFFSET;
    float hpos;

    if (writePairData) {
        PairData pairData;
        PairDataIterator gdit = mol.genericDataIterator();
        int index = 0;
        boolean firstPageWritten = false;

        List attributesV;

        if (attribs2write == null) {
            // write all descriptors
            attributesV = new Vector();

            //DescResult tmpPropResult;
            while (gdit.hasNext()) {
                pairData = gdit.nextPairData();

                attributesV.add(pairData.getKey());
            }
        } else {
            attributesV = attribs2write;
        }

        // sort descriptors by attribute name
        String[] attributes = new String[attributesV.size()];

        for (int i = 0; i < attributesV.size(); i++) {
            attributes[i] = (String) attributesV.get(i);
        }

        Arrays.sort(attributes);

        // write them
        for (int i = 0; i < attributes.length; i++) {
            pairData = mol.getData(attributes[i]);
            string = pairData.getKey() + " = " + pairData.toString();

            // reduce too complex data
            string = string.replace('\n', ' ');
            string = string.substring(0, Math.min(string.length(), WRITE_MAX_CHARACTERS));

            tp = cb.createTemplate(document.getPageSize().width() - pageBorder, fontSize + fontSizeDelta);
            tp.setFontAndSize(bf, fontSize);
            tp.beginText();
            tp.setTextMatrix(0, fontSizeDelta);
            tp.showText(string);
            tp.endText();
            cb.setLineWidth(1f);
            tp.moveTo(0, 0);
            tp.lineTo(document.getPageSize().width() - (2 * pageBorder), 0);
            tp.stroke();

            if (firstPageWritten) {
                hpos = document.getPageSize().height() - ((fontSize + fontSizeDelta) * (index + 1));
            } else {
                hpos = document.getPageSize().height() - h - ((fontSize + fontSizeDelta) * (index + 1));
            }

            if (hpos < pageBorder) {
                index = 1;
                firstPageWritten = true;
                hpos = document.getPageSize().height() - ((fontSize + fontSizeDelta) * (index + 1));

                try {
                    document.newPage();
                } catch (DocumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            cb.addTemplate(tp, pageBorder, hpos);

            index++;
        }
    }

    try {
        document.newPage();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return (true);
}

From source file:org.posterita.core.PDFReportGenerator.java

License:Open Source License

protected Image getTextAsImage(String text) throws OperationException {
    try {/*from  ww  w  .  j a  va  2 s.  co m*/
        PdfTemplate template = writer.getDirectContent().createTemplate(20, 20);
        //BaseFont bf = BaseFont.createFont("Helvetica", "winansi", false); 
        BaseFont bf = HEADER_FONT.getBaseFont();
        float size = 10;
        float width = bf.getWidthPoint(text, size);
        template.beginText();
        //template.setRGBColorFillF(1, 1, 1);
        template.setFontAndSize(bf, size);
        template.setTextMatrix(0, 2);
        template.showText(text);
        template.endText();
        template.setWidth(width);
        template.setHeight(size + 2);
        // make an Image object from the template
        Image img = Image.getInstance(template);
        img.setAlignment(Image.RIGHT | Image.TEXTWRAP);

        return img;

    } catch (Exception e) {
        throw new OperationException(e);
    }
}

From source file:org.unitime.timetable.webutil.timegrid.PdfTimetableGridTable.java

License:Open Source License

public void addTextVertical(PdfPCell cell, String text, boolean bold) throws Exception {
    if (text == null)
        return;/*from ww w.  j  a  v  a2 s. c o m*/
    if (text.indexOf("<span") >= 0)
        text = text.replaceAll("</span>", "").replaceAll("<span .*>", "");
    Font font = PdfFont.getFont(bold);
    BaseFont bf = font.getBaseFont();
    float width = bf.getWidthPoint(text, font.getSize());
    PdfTemplate template = iWriter.getDirectContent().createTemplate(2 * font.getSize() + 4, width);
    template.beginText();
    template.setColorFill(Color.BLACK);
    template.setFontAndSize(bf, font.getSize());
    template.setTextMatrix(0, 2);
    template.showText(text);
    template.endText();
    template.setWidth(width);
    template.setHeight(font.getSize() + 2);
    //make an Image object from the template
    Image img = Image.getInstance(template);
    img.setRotationDegrees(270);
    //embed the image in a Chunk
    Chunk ck = new Chunk(img, 0, 0);

    if (cell.getPhrase() == null) {
        cell.setPhrase(new Paragraph(ck));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
    } else {
        cell.getPhrase().add(ck);
    }
}