Example usage for com.itextpdf.text.pdf PdfContentByte ALIGN_CENTER

List of usage examples for com.itextpdf.text.pdf PdfContentByte ALIGN_CENTER

Introduction

In this page you can find the example usage for com.itextpdf.text.pdf PdfContentByte ALIGN_CENTER.

Prototype

int ALIGN_CENTER

To view the source code for com.itextpdf.text.pdf PdfContentByte ALIGN_CENTER.

Click Source Link

Document

The alignment is center

Usage

From source file:gravabncertificado007.CarimboCertificado.java

public void aplicaCarimboBin(String BN, String caminhoarquivo)
        throws DocumentException, IOException, RuntimeException {

    PdfReader.unethicalreading = true;//from  www  . j a v  a  2s .co  m
    //Cria o reader para o primeiro PDF

    PdfReader reader = new PdfReader(caminhoarquivo);
    Rectangle psize = reader.getPageSize(1);
    float width = psize.getWidth();
    float height = psize.getHeight();

    Document document = new Document(new Rectangle(width, height));

    PdfWriter writer = PdfWriter.getInstance(document,
            new FileOutputStream(caminhoarquivo.substring(0, caminhoarquivo.length() - 4) + "-C.pdf"));

    document.open();

    int i = 0;
    BN = BN.substring(BN.length() - 13, BN.length() - 4);
    PdfContentByte cb = writer.getDirectContent();
    while (i < reader.getNumberOfPages()) {
        i++;

        document.newPage();

        PdfContentByte under = writer.getDirectContentUnder();
        PdfImportedPage page1 = writer.getImportedPage(reader, i);
        cb.addTemplate(page1, 0, i * 0.2f);

        //CARIMBO DA BN
        BaseFont bf = BaseFont.createFont(BaseFont.COURIER_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
        cb.beginText();
        cb.setFontAndSize(bf, 14);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " ________________", width / 6, 44, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |               |", width / 6, 32, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |               |", width / 6, 22, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " NR", width / 6, 28, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " " + BN, width / 6, 16, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |               |", width / 6, 12, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " ________________", width / 6, 14, 0);
        cb.endText();

    }

    document.close();
    writer.close();
    reader.close();

}

From source file:gravabncertificado007.CarimboCertificado.java

public void aplicaCariboGedi(String BN, String caminhoarquivo)
        throws DocumentException, IOException, RuntimeException {

    PdfReader.unethicalreading = true;/*w w  w  .  j a  v  a 2s.  c  o m*/
    //Cria o reader para o primeiro PDF

    PdfReader reader = new PdfReader(caminhoarquivo);
    Rectangle psize = reader.getPageSize(1);
    float width = psize.getWidth();
    float height = psize.getHeight();

    Document document = new Document(new Rectangle(width, height));

    PdfWriter writer = PdfWriter.getInstance(document,
            new FileOutputStream(caminhoarquivo.substring(0, caminhoarquivo.length() - 4) + "-G.pdf"));

    document.open();

    int i = 0;
    BN = BN.substring(BN.length() - 13, BN.length() - 4);
    PdfContentByte cb = writer.getDirectContent();
    while (i < reader.getNumberOfPages()) {
        i++;

        document.newPage();

        PdfContentByte under = writer.getDirectContentUnder();
        PdfImportedPage page1 = writer.getImportedPage(reader, i);
        cb.addTemplate(page1, 0, i * 0.2f);

        BaseFont bf = BaseFont.createFont(BaseFont.COURIER_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
        cb.beginText();
        cb.setFontAndSize(bf, 14);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "  _________________  ", width / 6, 44, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |                 |", width / 6, 32, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |                 |", width / 6, 22, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |Copia Controlada |", width / 6, 28, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |                 |", width / 6, 16, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |                 |", width / 6, 12, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |_________________|", width / 6, 14, 0);
        cb.endText();
    }

    document.close();
    writer.close();
    reader.close();

}

From source file:hsa.awp.admingui.report.printer.MergePDFUtil.java

License:Open Source License

private void addPagination(int totalPages, int currentPageNumber) throws DocumentException, IOException {
    cb.beginText();/*from w w w  . j  a v a 2 s. co m*/
    cb.setFontAndSize(getBaseFont(), 9);
    cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "" + currentPageNumber + " of " + totalPages, 520, 5, 0);
    cb.endText();
}

From source file:jasperSoft.MergePDF.java

/**
 * /*from w  w  w .  j a  v  a  2s .co  m*/
 * @param streamOfPDFFiles
 * @param outputStream
 * @param paginate 
 */
public static void concatPDFs(List<InputStream> streamOfPDFFiles, OutputStream outputStream, boolean paginate) {

    Document document = new Document();
    try {
        List<InputStream> pdfs = streamOfPDFFiles;
        List<PdfReader> readers = new ArrayList<PdfReader>();
        int totalPages = 0;
        Iterator<InputStream> iteratorPDFs = pdfs.iterator();

        // Create Readers for the pdfs.
        while (iteratorPDFs.hasNext()) {
            InputStream pdf = iteratorPDFs.next();
            PdfReader pdfReader = new PdfReader(pdf);
            readers.add(pdfReader);
            totalPages += pdfReader.getNumberOfPages();
        }
        // Create a writer for the outputstream
        PdfWriter writer = PdfWriter.getInstance(document, outputStream);

        document.open();
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
        // data

        PdfImportedPage page;
        int currentPageNumber = 0;
        int pageOfCurrentReaderPDF = 0;
        Iterator<PdfReader> iteratorPDFReader = readers.iterator();

        // Loop through the PDF files and add to the output.
        while (iteratorPDFReader.hasNext()) {
            PdfReader pdfReader = iteratorPDFReader.next();

            // Create a new page in the target for each source page.
            while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                document.newPage();
                pageOfCurrentReaderPDF++;
                currentPageNumber++;
                page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
                cb.addTemplate(page, 0, 0);

                // Code for pagination.
                if (paginate) {
                    cb.beginText();
                    cb.setFontAndSize(bf, 9);
                    cb.showTextAligned(PdfContentByte.ALIGN_CENTER,
                            "" + currentPageNumber + " of " + totalPages, 520, 5, 0);
                    cb.endText();
                }
            }
            pageOfCurrentReaderPDF = 0;
        }
        outputStream.flush();
        document.close();
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (document.isOpen()) {
            document.close();
        }
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

From source file:jati.GerandoArquivoCarimbado.java

public static void GerandoArquivoCarimbadoPDF(String caminhoarquivo, String BN)
        throws InvalidPdfException, IOException {
    //Copiando arquivo informado.

    try {/*from   ww  w . j a  v a 2  s  .  co m*/

        // Adicionado parametro para nao retornar erro quando o documento for protegido.
        PdfReader.unethicalreading = true;

        PdfWriter writer = PdfWriter.getInstance(montaraAquivo(caminhoarquivo),
                new FileOutputStream(caminhoarquivo.substring(0, caminhoarquivo.length() - 4) + "-C.pdf"));
        // ABRE O DOCUMENTO CRIADO PARA MANUSEIO
        montaraAquivo(caminhoarquivo).open();

        //SUBSTRING RETIRA PARTE DO TEXTO ENTRE OS INDICES ESPECIFICADOS 
        //caminhoDestino RECEBE UM NOVO CAMINHO RESULTANTE DOS INDICES DE CAMINHO.LENGTH - BN.LENGTH
        // QUE FORMAM UMA NOVA STRING
        String caminhodestino = (caminhoarquivo.substring(0, caminhoarquivo.length() - BN.length()));

        File destinooriginal = new File(caminhodestino + "ORIGINAL\\");
        if (!destinooriginal.exists()) {
            destinooriginal.mkdir();
        }

        caminhodestino = (caminhodestino + "ORIGINAL\\" + BN);

        //TESTANDO NOVA FORMA DE COPIAR
        File origem = new File(caminhoarquivo);
        File destino = new File(caminhodestino);

        FileInputStream fis = new FileInputStream(origem);
        FileOutputStream fos = new FileOutputStream(destino);

        FileChannel inChannel = fis.getChannel();
        FileChannel outChannel = fos.getChannel();

        long transferFrom = outChannel.transferFrom(inChannel, 0, inChannel.size());

        fis.close();
        fos.close();
        inChannel.close();
        outChannel.close();

        // Thread.sleep(10);
        BN = BN.substring(0, BN.length() - 4);

        PdfContentByte cb = writer.getDirectContent();

        int i = 0;
        while (i < reader.getNumberOfPages()) {
            montaraAquivo(caminhodestino).newPage();

            i++;

            //PDFCONTETBYTE GERA UMA ESPECIE DE CODIGO DE BARRAS 
            PdfContentByte under = writer.getDirectContentUnder();
            PdfImportedPage page1 = writer.getImportedPage(reader, i);
            cb.addTemplate(page1, 0, i * 0.2f);

            //CARIMBO DA BN
            BaseFont bf = BaseFont.createFont(BaseFont.COURIER_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
            cb.beginText();
            cb.setFontAndSize(bf, 14);
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " ________________", width / 6, 44, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |               |", width / 6, 32, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |               |", width / 6, 22, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " NR", width / 6, 28, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " " + BN, width / 6, 16, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |               |", width / 6, 12, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " ________________", width / 6, 14, 0);
            cb.endText();

        }

        montaraAquivo(caminhodestino).close();
        writer.close();
        reader.close();
        origem.delete();

    } catch (IOException | DocumentException ex) {
    }

}

From source file:net.atomique.ksar.export.FilePDF.java

License:Open Source License

private void IndexPage(Document document) {
    try {/*from  w ww.j  a  v  a2s. c  o  m*/
        float pdfCenter = ((pdfwidth - pdfmargins) / 2);

        String title = "SAR Statistics";
        String t_date = "on " + mysar.myparser.getDate();
        String hostName = "for " + mysar.myparser.gethostName();
        String osType = mysar.myparser.getOstype();
        String graphStart = mysar.myparser.getStartOfGraph().toString();
        String graphEnd = mysar.myparser.getEndOfGraph().toString();

        pdfcb.beginText();
        pdfcb.setFontAndSize(bf, 40);
        pdfcb.setColorFill(new BaseColor(0x00, 0x00, 0x00));
        pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, title, pdfCenter, 500, 0);

        pdfcb.setFontAndSize(bf, 32);
        pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, hostName + " (" + osType + ")", pdfCenter, 400, 0);
        pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, t_date, pdfCenter, 300, 0);

        pdfcb.setFontAndSize(bf, 20);
        pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, graphStart, pdfCenter, 200, 0);
        pdfcb.showTextAligned(PdfContentByte.ALIGN_CENTER, graphEnd, pdfCenter, 150, 0);

        pdfcb.endText();
        document.newPage();

    } catch (Exception de) {
        log.error("IndexPage Exception", de);
    }
}

From source file:org.alfresco.extension.pdftoolkit.repo.action.executer.PDFWatermarkActionExecuter.java

License:Apache License

/**
 * Writes text watermark to one of the 5 preconfigured locations
 * /*from   w  w  w .j  a  v a  2  s  . c om*/
 * @param pcb
 * @param r
 * @param tokens
 * @param size
 * @param position
 */
private void writeAlignedText(PdfContentByte pcb, Rectangle r, Vector<String> tokens, float size,
        String position) {
    // get the dimensions of our 'rectangle' for text
    float height = size * tokens.size();
    float width = 0;
    float centerX = 0, startY = 0;
    for (int i = 0; i < tokens.size(); i++) {
        if (pcb.getEffectiveStringWidth(tokens.get(i), false) > width) {
            width = pcb.getEffectiveStringWidth(tokens.get(i), false);
        }
    }

    // now that we have the width and height, we can calculate the center
    // position for
    // the rectangle that will contain our text.
    if (position.equals(POSITION_BOTTOMLEFT)) {
        centerX = width / 2 + PAD;
        startY = 0 + PAD + height;
    } else if (position.equals(POSITION_BOTTOMRIGHT)) {
        centerX = r.getWidth() - (width / 2) - PAD;
        startY = 0 + PAD + height;
    } else if (position.equals(POSITION_TOPLEFT)) {
        centerX = width / 2 + PAD;
        startY = r.getHeight() - (PAD * 2);
    } else if (position.equals(POSITION_TOPRIGHT)) {
        centerX = r.getWidth() - (width / 2) - PAD;
        startY = r.getHeight() - (PAD * 2);
    } else if (position.equals(POSITION_CENTER)) {
        centerX = r.getWidth() / 2;
        startY = (r.getHeight() / 2) + (height / 2);
    }

    // apply text to PDF
    pcb.beginText();

    for (int t = 0; t < tokens.size(); t++) {
        pcb.showTextAligned(PdfContentByte.ALIGN_CENTER, tokens.get(t), centerX, startY - (size * t), 0);
    }

    pcb.endText();

}

From source file:org.gephi.preview.plugin.renderers.EdgeLabelRenderer.java

License:Open Source License

public void renderPDF(PDFTarget target, String label, float x, float y, Color color, float outlineSize,
        Color outlineColor) {/*from www . ja va 2 s.  co m*/
    PdfContentByte cb = target.getContentByte();
    cb.setRGBColorFill(color.getRed(), color.getGreen(), color.getBlue());
    BaseFont bf = target.getBaseFont(font);
    float textHeight = getTextHeight(bf, font.getSize(), 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();
}

From source file:org.gephi.preview.plugin.renderers.NodeLabelRenderer.java

License:Open Source License

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//w  w  w  .j  a  v  a 2  s  . co  m
    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();
}

From source file:ryerson.daspub.artifact.PublishQRTagSheetTask.java

License:Open Source License

/**
 * Draw label/*  w w  w  .  j  av  a2  s.co  m*/
 * @param Writer
 * @param Text
 * @param x
 * @param y
 */
private void drawLabel(PdfWriter Writer, String Text, int x, int y) throws DocumentException, IOException {
    drawLabel(Writer, Text, x, y, PdfContentByte.ALIGN_CENTER);
}