Example usage for com.lowagie.text.pdf PdfWriter getDirectContent

List of usage examples for com.lowagie.text.pdf PdfWriter getDirectContent

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfWriter getDirectContent.

Prototype


public PdfContentByte getDirectContent() 

Source Link

Document

Use this method to get the direct content for this document.

Usage

From source file:fr.aliasource.webmail.server.export.ConversationPdfEventHandler.java

License:GNU General Public License

/**
 * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter,
 *      com.lowagie.text.Document)/*from w  ww  .  j  a va 2 s  . c  o  m*/
 */
public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    cb.saveState();
    // write the headertable
    table.setTotalWidth(document.right() - document.left());
    table.writeSelectedRows(0, -1, document.left(), document.getPageSize().getHeight() - 20, cb);
    // compose the footer
    String text = writer.getPageNumber() + " / ";
    float textSize = helv.getWidthPoint(text, 12);
    float textBase = document.bottom() - 49;
    cb.beginText();
    cb.setFontAndSize(helv, 12);
    float adjust = helv.getWidthPoint("0", 12);
    cb.setTextMatrix(document.right() - textSize - adjust - 5, textBase);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(tpl, document.right() - adjust, textBase);
    cb.saveState();
    // draw a Rectangle around the page
    cb.setLineWidth(1);
    cb.rectangle(20, 20, document.getPageSize().getWidth() - 40, document.getPageSize().getHeight() - 40);
    cb.stroke();
    cb.restoreState();
}

From source file:gov.noaa.pfel.coastwatch.sgt.SgtUtil.java

License:Open Source License

/**
 * This creates a file to capture the pdf output generated by calls to 
 * graphics2D (e.g., use makeMap)./*from w w w . j  a  va 2  s  .  c o  m*/
 * This will overwrite an existing file.
 * 
 * @param pageSize e.g, PageSize.LETTER or PageSize.LETTER.rotate() (or A4, or, ...)
 * @param width the bounding box width, in 1/144ths of an inch
 * @param height the bounding box height, in 1/144ths of an inch
 * @param outputStream
 * @return an object[] with 0=g2D, 1=document, 2=pdfContentByte, 3=pdfTemplate
 * @throws Exception if trouble
 */
public static Object[] createPdf(com.lowagie.text.Rectangle pageSize, int bbWidth, int bbHeight,
        OutputStream outputStream) throws Exception {
    //currently, this uses itext
    //see the sample program:
    //  file://localhost/C:/programs/iText/examples/com/lowagie/examples/directcontent/graphics2D/G2D.java
    //Document.compress = false; //for test purposes only
    Document document = new Document(pageSize);
    document.addCreationDate();
    document.addCreator("gov.noaa.pfel.coastwatch.SgtUtil.createPdf");

    document.setPageSize(pageSize);
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);
    document.open();

    //create contentByte and template and Graphics2D objects
    PdfContentByte pdfContentByte = writer.getDirectContent();
    PdfTemplate pdfTemplate = pdfContentByte.createTemplate(bbWidth, bbHeight);
    Graphics2D g2D = pdfTemplate.createGraphics(bbWidth, bbHeight);

    return new Object[] { g2D, document, pdfContentByte, pdfTemplate };
}

From source file:gui.TransHistory.java

public void Convertpdf() throws Exception {
    display();//from w w  w  .j av  a2  s .c  om

    Document document = new Document(PageSize.A4.rotate());
    try {
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("Table.pdf"));

        document.open();
        PdfContentByte cb = writer.getDirectContent();

        cb.saveState();
        Graphics2D g2 = cb.createGraphics(500, 500);

        Shape oldClip = g2.getClip();
        g2.clipRect(20, 20, 500, 500);

        jTable1.print(g2);
        jTable1.getTableHeader().paint(g2);
        g2.setClip(oldClip);

        g2.dispose();
        cb.restoreState();
        cb.saveState();
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    document.close();

    //send mail
    query = "select email from profile_id where user_id = ?";
    psmt = con.prepareStatement(query);
    psmt.setString(1, t.user);
    rs = psmt.executeQuery();
    rs.next();
    SendMailWithAttachment smail = new SendMailWithAttachment();
    String message = "hereby is the requested transction report of account " + "no. = " + t.accNo
            + " from date " + fDate + " to " + toDate;

    smail.send(rs.getString(1), "Table.pdf", message);

}

From source file:io.vertigo.dynamo.plugins.export.pdf.PDFAdvancedPageNumberEvents.java

License:Apache License

/** {@inheritDoc} */
@Override// ww w .  java 2s  . c o  m
public void onOpenDocument(final PdfWriter writer, final Document document) {
    try {
        bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb = writer.getDirectContent();
        template = cb.createTemplate(50, 50);
    } catch (final DocumentException | IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:io.vertigo.quarto.plugins.export.pdf.PDFAdvancedPageNumberEvents.java

License:Apache License

/** {@inheritDoc} */
@Override//from  w w  w.  j  a v  a2 s  .  c o  m
public void onOpenDocument(final PdfWriter writer, final Document document) {
    try {
        bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb = writer.getDirectContent();
        template = cb.createTemplate(50, 50);
    } catch (final DocumentException | IOException e) {
        throw WrappedException.wrap(e);
    }
}

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

License:Open Source License

private void addTemplateToPage(PdfTemplate template, PdfWriter writer, String type) throws Exception {
    if (template != null) {

        if (type.equals(MessageConstants.LETTER_TYPE_PASSWORD)) {
            PdfContentByte cb = writer.getDirectContent();
            cb.addTemplate(template, getPointsFromMM(15f), getPointsFromMM(297 - 22));
        }/*from   ww  w  .  j a va2  s .  c  om*/
    }

}

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);//  w  w  w.  j  av  a 2  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:is.idega.idegaweb.egov.printing.business.DocumentBusinessBean.java

License:Open Source License

public void createHeaderDate(Document document, PdfWriter writer, String dateString) throws DocumentException {
    /*//from w w  w  .j a v  a 2 s .  co  m
     * final PdfPTable header = new PdfPTable(new float[]{1}); header.setWidthPercentage(100f); final PdfPCell defaultCell = header.getDefaultCell();
     * defaultCell.setBorder(0); //defaultCell.setFixedHeight(getPointsFromMM(40)); defaultCell.setPadding(0); defaultCell.setNoWrap(true);
     * defaultCell.setVerticalAlignment(align); header.addCell(new Phrase(new Chunk(dateString, getDefaultParagraphFont()))); document.add(header);
     */
    PdfContentByte cb = writer.getDirectContent();
    cb.beginText();
    Font font = getDefaultParagraphFont();

    try {
        BaseFont bf = BaseFont.createFont(font.getFamilyname(), BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        cb.setFontAndSize(bf, font.size());
        // we show some text starting on some absolute position with a given
        // alignment
        cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, dateString, getPointsFromMM(210 - 20),
                getPointsFromMM(297 - 20), 0);
        cb.endText();
    }

    catch (IOException e) {
        e.printStackTrace();
    }
}

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

License:Open Source License

public void createAddressContent(String addressString, PdfWriter writer) throws DocumentException {
    IWBundle iwb = getIWApplicationContext().getIWMainApplication()
            .getBundle(is.idega.idegaweb.egov.message.business.MessageConstants.IW_BUNDLE_IDENTIFIER);
    checkBundleDimensions(iwb);/*from  w ww  . j a  va  2  s. com*/
    Phrase Ph0 = new Phrase(addressString, getAddressFont());
    ColumnText ct = new ColumnText(writer.getDirectContent());
    /*
     * public void setSimpleColumn(Phrase phrase, float llx, float lly, float urx, float ury, float leading, int alignment) Parameters: phrase - a
     * Phrase llx - the lower left x corner lly - the lower left y corner urx - the upper right x corner ury - the upper right y corner leading - the
     * leading alignment - the column alignment
     * 
     */
    float llx = getPointsFromMM(addressLowerLeftX);// getPointsFromMM((20f+95f));
    float lly = getPointsFromMM(addressLowerLeftY);// 655f;
    float urx = getPointsFromMM(addressUpperRightX);// getPointsFromMM((193f));
    float ury = getPointsFromMM(addressUpperRightY);// getPointsFromMM(257f);
    // ct.setSimpleColumn(Ph0,getPointsFromMM((20f+95f)), 655f,
    // getPointsFromMM((193f)), getPointsFromMM(257f), 15, Element.ALIGN_LEFT);
    // ct.setSimpleColumn(Ph0,getPointsFromMM(new
    // Float(addressLowerLeftX).floatValue()), new
    // Float(addressLowerLeftY).floatValue(), new
    // Float(addressUpperRightX).floatValue(), new
    // Float(addressUpperRightY).floatValue(), 15, Element.ALIGN_LEFT);
    ct.setSimpleColumn(Ph0, llx, lly, urx, ury, 15, Element.ALIGN_LEFT);
    ct.go();
}

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

License:Open Source License

public void createCommuneFooter(PdfWriter writer) throws Exception {

    PdfContentByte cb = writer.getDirectContent();
    Font nameFont = getDefaultParagraphFont();
    nameFont.setSize(9);/*  ww  w .j a  v  a2 s.c  o m*/
    Font textFont = getDefaultTextFont();
    textFont.setSize(9);

    PdfPTable table = new PdfPTable(4);
    table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
    table.getDefaultCell().setNoWrap(true);

    IWBundle iwb = getIWApplicationContext().getIWMainApplication()
            .getBundle(is.idega.idegaweb.egov.message.business.MessageConstants.IW_BUNDLE_IDENTIFIER);

    table.addCell(new Phrase(iwb.getProperty("commune.name_mailaddr", "Mailaddress"), nameFont));
    table.addCell(new Phrase(iwb.getProperty("commune.name_visitaddr", "Visitaddress"), nameFont));
    table.addCell(new Phrase(iwb.getProperty("commune.name_contact", "Contact"), nameFont));
    table.addCell(new Phrase(iwb.getProperty("commune.name_org_nr", "Organizationsnr"), nameFont));

    table.addCell(new Phrase(iwb.getProperty("commune.mail_name", "Mail name"), getTextFont()));
    table.addCell(new Phrase(iwb.getProperty("commune.visit_name", "Visit name"), textFont));
    table.addCell(new Phrase(iwb.getProperty("commune.website", "www.some-place.com"), textFont));
    table.addCell(new Phrase(iwb.getProperty("commune.org_number", "XXXXXX-XXXX"), textFont));

    table.addCell(new Phrase(iwb.getProperty("commune.mail_zip", "Zip code"), textFont));
    table.addCell(new Phrase(iwb.getProperty("commune.visit_streetaddr", "Street and number,"), textFont));
    table.addCell(new Phrase(iwb.getProperty("commune.support_email", "email@someplace.com"), textFont));
    table.addCell(new Phrase(" ", textFont));

    table.addCell(new Phrase(" ", textFont));
    table.addCell(new Phrase(iwb.getProperty("commune.visit_zip", "Visit zip"), textFont));
    table.addCell(new Phrase(iwb.getProperty("commune.office_phone", "office phone"), textFont));
    table.addCell(new Phrase(" ", textFont));

    int distFromBottomMM = 30;
    int[] widths = { 20, 20, 30, 20 };
    table.setWidths(widths);
    table.setTotalWidth(getPointsFromMM(210 - 25 - 20));
    table.writeSelectedRows(0, -1, getPointsFromMM(25), getPointsFromMM(distFromBottomMM), cb);

    PdfContentByte linebyte = new PdfContentByte(writer);
    // we add some crosses to visualize the destinations

    linebyte.moveTo(getPointsFromMM(25), getPointsFromMM(distFromBottomMM + 2));
    linebyte.lineTo(getPointsFromMM(210 - 25), getPointsFromMM(distFromBottomMM + 2));

    linebyte.stroke();

    // we add the template on different positions
    cb.add(linebyte);
}