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:com.orange.atk.atkUI.corecli.utils.PdfUtilities.java

License:Apache License

/**
 * Adds a text in the given pdf file.//from   ww w .j av  a2 s . com
 * @param pdfFileName
 * @param text
 * @param x
 * @param y
 * @param rotation
 * @param page
 * @throws Exception
 */
public void addText(String pdfFileName, String text, int x, int y, int rotation, int page) throws Exception {
    // see example on http://itextdocs.lowagie.com/examples/com/lowagie/examples/general/copystamp/AddWatermarkPageNumbers.java
    // 1. copy
    File tmpPDFFile = new File(tmpDir, "tmpPDF.pdf");
    copyFile(new File(pdfFileName), tmpPDFFile);
    // 2. add text
    // we create a reader for a certain document
    PdfReader reader = new PdfReader(tmpPDFFile.getAbsolutePath());
    // we create a stamper that will copy the document to a new file
    PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(pdfFileName));
    // adding content to each page
    PdfContentByte over;
    BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
    over = stamp.getOverContent(page);
    over.beginText();
    over.setFontAndSize(bf, 32);
    over.setTextMatrix(30, 30);
    over.setColorFill(java.awt.Color.RED);
    over.showTextAligned(Element.ALIGN_LEFT, text, x, y, rotation);
    over.endText();
    // closing PdfStamper will generate the new PDF file
    stamp.close();
}

From source file:com.qcadoo.report.api.FontUtils.java

License:Open Source License

/**
 * Prepare fonts./*from  w  w  w.  ja v a 2  s . c  o  m*/
 * 
 * @throws DocumentException
 * @throws IOException
 */
public static synchronized void prepare() throws DocumentException, IOException {
    if (dejavuBold10Dark == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Pdf fonts initialization");
        }
        try {
            FontFactory.register("/fonts/dejaVu/DejaVuSans.ttf");
            dejavu = BaseFont.createFont("/fonts/dejaVu/DejaVuSans.ttf", BaseFont.IDENTITY_H,
                    BaseFont.EMBEDDED);
        } catch (ExceptionConverter e) {
            LOG.warn("Font not found, using embedded font helvetica");
            dejavu = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED);
        }
        dejavuBold70Light = new Font(dejavu, 70);
        dejavuBold70Light.setStyle(Font.BOLD);
        dejavuBold70Light.setColor(ColorUtils.getLightColor());

        dejavuBold70Dark = new Font(dejavu, 70);
        dejavuBold70Dark.setStyle(Font.BOLD);
        dejavuBold70Dark.setColor(ColorUtils.getDarkColor());

        dejavuBold19Light = new Font(dejavu, 19);
        dejavuBold19Light.setStyle(Font.BOLD);
        dejavuBold19Light.setColor(ColorUtils.getLightColor());

        dejavuBold19Dark = new Font(dejavu, 19);
        dejavuBold19Dark.setStyle(Font.BOLD);
        dejavuBold19Dark.setColor(ColorUtils.getDarkColor());

        dejavuBold14Dark = new Font(dejavu, 14);
        dejavuBold14Dark.setStyle(Font.BOLD);
        dejavuBold14Dark.setColor(ColorUtils.getDarkColor());

        dejavuBold17Light = new Font(dejavu, 17);
        dejavuBold17Light.setStyle(Font.BOLD);
        dejavuBold17Light.setColor(ColorUtils.getLightColor());

        dejavuBold14Light = new Font(dejavu, 17);
        dejavuBold14Light.setStyle(Font.BOLD);
        dejavuBold14Light.setColor(ColorUtils.getLightColor());

        dejavuBold17Dark = new Font(dejavu, 17);
        dejavuBold17Dark.setStyle(Font.BOLD);
        dejavuBold17Dark.setColor(ColorUtils.getDarkColor());

        dejavuRegular9Light = new Font(dejavu, 9);
        dejavuRegular9Light.setColor(ColorUtils.getLightColor());

        dejavuRegular9Dark = new Font(dejavu, 9);
        dejavuRegular9Dark.setColor(ColorUtils.getDarkColor());

        dejavuRegular7Dark = new Font(dejavu, 7);
        dejavuRegular7Dark.setColor(ColorUtils.getDarkColor());

        dejavuBold9Dark = new Font(dejavu, 9);
        dejavuBold9Dark.setColor(ColorUtils.getDarkColor());
        dejavuBold9Dark.setStyle(Font.BOLD);

        dejavuBold11Dark = new Font(dejavu, 11);
        dejavuBold11Dark.setColor(ColorUtils.getDarkColor());
        dejavuBold11Dark.setStyle(Font.BOLD);

        dejavuBold11Light = new Font(dejavu, 11);
        dejavuBold11Light.setColor(ColorUtils.getLightColor());
        dejavuBold11Light.setStyle(Font.BOLD);

        dejavuRegular10Dark = new Font(dejavu, 10);
        dejavuRegular10Dark.setColor(ColorUtils.getDarkColor());

        dejavuBold10Dark = new Font(dejavu, 10);
        dejavuBold10Dark.setColor(ColorUtils.getDarkColor());
        dejavuBold10Dark.setStyle(Font.BOLD);

        dejavuRegular7Light = new Font(dejavu, 7);
        dejavuRegular7Light.setColor(ColorUtils.getLightColor());

        dejavuBold7Dark = new Font(dejavu, 7);
        dejavuBold7Dark.setColor(ColorUtils.getDarkColor());
        dejavuBold7Dark.setStyle(Font.BOLD);

        dejavuBold8Dark = new Font(dejavu, 8);
        dejavuBold8Dark.setColor(ColorUtils.getDarkColor());
        dejavuBold8Dark.setStyle(Font.BOLD);

    }
}

From source file:com.qubit.terra.docs.util.FontManager.java

License:Open Source License

protected FontEntry createFontEntry(File ttfFile)
        throws FontFormatException, IOException, FileNotFoundException, DocumentException {
    InputStream is = null;/*from w  w  w .  ja  va2  s .c  o m*/
    try {
        is = new FileInputStream(ttfFile);
        byte[] fontData = RandomAccessFileOrArray.InputStreamToArray(is);
        BaseFont baseFont = BaseFont.createFont(ttfFile.getName(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED,
                false, fontData, null);
        FontEntry fontEntry = new FontEntry(ttfFile.getName(), baseFont);
        return fontEntry;
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:com.square.adherent.noyau.service.implementations.RelevePrestationServiceImpl.java

License:Open Source License

@Override
public FichierDto getRelevePrestationByteArray(Long idRelevePrestation, Long idPersonne, boolean duplicata) {
    logger.debug(messageSourceUtil.get(MessageKeyUtil.LOGGER_DEBUG_CONVERSION_RELEVE_PRESTATION,
            new String[] { String.valueOf(idRelevePrestation) }));
    final CritereSelectionRelevePrestationDto critereSelectionRelevePrestationDto = new CritereSelectionRelevePrestationDto();
    critereSelectionRelevePrestationDto.setRelevePrestationId(idRelevePrestation);
    if (idPersonne != null) {
        critereSelectionRelevePrestationDto.setIdPersonne(idPersonne);
    }/*from ww  w.j ava  2  s  .  co  m*/
    final List<RelevePrestation> lstReleves = relevePrestationDao
            .getListeReleveParCriteres(critereSelectionRelevePrestationDto, null);
    if (lstReleves.size() == 1) {
        final RelevePrestation releve = lstReleves.get(0);
        final String error = messageSourceUtil.get(MessageKeyUtil.ERROR_RECUPERATION_FICHIER);
        FichierDto fichier;
        final String cheminFichier = serveurEmcRepReleve + File.separator + releve.getNomFichier();
        try {
            fichier = new FichierDto();
            fichier.setNomFichier(releve.getNomFichierCommercial());
            if (duplicata) {
                // On appose la mention "DUPLICATA" sur toutes les pages du relev.
                try {
                    final PdfReader reader = new PdfReader(cheminFichier);
                    final int nombrePages = reader.getNumberOfPages();
                    final BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA_BOLDOBLIQUE, BaseFont.WINANSI,
                            BaseFont.EMBEDDED);
                    final PdfStamper stamp = new PdfStamper(reader,
                            new FileOutputStream(FICHIER_DUPLICATA_TEMP));
                    final int taillePolice = 56;
                    final int positionX = ((int) PageSize.A4.getWidth()) / 2;
                    final int positionY = ((int) PageSize.A4.getHeight()) / 2;
                    final int rotation = 30;
                    for (int i = 1; i <= nombrePages; i++) {
                        final PdfContentByte over = stamp.getOverContent(i);
                        over.beginText();
                        over.setColorFill(Color.GRAY);
                        final PdfGState gs1 = new PdfGState();
                        gs1.setFillOpacity(NIVEAU_TRANSPARENCE);
                        over.setGState(gs1);
                        over.setFontAndSize(bf, taillePolice);
                        over.showTextAligned(PdfContentByte.ALIGN_CENTER, "DUPLICATA", positionX, positionY,
                                rotation);
                        over.endText();
                    }
                    stamp.close();
                    reader.close();
                    fichier.setContenu(IOUtils.toByteArray(new FileInputStream(FICHIER_DUPLICATA_TEMP)));
                    final File file = new File(FICHIER_DUPLICATA_TEMP);
                    file.delete();
                } catch (DocumentException e) {
                    throw new TechnicalException(
                            messageSourceUtil.get(MessageKeyUtil.ERROR_IMPOSSIBLE_AJOUTER_MENTION_DUPLICATA,
                                    new String[] { releve.getNomFichier() }));
                }
            } else {
                fichier.setContenu(IOUtils.toByteArray(new FileInputStream(cheminFichier)));
            }
            fichier.setTypeMime(Magic.getMagicMatch(fichier.getContenu()).getMimeType());
        } catch (FileNotFoundException e) {
            logger.error(error + releve.getNomFichier(), e);
            throw new TechnicalException(error + cheminFichier);
        } catch (IOException e) {
            logger.error(error + releve.getNomFichier(), e);
            throw new TechnicalException(error + cheminFichier);
        } catch (MagicParseException e) {
            logger.error(error + releve.getNomFichier(), e);
            throw new TechnicalException(error + cheminFichier);
        } catch (MagicMatchNotFoundException e) {
            logger.error(error + releve.getNomFichier(), e);
            throw new TechnicalException(error + cheminFichier);
        } catch (MagicException e) {
            logger.error(error + releve.getNomFichier(), e);
            throw new TechnicalException(error + cheminFichier);
        }
        return fichier;
    } else {
        throw new TechnicalException(
                messageSourceUtil.get(MessageKeyUtil.ERROR_ABSCENCE_RELEVE_PRESTATION_PERSONNE));
    }
}

From source file:com.teag.reports.CashFlows.java

private void drawLifeExp() {
    PdfContentByte cb = writer.getDirectContentUnder();
    char c = userInfo.getClientFirstName().charAt(0);
    char s = ' ';
    if (!userInfo.isSingle()) {
        s = userInfo.getSpouseFirstName().charAt(0);
    }//from w w w .  ja  v  a 2  s.  c om

    String cText = Character.toString(c) + ": " + userInfo.getClientFirstName()
            + " actuarial life expectancy - " + userInfo.getClientLifeExpectancy() + " years";
    String sText = Character.toString(s) + ": " + userInfo.getSpouseFirstName()
            + " actuarial life expectancy - " + userInfo.getSpouseLifeExpectancy() + " years";
    try {
        BaseFont font = BaseFont.createFont(Locations.getFontLocation() + "times.ttf", BaseFont.CP1252,
                BaseFont.EMBEDDED);

        cb.beginText();
        cb.setFontAndSize(font, 8);
        cb.setRGBColorFill(255, 0, 0);
        cb.setTextMatrix(72 * .5F, .5f * 72);
        cb.showText(cText);
        cb.endText();
        if (!userInfo.isSingle()) {
            cb.beginText();
            cb.setFontAndSize(font, 8);
            cb.setRGBColorFill(255, 0, 0);
            cb.setTextMatrix(72 * .5F, .35f * 72);
            cb.showText(sText);
            cb.endText();
        }
    } catch (Exception e) {
        System.err.println("Error printing Life Expectancy");
    }
}

From source file:com.teag.reports.EstatePlan.java

private void drawLifeExp() {
    PdfContentByte cb = writer.getDirectContentUnder();

    char c = userInfo.getClientFirstName().charAt(0);
    char s = ' ';
    if (!userInfo.isSingle()) {
        s = userInfo.getSpouseFirstName().charAt(0);
    }/* ww  w  .j  av  a 2s.  com*/

    String cText = Character.toString(c).toUpperCase() + ": " + userInfo.getClientFirstName()
            + " actuarial life expectancy - " + userInfo.getClientLifeExpectancy() + " years";
    String sText = Character.toString(s).toUpperCase() + ": " + userInfo.getSpouseFirstName()
            + " actuarial life expectancy - " + userInfo.getSpouseLifeExpectancy() + " years";
    try {
        BaseFont font = BaseFont.createFont(Locations.getFontLocation() + "times.ttf", BaseFont.CP1252,
                BaseFont.EMBEDDED);

        cb.beginText();
        cb.setFontAndSize(font, 8);
        cb.setRGBColorFill(255, 0, 0);
        cb.setTextMatrix(72 * .5F, .4f * 72);
        cb.showText(cText);
        cb.endText();
        if (!userInfo.isSingle()) {
            cb.beginText();
            cb.setFontAndSize(font, 8);
            cb.setRGBColorFill(255, 0, 0);
            cb.setTextMatrix(72 * .5F, .25f * 72);
            cb.showText(sText);
            cb.endText();
        }
    } catch (Exception e) {
        System.err.println("Error printing Life Expectancy");
    }
}

From source file:controllers.PdfGenerator.java

public void toStream(String string, OutputStream os, String documentBaseURL, List<String> fonts) {
    try {//  w  ww  .j a va 2s.  c  o m
        InputStream input = new ByteArrayInputStream(string.getBytes("UTF-8"));
        ITextRenderer renderer = new ITextRenderer();
        if (fonts != null)
            for (String font : fonts) {
                renderer.getFontResolver().addFont(font, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            }
        PdfUserAgent myUserAgent = new PdfUserAgent(renderer.getOutputDevice());
        myUserAgent.setSharedContext(renderer.getSharedContext());
        renderer.getSharedContext().setUserAgentCallback(myUserAgent);
        Document document = new HtmlDocumentBuilder().parse(input);
        renderer.setDocument(document, documentBaseURL);
        renderer.layout();
        renderer.createPDF(os);
    } catch (Exception e) {
        Logger.error("Error creating document from template", e);
    }
}

From source file:corner.orm.tapestry.pdf.PdfUtils.java

License:Apache License

/**
 * //w ww. j  a v  a2s .  c  om
 * @return
 */
public static final BaseFont createSongLightBaseFont() {
    BaseFont bfChinese;
    try {
        bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
        return bfChinese;
    } catch (DocumentException e) {
        throw new PdfSystemException(e);
    } catch (IOException e) {
        throw new PdfSystemException(e);
    }

}

From source file:de.thorstenberger.taskmodel.view.webapp.filter.ExportPDFFilter.java

License:Open Source License

/**
 * Render the given xhtml document as pdf and write it to the response outputstream.
 *
 * @param dom/*from  w w w . j  a  va  2 s.  c om*/
 *            xhtml document
 * @param request
 * @param response
 * @throws IOException
 *             if the document could not get renderered
 */
private void renderPdf(final Document dom, final HttpServletRequest request, final ServletResponse response)
        throws IOException {
    final ITextRenderer renderer = new ITextRenderer(80 / 3f, 15);
    if (fontPath != null) {
        try {
            log.debug("Using unicode font at path " + fontPath);
            renderer.getFontResolver().addFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        } catch (DocumentException e1) {
            e1.printStackTrace();
        }
    }
    renderer.setDocument(dom, getLocalURL(request));
    renderer.layout();

    response.setContentType("application/pdf");
    // set an appropriate filename
    ((HttpServletResponse) response).setHeader("Content-Disposition",
            "attachment; filename=" + request.getParameter(EXPORTFILENAME));

    try {
        renderer.createPDF(response.getOutputStream());
    } catch (final DocumentException e) {
        log.error("Could not render pdf.", e);
        // FIXME should we just return the original content?
        throw new IOException(e.getMessage());
    }
}

From source file:de.unigoettingen.sub.commons.contentlib.pdflib.PDFTitlePage.java

License:Apache License

/************************************************************************************
 * get font as {@link Font} from given font name and size
 * //from ww w .j a  v a2s  .c o  m
 * @param fontname name of font
 * @param fontsize size of font
 * 
 * @throws PDFManagerException
 ************************************************************************************/
private Font getPDFFont(int fontsize) throws PDFManagerException {
    Font resultfont = null;

    // set the base font
    try {
        if (this.ttffontpath == null) {
            // don't use TTF

            LOGGER.debug("Do not use TrueType Font... instead standard Arial is used");
            resultfont = FontFactory.getFont("Arial", BaseFont.CP1252, BaseFont.EMBEDDED, fontsize);

            // String[] codePages = basefont.getCodePagesSupported();
            // System.out.println("All available encodings for font:\n\n");
            // for (int i = 0; i < codePages.length; i++) {
            // System.out.println(codePages[i]);
            // }
        } else {
            // load font, embedd it - use unicode
            LOGGER.debug("Use TrueType Font... at:" + this.ttffontpath);

            BaseFont bf = BaseFont.createFont(this.ttffontpath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            resultfont = new Font(bf, fontsize);
        }

    } catch (Exception e) {
        LOGGER.error("DocumentException while creating title page for PDF", e);
        throw new PDFManagerException("Exception while creating Titlepage for PDF", e);
    }

    return resultfont;
}