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

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

Introduction

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

Prototype

public void showText(final PdfTextArray text) 

Source Link

Document

Show an array of text.

Usage

From source file:bouttime.report.boutsheet.BoutSheetReport.java

License:Open Source License

/**
 * Add the header string to the document.
 * @param writer PdfWriter object to use.
 * @param document Document object to add the header to.
 *///www.  j  av a 2s.  co  m
private void addHeader(PdfWriter writer, Document document) {
    if (this.headerString == null) {
        return;
    }

    PdfContentByte cb = writer.getDirectContent();

    float textBase = document.top() + 5;
    cb.beginText();
    cb.setFontAndSize(this.baseFont, 12);
    cb.setTextMatrix(document.left(), textBase);
    cb.showText(this.headerString);
    cb.endText();
}

From source file:bouttime.report.bracketsheet.BracketSheetUtil.java

License:Open Source License

public static void drawString(PdfContentByte cb, BaseFont bf, float x, float y, float fontSize, String string)
        throws DocumentException, IOException {

    if (bf == null) {
        bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.EMBEDDED);
    }/*from  w w w.  ja  v a2 s .c  o  m*/

    cb.setFontAndSize(bf, fontSize);

    cb.beginText();
    cb.setTextMatrix(x, y);
    cb.showText(string);
    cb.endText();
    cb.stroke();
}

From source file:co.ordinate.printer.NewJFrame.java

private void A4BTNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_A4BTNActionPerformed

    new SwingWorker<Object, Object>() {
        String filename;/*from   ww  w . j a  v  a 2 s.c  om*/

        @Override
        protected void done() {

            ConsoleMsg("Printing PROFILE SHEET IN PROGRESS.. ");

        }

        @Override
        protected Object doInBackground() throws Exception {

            Document doc = new Document();
            try {
                PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("A4.pdf"));
                doc.open();
                doc.addTitle("Recruitment PET Sheett - " + "Created By SOS : ");
                doc.addSubject("Confidential Report Eyes Only");
                doc.addKeywords("");
                doc.addAuthor("SOS");
                doc.addCreator("SOS");

                // A4 = 210mm x 297mm ~ 605points x 855points
                doc.setPageSize(PageSize.A4);
                doc.setMargins(10f, 10f, 10f, 10f);

                /////////////////////////////////////////////////////////////
                int pageno = 1;

                for (int i = 0; i == pageno; i++) {

                    //    doc.add(imageRight);
                }

                PdfContentByte cb = writer.getDirectContent();

                int i = 1;
                while (i <= pageno) {
                    doc.newPage();

                    i++;
                }
                BaseFont labelFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", true);
                float width = doc.getPageSize().getWidth();
                float height = doc.getPageSize().getHeight();
                for (float h = 0; h <= height; h++) {
                    for (float w = 0; w <= width; w++) {
                        cb.beginText();
                        // cb.setColorFill(TITLE_COLOR);
                        cb.setFontAndSize(labelFont, 2);
                        cb.setTextMatrix(w, h);

                        cb.showText(String.valueOf(h) + ":" + String.valueOf(w));
                        cb.endText();
                        w += 15;
                    }
                    h += 2;
                }
                cb.beginText();
                // cb.setColorFill(TITLE_COLOR);
                cb.setFontAndSize(labelFont, 4);
                cb.setTextMatrix(20f, 220f);
                cb.showText("abcd");
                cb.endText();

            } catch (Exception e) {
                System.err.println(e.getMessage());

                ConsoleMsg(e.getMessage());
            } finally {
                doc.close();
            }

            ConsoleMsg("PDF... GENERATED");

            return null;
            //    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }.execute();

    // TODO add your handling code here:
}

From source file:co.ordinate.printer.NewJFrame.java

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    new SwingWorker<Object, Object>() {
        String filename;//  ww w . java  2  s.  c o  m

        @Override
        protected void done() {

            ConsoleMsg("Printing PROFILE SHEET IN PROGRESS.. ");

        }

        @Override
        protected Object doInBackground() throws Exception {

            Document doc = new Document();
            try {
                PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("letter.pdf"));
                doc.open();
                doc.addTitle("Recruitment PET Sheett - " + "Created By SOS : ");
                doc.addSubject("Confidential Report Eyes Only");
                doc.addKeywords("");
                doc.addAuthor("SOS");
                doc.addCreator("SOS");

                // A4 = 210mm x 297mm ~ 605points x 855points
                doc.setPageSize(PageSize.LETTER);
                doc.setMargins(10f, 10f, 10f, 10f);

                /////////////////////////////////////////////////////////////
                int pageno = 1;

                for (int i = 0; i == pageno; i++) {

                    //    doc.add(imageRight);
                }

                PdfContentByte cb = writer.getDirectContent();

                int i = 1;
                while (i <= pageno) {
                    doc.newPage();

                    i++;
                }
                BaseFont labelFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", true);
                float width = doc.getPageSize().getWidth();
                float height = doc.getPageSize().getHeight();
                for (float h = 0; h <= height; h++) {
                    for (float w = 0; w <= width; w++) {
                        cb.beginText();
                        // cb.setColorFill(TITLE_COLOR);
                        cb.setFontAndSize(labelFont, 4);
                        cb.setTextMatrix(w, h);

                        cb.showText(String.valueOf(h) + ":" + String.valueOf(w));
                        cb.endText();
                        w += 25;
                    }
                    h += 5;
                }
                cb.beginText();
                // cb.setColorFill(TITLE_COLOR);
                cb.setFontAndSize(labelFont, 4);
                cb.setTextMatrix(20f, 220f);
                cb.showText("abcd");
                cb.endText();

            } catch (Exception e) {
                System.err.println(e.getMessage());

                ConsoleMsg(e.getMessage());
            } finally {
                doc.close();
            }

            ConsoleMsg("PDF... GENERATED");

            return null;
            //    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }.execute();

    // TODO add your handling code here:
}

From source file:co.ordinate.printer.NewJFrame.java

private void printA6Sheet() {
    new SwingWorker<Object, Object>() {
        String filename;//from  w w  w .java2  s .  c  o m

        @Override
        protected void done() {

            ConsoleMsg("Printing PROFILE SHEET IN PROGRESS.. ");
            System.err.println("Printing Done");
        }

        @Override
        protected Object doInBackground() throws Exception {

            Document doc = new Document();
            try {
                PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream("A6.pdf"));
                doc.open();
                doc.addTitle("Recruitment PET Sheett - " + "Created By SOS : ");
                doc.addSubject("Confidential Report Eyes Only");
                doc.addKeywords("");
                doc.addAuthor("SOS");
                doc.addCreator("SOS");

                // A4 = 210mm x 297mm ~ 605points x 855points
                doc.setPageSize(PageSize.A5);
                doc.setMargins(10f, 10f, 10f, 10f);

                /////////////////////////////////////////////////////////////
                int pageno = 1;

                for (int i = 0; i == pageno; i++) {

                    //    doc.add(imageRight);
                }

                PdfContentByte cb = writer.getDirectContent();

                int i = 1;
                while (i <= pageno) {
                    doc.newPage();

                    i++;
                }
                BaseFont labelFont = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", true);
                float width = doc.getPageSize().getWidth();
                float height = doc.getPageSize().getHeight();
                for (float h = 0; h <= height; h++) {
                    for (float w = 0; w <= width; w++) {
                        cb.beginText();
                        // cb.setColorFill(TITLE_COLOR);
                        cb.setFontAndSize(labelFont, 4);
                        cb.setTextMatrix(w, h);

                        cb.showText(String.valueOf(h) + ":" + String.valueOf(w));
                        cb.endText();
                        w += 30;
                    }
                    h += 8;
                }
                cb.beginText();
                // cb.setColorFill(TITLE_COLOR);
                cb.setFontAndSize(labelFont, 4);
                cb.setTextMatrix(20f, 220f);
                cb.showText("abcd");
                cb.endText();

            } catch (Exception e) {
                System.err.println(e.getMessage());

                ConsoleMsg(e.getMessage());
            } finally {
                doc.close();
            }

            ConsoleMsg("PDF... GENERATED");
            System.err.println("Printing Done");
            return null;
            //    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }.execute(); // TODO add your handling code here:

}

From source file:com.automaster.autoview.server.servlet.TextStateOperators.java

/**
 * Creates a PDF document./*  w  w w .  j av  a  2s . c o  m*/
 * @param filename the path to the new PDF document
 * @throws DocumentException 
 * @throws IOException
 */
public void createPdf(String filename) throws IOException, DocumentException {
    // step 1
    Rectangle rect = new Rectangle(595, 842);
    Document document = new Document(rect, 30, 30, 30, 30);
    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
    // step 3
    document.open();
    // step 4
    //PdfAcroForm canva = writer.getAcroForm();

    BaseFont bf = BaseFont.createFont();

    //canva.addMultiLineTextField("txtTeste", "texto de teste vamos ver se le quebra a linha", bf, 15, 300, 800, 100, 100);

    PdfContentByte canvas = writer.getDirectContent();
    String text = "AWAY again";
    String text1 = "TESTE Rua alameda luis de sousa santos, N 52, castanhal, CEP: 68742-783, ao lado do posto adriano";
    canvas.beginText();
    // line 1
    canvas.setFontAndSize(bf, 16);
    canvas.moveText(36, 800);
    canvas.moveTextWithLeading(0, -24);
    canvas.setTextRise(10);
    canvas.showText(text);
    // line 2
    canvas.setWordSpacing(20);
    canvas.newlineShowText(text);
    // line 3
    canvas.setCharacterSpacing(10);
    canvas.newlineShowText(text);
    canvas.setWordSpacing(0);
    canvas.setCharacterSpacing(0);
    // line 4
    canvas.setHorizontalScaling(50);
    canvas.newlineShowText(text);
    canvas.setHorizontalScaling(100);
    // line 5
    canvas.newlineShowText(text);
    canvas.setTextRise(15);
    canvas.setFontAndSize(bf, 12);
    canvas.setColorFill(BaseColor.RED);
    canvas.showText("2");
    canvas.setColorFill(GrayColor.GRAYBLACK);
    // line 6
    canvas.setLeading(56);
    canvas.setMiterLimit(5);
    canvas.newlineShowText(
            "Rua alameda luis de sousa santos, N 52, castanhal, CEP: 68742-783, ao lado do posto" + text);
    canvas.setLeading(24);
    canvas.newlineText();
    Paragraph paragraph = new Paragraph("teste");
    PdfWriter pdfWriter = canvas.getPdfWriter();
    PdfAcroForm pdfAcroForm = pdfWriter.getAcroForm();
    //pdfAcroForm.addMultiLineTextField("txtTeste", "Changing the adriano leading: Rua alameda luis de sousa santos, N 52, castanhal, CEP: 68742-783, ao lado do posto", bf, 12, 30, 650, 550, 750);
    //pdfAcroForm.addHiddenField("txtTeste", text); 
    PdfFormField pdfFormField = new PdfFormField(pdfWriter, 30, 650, 550, 750, null);
    pdfFormField.setFieldName("txtTeste");
    pdfFormField.setValueAsString(text1);
    //TextField textField = new TextField(canvas.getPdfWriter(), new Rectangle(30, 650, 550, 750), "txtTest");
    //textField.setText(text);
    pdfAcroForm.addFormField(pdfFormField);
    // line 7
    PdfTextArray array = new PdfTextArray("A");
    array.add(120);
    array.add("W");
    array.add(120);
    array.add("A");
    array.add(95);
    array.add("Y again");
    canvas.showText(array);
    canvas.endText();

    canvas.setColorFill(BaseColor.BLUE);
    canvas.beginText();
    canvas.setTextMatrix(360, 770);
    canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL);
    canvas.setFontAndSize(bf, 24);
    canvas.showText(text);
    canvas.endText();

    canvas.beginText();
    canvas.setTextMatrix(360, 730);
    canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE);
    canvas.setFontAndSize(bf, 24);
    canvas.showText(text);
    canvas.endText();

    canvas.beginText();
    canvas.setTextMatrix(360, 690);
    canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
    canvas.setFontAndSize(bf, 24);
    canvas.showText(text);
    canvas.endText();

    canvas.beginText();
    canvas.setTextMatrix(360, 650);
    canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE);
    canvas.setFontAndSize(bf, 24);
    canvas.showText(text);
    canvas.endText();

    PdfTemplate template = canvas.createTemplate(200, 36);
    template.setLineWidth(2);
    for (int i = 0; i < 6; i++) {
        template.moveTo(0, i * 6);
        template.lineTo(200, i * 6);
    }
    template.stroke();

    canvas.saveState();
    canvas.beginText();
    canvas.setTextMatrix(360, 610);
    canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_CLIP);
    canvas.setFontAndSize(bf, 24);
    canvas.showText(text);
    canvas.endText();
    canvas.addTemplate(template, 360, 610);
    canvas.restoreState();

    canvas.saveState();
    canvas.beginText();
    canvas.setTextMatrix(360, 570);
    canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_STROKE_CLIP);
    canvas.setFontAndSize(bf, 24);
    canvas.showText(text);
    canvas.endText();
    canvas.addTemplate(template, 360, 570);
    canvas.restoreState();

    canvas.saveState();
    canvas.beginText();
    canvas.setTextMatrix(360, 530);
    canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE_CLIP);
    canvas.setFontAndSize(bf, 24);
    canvas.showText(text);
    canvas.endText();
    canvas.addTemplate(template, 360, 530);
    canvas.restoreState();

    canvas.saveState();
    canvas.beginText();
    canvas.setTextMatrix(360, 490);
    canvas.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_CLIP);
    canvas.setFontAndSize(bf, 24);
    canvas.showText(text);
    canvas.endText();
    canvas.addTemplate(template, 360, 490);
    canvas.restoreState();

    // step 5
    //document.add((Element) pdfAcroForm);
    document.close();
}

From source file:com.dandymadeproductions.ajqvue.io.PDFDataTableDumpThread.java

License:Open Source License

public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb;
    String text;//from   w  w w .  j av  a  2s. c om
    float textSize;
    float textBase;
    float adjust;

    cb = writer.getDirectContent();
    textBase = document.bottom() - 20;

    cb.beginText();
    cb.setFontAndSize(BASE_FONT, 12);
    adjust = BASE_FONT.getWidthPoint("000", 12);

    cb.setTextMatrix(document.bottom(), textBase);
    cb.showText("Produced by iTextpdf");

    text = "Page " + writer.getPageNumber() + " of ";
    textSize = BASE_FONT.getWidthPoint(text, 12);
    cb.setTextMatrix(document.right() - textSize - adjust, textBase);
    cb.showText(text);

    cb.endText();

    if (pdfTemplate != null)
        cb.addTemplate(pdfTemplate, document.right() - adjust, textBase);
}

From source file:com.dandymadeproductions.myjsqlview.io.PDFDataTableDumpThread.java

License:Open Source License

public void onEndPage(PdfWriter writer, Document document) {
    PdfContentByte cb = writer.getDirectContent();
    String text = "Page " + writer.getPageNumber() + " of ";
    float textSize = BASE_FONT.getWidthPoint(text, 12);
    float textBase = document.bottom() - 20;
    cb.beginText();//from  w w w.ja va  2s . c  o m
    cb.setFontAndSize(BASE_FONT, 12);
    float adjust = BASE_FONT.getWidthPoint("000", 12);
    cb.setTextMatrix(document.right() - textSize - adjust, textBase);
    cb.showText(text);
    cb.endText();
    cb.addTemplate(pdfTemplate, document.right() - adjust, textBase);
}

From source file:com.ephesoft.dcma.imagemagick.MultiPageExecutor.java

License:Open Source License

/**
 * The <code>addImageToPdf</code> method is used to add image to pdf and make it searchable by adding image text in invisible mode
 * w.r.t parameter 'isPdfSearchable' passed.
 * //from w w w  . j  a va2 s  . c  om
 * @param pdfWriter {@link PdfWriter} writer of pdf in which image has to be added
 * @param htmlUrl {@link HocrPage} corresponding html file for fetching text and coordinates
 * @param imageUrl {@link String} url of image to be added in pdf
 * @param isPdfSearchable true for searchable pdf else otherwise
 * @param widthOfLine
 */
private void addImageToPdf(PdfWriter pdfWriter, HocrPage hocrPage, String imageUrl, boolean isPdfSearchable,
        final int widthOfLine) {
    if (null != pdfWriter && null != imageUrl && imageUrl.length() > 0) {
        try {
            LOGGER.info("Adding image" + imageUrl + " to pdf using iText");
            Image pageImage = Image.getInstance(imageUrl);
            float dotsPerPointX = pageImage.getDpiX() / PDF_RESOLUTION;
            float dotsPerPointY = pageImage.getDpiY() / PDF_RESOLUTION;
            PdfContentByte pdfContentByte = pdfWriter.getDirectContent();

            pageImage.scaleToFit(pageImage.getWidth() / dotsPerPointX, pageImage.getHeight() / dotsPerPointY);

            pageImage.setAbsolutePosition(0, 0);

            // Add image to pdf
            pdfWriter.getDirectContentUnder().addImage(pageImage);
            pdfWriter.getDirectContentUnder().add(pdfContentByte);

            // If pdf is to be made searchable
            if (isPdfSearchable) {
                LOGGER.info("Adding invisible text for image: " + imageUrl);
                float pageImagePixelHeight = pageImage.getHeight();
                Font defaultFont = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.BOLD, CMYKColor.BLACK);

                // Fetch text and coordinates for image to be added
                Map<String, int[]> textCoordinatesMap = getTextWithCoordinatesMap(hocrPage, widthOfLine);
                Set<String> ketSet = textCoordinatesMap.keySet();

                // Add text at specific location
                for (String key : ketSet) {
                    int[] coordinates = textCoordinatesMap.get(key);
                    float bboxWidthPt = (coordinates[2] - coordinates[0]) / dotsPerPointX;
                    float bboxHeightPt = (coordinates[3] - coordinates[1]) / dotsPerPointY;
                    pdfContentByte.beginText();

                    // To make text added as invisible
                    pdfContentByte.setTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE);
                    pdfContentByte.setLineWidth(Math.round(bboxWidthPt));

                    // Ceil is used so that minimum font of any text is 1
                    // For exception of unbalanced beginText() and endText()
                    if (bboxHeightPt > 0.0) {
                        pdfContentByte.setFontAndSize(defaultFont.getBaseFont(),
                                (float) Math.ceil(bboxHeightPt));
                    } else {
                        pdfContentByte.setFontAndSize(defaultFont.getBaseFont(), 1);
                    }
                    float xCoordinate = (float) (coordinates[0] / dotsPerPointX);
                    float yCoordinate = (float) ((pageImagePixelHeight - coordinates[3]) / dotsPerPointY);
                    pdfContentByte.moveText(xCoordinate, yCoordinate);
                    pdfContentByte.showText(key);
                    pdfContentByte.endText();
                }
            }
            pdfContentByte.closePath();
        } catch (BadElementException badElementException) {
            LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: "
                    + badElementException.toString());
        } catch (DocumentException documentException) {
            LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: "
                    + documentException.toString());
        } catch (MalformedURLException malformedURLException) {
            LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: "
                    + malformedURLException.toString());
        } catch (IOException ioException) {
            LOGGER.error("Error occurred while adding image" + imageUrl + " to pdf using Itext: "
                    + ioException.toString());
        }
    }
}

From source file:com.example.admin.avoidq.billGenerated.java

private void createHeadings(PdfContentByte cb, float x, float y, String text) {

    cb.beginText();/*from w  w  w  .  j  av  a 2 s . c  o  m*/
    cb.setFontAndSize(bfBold, 8);
    cb.setTextMatrix(x, y);
    cb.showText(text.trim());
    cb.endText();

}