Search in sources :

Example 6 with BaseFont

use of com.itextpdf.text.pdf.BaseFont in project MtgDesktopCompanion by nicho92.

the class DCIDeckSheetExport method export.

@Override
public void export(MagicDeck deck, File dest) throws IOException {
    PdfReader reader = new PdfReader(new URL(getString("PDF_URL")));
    Document document = new Document(reader.getPageSize(1));
    PdfWriter writer;
    try {
        writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
    } catch (DocumentException e) {
        throw new IOException(e.getMessage());
    }
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    // copy first page to new pdf file
    PdfImportedPage page = writer.getImportedPage(reader, 1);
    document.newPage();
    cb.addTemplate(page, 0, 0);
    Font helvetica = new Font(FontFamily.HELVETICA, 12);
    BaseFont bfHelv = helvetica.getCalculatedBaseFont(false);
    cb.beginText();
    cb.setFontAndSize(bfHelv, 11);
    // HEADER
    cb.setTextMatrix(page.getWidth() - 51f, page.getHeight() - 49);
    cb.showText(getString("LAST_NAME").substring(0, 1).toUpperCase());
    cb.setTextMatrix(page.getWidth() / 3.2f, page.getHeight() - 73);
    if (!getString("FORCED_DATE").equalsIgnoreCase(""))
        cb.showText(getString("FORCED_DATE"));
    else
        cb.showText(new SimpleDateFormat(getString("DATE_FORMAT")).format(new Date()));
    cb.setTextMatrix(page.getWidth() / 1.48f, page.getHeight() - 73);
    cb.showText(getString("EVENT_NAME"));
    cb.setTextMatrix(page.getWidth() / 3.2f, page.getHeight() - 96);
    cb.showText(getString("LOCATION"));
    cb.setTextMatrix(page.getWidth() / 1.48f, page.getHeight() - 96);
    cb.showText(deck.getName());
    cb.setTextMatrix(page.getWidth() / 1.48f, page.getHeight() - 119);
    if (getString("DECK_DESIGNER").equals(""))
        cb.showText(getString("LAST_NAME") + " " + getString("FIRST_NAME"));
    else
        cb.showText(getString("DECK_DESIGNER"));
    // MAIN DECK
    int count = 0;
    for (MagicCard mc : deck.getMap().keySet()) {
        cb.setTextMatrix(page.getWidth() / 6.4f, page.getHeight() - 185 - count);
        cb.showText(deck.getMap().get(mc) + space + mc.getName());
        count += 18;
    }
    // CONTINUED and BASIC LAND
    if (getString("FILL_CONTINUED_LANDS").equalsIgnoreCase("true")) {
        count = 0;
        for (MagicCard mc : deck.getMap().keySet()) {
            if (mc.getTypes().contains("Land")) {
                cb.setTextMatrix(page.getWidth() / 1.7f, page.getHeight() - 185 - count);
                cb.showText(deck.getMap().get(mc) + space + mc.getName());
                count += 18;
            }
        }
    }
    // SIDEBOARD
    count = 0;
    for (MagicCard mc : deck.getMapSideBoard().keySet()) {
        cb.setTextMatrix(page.getWidth() / 1.7f, page.getHeight() - 418 - count);
        cb.showText(deck.getMapSideBoard().get(mc) + space + mc.getName());
        count += 18;
    }
    // BOTTOM card count
    cb.setTextMatrix((page.getWidth() / 2f) - 30, 45);
    cb.showText(String.valueOf(deck.getAsList().size()));
    cb.setTextMatrix(page.getWidth() - 70, 100);
    cb.showText(String.valueOf(deck.getSideAsList().size()));
    // LEFT TEXT
    cb.showTextAligned(PdfContentByte.ALIGN_LEFT, getString("LAST_NAME"), 52, 90, 90);
    cb.showTextAligned(PdfContentByte.ALIGN_LEFT, getString("FIRST_NAME"), 52, 295, 90);
    String dci = getString("DCI_NUMBER");
    count = 0;
    for (int i = 0; i < dci.length(); i++) {
        char c = dci.charAt(i);
        cb.showTextAligned(PdfContentByte.ALIGN_LEFT, String.valueOf(c), 52, (428 + count), 90);
        count += 22;
    }
    cb.endText();
    document.close();
}
Also used : PdfWriter(com.itextpdf.text.pdf.PdfWriter) PdfReader(com.itextpdf.text.pdf.PdfReader) IOException(java.io.IOException) Document(com.itextpdf.text.Document) URL(java.net.URL) Font(com.itextpdf.text.Font) BaseFont(com.itextpdf.text.pdf.BaseFont) Date(java.util.Date) PdfImportedPage(com.itextpdf.text.pdf.PdfImportedPage) MagicCard(org.magic.api.beans.MagicCard) FileOutputStream(java.io.FileOutputStream) DocumentException(com.itextpdf.text.DocumentException) BaseFont(com.itextpdf.text.pdf.BaseFont) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) SimpleDateFormat(java.text.SimpleDateFormat)

Example 7 with BaseFont

use of com.itextpdf.text.pdf.BaseFont in project gephi by gephi.

the class NodeLabelRenderer method renderPDF.

public void renderPDF(PDFTarget target, Node node, String label, float x, float y, int fontSize, Color color, float outlineSize, Color outlineColor, boolean showBox, Color boxColor) {
    Font font = fontCache.get(fontSize);
    PdfContentByte cb = target.getContentByte();
    BaseFont bf = target.getBaseFont(font);
    // Box
    if (showBox) {
        cb.setRGBColorFill(boxColor.getRed(), boxColor.getGreen(), boxColor.getBlue());
        if (boxColor.getAlpha() < 255) {
            cb.saveState();
            float alpha = boxColor.getAlpha() / 255f;
            PdfGState gState = new PdfGState();
            gState.setFillOpacity(alpha);
            cb.setGState(gState);
        }
        float textWidth = getTextWidth(bf, fontSize, label);
        float textHeight = getTextHeight(bf, fontSize, label);
        // A height of just textHeight seems to be half the text height sometimes
        // BaseFont getAscentPoint and getDescentPoint may be not very precise
        cb.rectangle(x - textWidth / 2f - outlineSize / 2f, -y - outlineSize / 2f - textHeight, textWidth + outlineSize, textHeight * 2f + outlineSize);
        cb.fill();
        if (boxColor.getAlpha() < 255) {
            cb.restoreState();
        }
    }
    cb.setRGBColorFill(color.getRed(), color.getGreen(), color.getBlue());
    float textHeight = getTextHeight(bf, fontSize, label);
    if (outlineSize > 0) {
        cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE);
        cb.setRGBColorStroke(outlineColor.getRed(), outlineColor.getGreen(), outlineColor.getBlue());
        cb.setLineWidth(outlineSize);
        cb.setLineJoin(PdfContentByte.LINE_JOIN_ROUND);
        cb.setLineCap(PdfContentByte.LINE_CAP_ROUND);
        if (outlineColor.getAlpha() < 255) {
            cb.saveState();
            float alpha = outlineColor.getAlpha() / 255f;
            PdfGState gState = new PdfGState();
            gState.setStrokeOpacity(alpha);
            cb.setGState(gState);
        }
        cb.beginText();
        cb.setFontAndSize(bf, font.getSize());
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, label, x, -y - (textHeight / 2f), 0f);
        cb.endText();
        if (outlineColor.getAlpha() < 255) {
            cb.restoreState();
        }
    }
    cb.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
    cb.beginText();
    cb.setFontAndSize(bf, font.getSize());
    cb.showTextAligned(PdfContentByte.ALIGN_CENTER, label, x, -y - (textHeight / 2f), 0f);
    cb.endText();
}
Also used : BaseFont(com.itextpdf.text.pdf.BaseFont) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) BaseFont(com.itextpdf.text.pdf.BaseFont) Font(java.awt.Font) PdfGState(com.itextpdf.text.pdf.PdfGState)

Example 8 with BaseFont

use of com.itextpdf.text.pdf.BaseFont in project trainning by fernandotomasio.

the class DOC002PDF method makeReport.

@Override
public void makeReport() {
    teachingDocumentsService = (TeachingDocumentsService) services.get("teachingDocumentsService");
    document = documentManager.getDocumentPortrait();
    os = documentManager.prepareDocument(document);
    writer = documentManager.getWritter();
    ChapterSectionTOC eventHandler = new ChapterSectionTOC();
    writer.setPageEvent(eventHandler);
    document.open();
    try {
        String curriculoMinimoIdParam = (String) params.get("curriculoMinimoId");
        Long curriculoMinimoId = Long.parseLong(curriculoMinimoIdParam);
        CurriculoMinimoDTO curriculoMinimo = teachingDocumentsService.findCurriculoMinimo(curriculoMinimoId);
        buildCover(document, curriculoMinimo);
        buildFrontispicio(document, curriculoMinimo);
        document.add(buildPrefacio(curriculoMinimo));
        document.add(Chunk.NEXTPAGE);
        document.add(buildAbreviaturas(curriculoMinimo));
        document.add(Chunk.NEXTPAGE);
        document.add(buildDisposicoesPreliminares(curriculoMinimo));
        document.add(Chunk.NEXTPAGE);
        document.add(buildAtividadesComplementares(curriculoMinimo));
        document.add(Chunk.NEXTPAGE);
        document.add(buildComplementacaoInstrucao(curriculoMinimo));
        document.add(Chunk.NEXTPAGE);
        // PdfPTable tableAtividadesAvaliativas = buildAtividadesAvaliativasTable(curriculoMinimo);
        // document.add(tableAtividadesAvaliativas);
        Paragraph p12 = new Paragraph();
        p12.add(new Phrase("4 DETALHAMENTO DAS UNIDADES DIDÁTICAS", fontManager.getBoldFont()));
        document.add(p12);
        document.add(Chunk.NEWLINE);
        DisciplinaDTO[] disciplinas = teachingDocumentsService.findAllDisciplinasByCurriculoMinimo(curriculoMinimo.getId());
        for (DisciplinaDTO disciplina : disciplinas) {
            PdfPTable tableDisciplina = buildTableDisciplina(disciplina);
            document.add(tableDisciplina);
            document.add(Chunk.NEWLINE);
            UnidadeDidaticaDTO[] unidades = teachingDocumentsService.findAllUnidadesDidaticas(disciplina.getId());
            PdfPTable unidadesHeaderTable = new PdfPTable(1);
            unidadesHeaderTable.setWidthPercentage(100);
            PdfPCell p20 = new PdfPCell(new Phrase("UNIDADES DIDÁTICAS", fontManager.getBoldFont()));
            unidadesHeaderTable.addCell(p20);
            unidadesHeaderTable.setHorizontalAlignment(Element.ALIGN_CENTER);
            document.add(unidadesHeaderTable);
            document.add(Chunk.NEWLINE);
            for (UnidadeDidaticaDTO unidade : unidades) {
                PdfPTable tableUnidade = buildTableUnidade(unidade);
                document.add(tableUnidade);
                document.add(Chunk.NEWLINE);
            }
            PdfPTable tableRecomendacoes = buildTableRecomendacoesMetodologicas(disciplina);
            document.add(tableRecomendacoes);
            document.add(Chunk.NEWLINE);
            PdfPTable perfisTable = buildTablePerfisRelacionamento(disciplina);
            document.add(perfisTable);
            document.add(Chunk.NEWLINE);
            PdfPTable referenciasTable = buildTableReferencias(disciplina);
            document.add(referenciasTable);
            document.add(Chunk.NEXTPAGE);
        }
        document.add(Chunk.NEXTPAGE);
        document.add(buildDisposicoesFinais(curriculoMinimo));
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);
        document.add(buildAssinatura());
        document.add(Chunk.NEXTPAGE);
        document.add(buildSumario(eventHandler.titles));
    } catch (DocumentException e) {
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, e);
        throw new CoreException(e.getMessage());
    } finally {
        document.close();
    }
    try {
        PdfReader reader = new PdfReader(os.toByteArray());
        int n = reader.getNumberOfPages();
        String pagesInterval = String.format("1-4,%d, 5-%d", n, n - 1);
        reader.selectPages(pagesInterval);
        os = new ByteArrayOutputStream();
        PdfStamper stamper = new PdfStamper(reader, os);
        stamper.close();
        reader.close();
    } catch (IOException ex) {
        Logger.getLogger(DOC002PDF.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(DOC002PDF.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        PdfReader reader = new PdfReader(os.toByteArray());
        os = new ByteArrayOutputStream();
        PdfStamper stamper = new PdfStamper(reader, os);
        int total = reader.getNumberOfPages();
        PdfContentByte over = null;
        int total_pages = total - 1;
        for (int i = 1; i < total; i++) {
            int actual_page = i - 1;
            if (i > 5) {
                over = stamper.getOverContent(i);
                System.out.println(over);
                over.beginText();
                BaseFont baseFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, "UTF-8", false);
                over.setFontAndSize(baseFont, 12);
                if ((i % 2) == 0) {
                    over.showTextAligned(PdfContentByte.ALIGN_RIGHT, actual_page + "/" + total_pages, 538, 803, 0);
                } else {
                    over.showTextAligned(PdfContentByte.ALIGN_LEFT, actual_page + "/" + total_pages, 58, 803, 0);
                }
                over.endText();
            }
        }
        // PdfContentByte content = stamper.getOverContent(1);
        // ColumnText ct = new ColumnText( content );
        // ct.setSimpleColumn(50,50,50,50);
        // ct.setText(new Phrase("TESTANDO"));
        // ct.go();
        stamper.close();
        reader.close();
    } catch (IOException ex) {
    // Logger.getLogger(DOC001PDF.class.getName()).log(Level.SEVERE, null, ex);
    } catch (DocumentException ex) {
        Logger.getLogger(DOC001PDF.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : PdfPCell(com.itextpdf.text.pdf.PdfPCell) Phrase(com.itextpdf.text.Phrase) PdfReader(com.itextpdf.text.pdf.PdfReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UnidadeDidaticaDTO(com.tomasio.projects.trainning.dto.UnidadeDidaticaDTO) ObjetivoUnidadeDidaticaDTO(com.tomasio.projects.trainning.dto.ObjetivoUnidadeDidaticaDTO) Paragraph(com.itextpdf.text.Paragraph) PdfPTable(com.itextpdf.text.pdf.PdfPTable) CoreException(com.tomasio.projects.trainning.exeption.CoreException) PdfStamper(com.itextpdf.text.pdf.PdfStamper) DocumentException(com.itextpdf.text.DocumentException) DisciplinaDTO(com.tomasio.projects.trainning.dto.DisciplinaDTO) ObjetivoDisciplinaDTO(com.tomasio.projects.trainning.dto.ObjetivoDisciplinaDTO) BaseFont(com.itextpdf.text.pdf.BaseFont) CurriculoMinimoDTO(com.tomasio.projects.trainning.dto.CurriculoMinimoDTO) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte)

Example 9 with BaseFont

use of com.itextpdf.text.pdf.BaseFont in project summer-bean by cn-cerc.

the class Barcode128Template method output.

@Override
public void output(Document document, PdfWriter writer) throws DocumentException, IOException {
    PdfContentByte cb = writer.getDirectContent();
    BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
    // 设置中文字体和字体样式
    Font f8 = new Font(bfChinese, fontSize, Font.NORMAL);
    DataSet dataSet = this.getDataSet();
    dataSet.first();
    while (dataSet.fetch()) {
        // 条码信息
        Barcode128 code128 = new Barcode128();
        code128.setBarHeight(barHeight);
        String code = dataSet.getString("Code_");
        code128.setCode(code);
        // 反算条码宽度
        int length = code.length();
        float x = 125 / ((length + 2) * 11 + 2f);
        code128.setX(x);
        document.add(code128.createImageWithBarcode(cb, null, null));
        // 描述信息
        Paragraph paragraph = new Paragraph(dataSet.getString("Name_"), f8);
        paragraph.setAlignment(Element.ALIGN_CENTER);
        document.add(paragraph);
    }
}
Also used : Barcode128(com.itextpdf.text.pdf.Barcode128) DataSet(cn.cerc.jdb.core.DataSet) BaseFont(com.itextpdf.text.pdf.BaseFont) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) Font(com.itextpdf.text.Font) BaseFont(com.itextpdf.text.pdf.BaseFont) Paragraph(com.itextpdf.text.Paragraph)

Example 10 with BaseFont

use of com.itextpdf.text.pdf.BaseFont in project summer-bean by cn-cerc.

the class BarcodeTemplate method output.

@Override
public void output(Document document, PdfWriter writer) throws DocumentException, IOException {
    PdfContentByte cb = writer.getDirectContent();
    BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
    // 设置中文字体和字体样式
    Font f8 = new Font(bfChinese, fontSize, Font.NORMAL);
    DataSet dataSet = this.getDataSet();
    dataSet.first();
    while (dataSet.fetch()) {
        // 商品名称
        if (dataSet.getCurrent().hasValue("Name_"))
            document.add(new Paragraph(dataSet.getString("Name_"), f8));
        // 商品条码
        BarcodeEAN codeEAN = new BarcodeEAN();
        codeEAN.setBarHeight(barHeight);
        codeEAN.setCode(dataSet.getString("Code_"));
        document.add(codeEAN.createImageWithBarcode(cb, null, null));
        codeEAN.setGuardBars(false);
    }
}
Also used : BarcodeEAN(com.itextpdf.text.pdf.BarcodeEAN) DataSet(cn.cerc.jdb.core.DataSet) BaseFont(com.itextpdf.text.pdf.BaseFont) PdfContentByte(com.itextpdf.text.pdf.PdfContentByte) Font(com.itextpdf.text.Font) BaseFont(com.itextpdf.text.pdf.BaseFont) Paragraph(com.itextpdf.text.Paragraph)

Aggregations

BaseFont (com.itextpdf.text.pdf.BaseFont)12 Font (com.itextpdf.text.Font)7 Paragraph (com.itextpdf.text.Paragraph)7 PdfContentByte (com.itextpdf.text.pdf.PdfContentByte)7 PdfPTable (com.itextpdf.text.pdf.PdfPTable)4 PdfReader (com.itextpdf.text.pdf.PdfReader)4 IOException (java.io.IOException)4 Document (com.itextpdf.text.Document)3 DocumentException (com.itextpdf.text.DocumentException)3 PdfPCell (com.itextpdf.text.pdf.PdfPCell)3 PdfStamper (com.itextpdf.text.pdf.PdfStamper)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FileOutputStream (java.io.FileOutputStream)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 DataSet (cn.cerc.jdb.core.DataSet)2 Record (cn.cerc.jdb.core.Record)2 Column (cn.cerc.jexport.excel.Column)2 BaseColor (com.itextpdf.text.BaseColor)2 Phrase (com.itextpdf.text.Phrase)2