Example usage for com.lowagie.text.pdf BaseFont EMBEDDED

List of usage examples for com.lowagie.text.pdf BaseFont EMBEDDED

Introduction

In this page you can find the example usage for com.lowagie.text.pdf BaseFont EMBEDDED.

Prototype

boolean EMBEDDED

To view the source code for com.lowagie.text.pdf BaseFont EMBEDDED.

Click Source Link

Document

if the font has to be embedded

Usage

From source file:org.kuali.coeus.common.framework.print.watermark.Font.java

License:Open Source License

/**
 * //from w w  w.  j  av a  2s.  c o  m
 * This method for setting the Font details
 * Here set the basic FONT(TIMES_BOLD, WINANSI, EMBEDDED).
 * @return BaseFont
 */
public BaseFont getBaseFont() {
    try {
        return BaseFont.createFont(BaseFont.TIMES_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED);
    } catch (Exception exception) {
        LOG.error("Exception occured in Watermark getBaseFont. BaseFontException: " + exception);
        return null;
    }
}

From source file:org.kuali.kfs.sys.PdfFormFillerUtil.java

License:Open Source License

/**
 * This method creates a Final watermark on the input Stream.
 *
 * @param templateStream//from  w  w w  .j  a  va  2  s .  com
 * @param finalmarkText
 * @return
 * @throws IOException
 * @throws DocumentException
 */
public static byte[] createFinalmarkOnFile(byte[] templateStream, String finalmarkText)
        throws IOException, DocumentException {
    // Create a PDF reader for the template
    PdfReader pdfReader = new PdfReader(templateStream);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    // Create a PDF writer
    PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream);
    int n = pdfReader.getNumberOfPages();
    int i = 1;
    PdfContentByte over;
    BaseFont bf;
    try {
        bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
        PdfGState gstate = new PdfGState();
        while (i <= n) {
            // Watermark under the existing page
            Rectangle pageSize = pdfReader.getPageSizeWithRotation(i);
            over = pdfStamper.getOverContent(i);
            over.beginText();
            over.setFontAndSize(bf, 8);
            over.setGState(gstate);
            over.setColorFill(Color.BLACK);
            over.showTextAligned(Element.ALIGN_CENTER, finalmarkText, (pageSize.width() / 2),
                    (pageSize.height() - 10), 0);
            over.endText();
            i++;
        }
        pdfStamper.close();
    } catch (DocumentException ex) {
        throw new IOException("iText error creating final watermark on PDF", ex);
    } catch (IOException ex) {
        throw new IOException("IO error creating final watermark on PDF", ex);
    }
    return outputStream.toByteArray();
}

From source file:org.kuali.kfs.sys.PdfFormFillerUtil.java

License:Open Source License

/**
 * This Method creates a custom watermark on the File.
 *
 * @param templateStream//from  w w w .ja  v a 2  s  .  c om
 * @param watermarkText
 * @return
 * @throws IOException
 * @throws DocumentException
 */
public static byte[] createWatermarkOnFile(byte[] templateStream, String watermarkText)
        throws IOException, DocumentException {
    // Create a PDF reader for the template
    PdfReader pdfReader = new PdfReader(templateStream);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    // Create a PDF writer
    PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream);
    int n = pdfReader.getNumberOfPages();
    int i = 1;
    PdfContentByte over;
    BaseFont bf;
    try {
        bf = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED);
        PdfGState gstate = new PdfGState();
        gstate.setFillOpacity(0.5f);
        while (i <= n) {
            // Watermark under the existing page
            Rectangle pageSize = pdfReader.getPageSizeWithRotation(i);
            over = pdfStamper.getOverContent(i);
            over.beginText();
            over.setFontAndSize(bf, 200);
            over.setGState(gstate);
            over.setColorFill(Color.LIGHT_GRAY);
            over.showTextAligned(Element.ALIGN_CENTER, watermarkText, (pageSize.width() / 2),
                    (pageSize.height() / 2), 45);
            over.endText();
            i++;
        }
        pdfStamper.close();
    } catch (DocumentException ex) {
        throw new IOException("iText error creating watermark on PDF", ex);
    } catch (IOException ex) {
        throw new IOException("IO error creating watermark on PDF", ex);
    }
    return outputStream.toByteArray();
}

From source file:org.meveo.admin.action.billing.BillingAccountBean.java

License:Open Source License

public void generatePDF(long invoiceId) {
    Invoice invoice = invoiceService.findById(invoiceId);
    byte[] invoicePdf = invoice.getPdf();
    FacesContext context = FacesContext.getCurrentInstance();
    String invoiceFilename = null;
    BillingRun billingRun = invoice.getBillingRun();
    invoiceFilename = invoice.getInvoiceNumber() + ".pdf";
    if (billingRun != null && billingRun.getStatus() != BillingRunStatusEnum.VALIDATED) {
        invoiceFilename = "unvalidated-invoice.pdf";
    }//from   w  w w .j  av  a  2s  . c om

    HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
    response.setContentType("application/pdf"); // fill in
    response.setHeader("Content-disposition", "attachment; filename=" + invoiceFilename);

    try {
        OutputStream os = response.getOutputStream();
        Document document = new Document(PageSize.A4);
        if (billingRun != null && invoice.getBillingRun().getStatus() != BillingRunStatusEnum.VALIDATED) {
            // Add watemark image
            PdfReader reader = new PdfReader(invoicePdf);
            int n = reader.getNumberOfPages();
            PdfStamper stamp = new PdfStamper(reader, os);
            PdfContentByte over = null;
            BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
            PdfGState gs = new PdfGState();
            gs.setFillOpacity(0.5f);
            int i = 1;
            while (i <= n) {
                over = stamp.getOverContent(i);
                over.setGState(gs);
                over.beginText();
                System.out.println("top=" + document.top() + ",bottom=" + document.bottom());
                over.setTextMatrix(document.top(), document.bottom());
                over.setFontAndSize(bf, 150);
                over.setColorFill(Color.GRAY);
                over.showTextAligned(Element.ALIGN_CENTER, "TEST", document.getPageSize().getWidth() / 2,
                        document.getPageSize().getHeight() / 2, 45);
                over.endText();
                i++;
            }

            stamp.close();
        } else {
            os.write(invoicePdf); // fill in PDF with bytes
        }

        // contentType
        os.flush();
        os.close();
        context.responseComplete();
    } catch (IOException e) {
        log.error("failed to generate PDF ", e);
    } catch (DocumentException e) {
        log.error("error in generation PDF ", e);
    }
}

From source file:org.openconcerto.erp.core.finance.accounting.report.PdfGenerator.java

License:Open Source License

private void init() throws FileNotFoundException {

    // we create a reader for a certain document
    PdfReader reader = null;//from  w ww . j  ava2  s.c  om
    PdfWriter writer = null;
    try {
        reader = new PdfReader(getStreamStatic(this.fileNameIn));

        // we retrieve the total number of pages
        int n = reader.getNumberOfPages();
        // we retrieve the size of the first page
        Rectangle psize = reader.getPageSize(1);

        psize.setRight(psize.getRight() - this.templateOffsetX);
        psize.setTop(psize.getTop() - this.templateOffsetY);

        this.width = (int) psize.getWidth();
        float height = psize.getHeight();

        // step 1: creation of a document-object
        int MARGIN = 32;
        this.document = new Document(psize, MARGIN, MARGIN, MARGIN, MARGIN);
        // step 2: we create a writer that listens to the document
        if (!this.directoryOut.exists()) {
            this.directoryOut.mkdirs();
        }
        System.err.println("Directory out " + this.directoryOut.getAbsolutePath());
        File f = new File(this.directoryOut, this.fileNameOut);
        if (f.exists()) {
            f.renameTo(new File(this.directoryOut, "Old" + this.fileNameOut));
            f = new File(this.directoryOut, this.fileNameOut);
        }

        System.err.println("Creation du fichier " + f.getAbsolutePath());

        writer = PdfWriter.getInstance(this.document, new FileOutputStream(f));

        this.document.open();
        // step 4: we add content
        this.cb = writer.getDirectContent();

        System.out.println("There are " + n + " pages in the document.");

        this.document.newPage();

        PdfImportedPage page1 = writer.getImportedPage(reader, 1);

        this.cb.addTemplate(page1, -this.templateOffsetX, -this.templateOffsetY);

        this.bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.EMBEDDED);
        this.bfb = BaseFont.createFont(BaseFont.TIMES_BOLD, BaseFont.CP1252, BaseFont.EMBEDDED);

    } catch (FileNotFoundException fE) {
        throw fE;
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:org.orbeon.oxf.processor.pdf.XHTMLToPDFProcessor.java

License:Open Source License

public static void embedFonts(ITextRenderer renderer) {
    final PropertySet propertySet = Properties.instance().getPropertySet();
    for (final String propertyName : propertySet.getPropertiesStartsWith("oxf.fr.pdf.font.path")) {
        final String path = StringUtils.trimToNull(propertySet.getString(propertyName));
        if (path != null) {
            try {
                // Overriding the font family is optional
                final String family;
                {//from  w w  w . ja v  a 2 s  .  c  o m
                    final String[] tokens = StringUtils.split(propertyName, '.');
                    if (tokens.length >= 6) {
                        final String id = tokens[5];
                        family = StringUtils
                                .trimToNull(propertySet.getString("oxf.fr.pdf.font.family" + '.' + id));
                    } else {
                        family = null;
                    }
                }

                // Add the font
                renderer.getFontResolver().addFont(path, family, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, null);
            } catch (Exception e) {
                logger.warn("Failed to load font by path: '" + path + "' specified with property '"
                        + propertyName + "'");
            }
        }
    }
}

From source file:org.projectforge.framework.renderer.FontMap.java

License:Open Source License

public void loadFonts(File fontDir) {
    @SuppressWarnings("unchecked")
    final Collection<File> files = FileUtils.listFiles(fontDir, new String[] { "afm" }, true); // Read all afm files recursively.
    if (CollectionUtils.isNotEmpty(files) == true) {
        for (File file : files) {
            BaseFont font = null;// w  w  w  . jav a 2s. c  o  m
            try {
                font = BaseFont.createFont(file.getAbsolutePath(), BaseFont.CP1252, BaseFont.EMBEDDED);
            } catch (DocumentException ex) {
                log.error("Error while loading font '" + file.getAbsolutePath() + "': " + ex.getMessage(), ex);
                continue;
            } catch (IOException ex) {
                log.error("Error while loading font '" + file.getAbsolutePath() + "': " + ex.getMessage(), ex);
                continue;
            }
            final String fontName = font.getPostscriptFontName();
            fontMap.put(fontName, font);
        }
    }
}

From source file:org.pz.platypus.plugin.html.HtmlFont.java

License:Open Source License

/**
 * Creates an iText Font object based on the class fields
 * @param f the PdfFont containing the parameters for the font
 * @return the iText Font object/*from  ww  w .  j  ava 2 s  .  c o m*/
 */
Font createFont(final HtmlFont f) {
    int style = 0;
    //       Color col  = new Color( color.getR(), color.getG(), color.getB() );
    Font font;

    String iTextFontName = createItextFontName(f);
    if (!isBase14Font(f.typeface)) {
        style = computeItextStyle();
    }

    try {
        font = FontFactory.getFont(iTextFontName, BaseFont.CP1252, BaseFont.EMBEDDED, size, style);
    } catch (Exception ex) {
        System.out.println("Exception in PdfFont.createFont() for FontFactory.getFont() for " + iTextFontName);
        font = null;
    }

    if (font == null || font.getBaseFont() == null) {
        gdd.logWarning("iText could not find font for: " + iTextFontName + ". Using Times-Roman");
        font = FontFactory.getFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.EMBEDDED, size, style);
    }

    return (font);
}

From source file:org.pz.platypus.plugin.pdf.PdfFontFactory.java

License:Open Source License

/**
 * Creates an iText Font object based on a passed-in PdfFont
 *
 * @param f the PdfFont containing the parameters for the font
 * @return the iText Font object/*from   w  w  w .  j  av a 2 s.  c  om*/
 */
public Font createItextFont(final PdfFont f) {
    PdfFont pf = f;

    int style = 0;

    Font font = null;

    if (pf == null) {
        pf = new PdfFont(pdfData);
    }

    Color col = new Color(pf.getColor().getR(), pf.getColor().getG(), pf.getColor().getB());
    String iTextFontName = createItextFontName(pf);

    if (!isBase14Font(pf.getFace())) {
        style = computeItextStyle(pf);
        font = getIdentityHFont(iTextFontName, pf.getSize(), style, col);
    }

    if (font == null) { //TODO: identify when this would be the case.
        font = getCp1252Font(iTextFontName, pf.getSize(), style, col);
    }

    if (font == null || font.getBaseFont() == null) { //TODO: Make error msg use literals
        gdd.logWarning("iText could not find font for: " + iTextFontName + ". Using Times-Roman");
        font = FontFactory.getFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.EMBEDDED, pf.getSize(),
                style, col);
    }

    // #if debug
    //        Set fonts = FontFactory.getRegisteredFonts();
    //        Set families = FontFactory.getRegisteredFamilies();

    return (font);
}

From source file:org.pz.platypus.plugin.pdf.PdfFontFactory.java

License:Open Source License

/**
 * Gets the font with CP1252 (aka WINANSI) encoding
 * @param fontName name of font to get//from w w  w. ja v a 2 s .c  o  m
 * @param size  size in points
 * @param style bold, italic, etc.
 * @param color font color (RGB 0-255)
 * @return the font, or null if an error occurred.
 */
Font getCp1252Font(final String fontName, float size, int style, Color color) {
    Font font;
    try {

        font = FontFactory.getFont(fontName, BaseFont.CP1252, BaseFont.EMBEDDED, size, style, color);
    } catch (Exception ex) {
        font = null;
    }

    return (font);
}