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

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

Introduction

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

Prototype


public void fillStroke() 

Source Link

Document

Fills the path using the non-zero winding number rule to determine the region to fill and strokes it.

Usage

From source file:nz.ac.waikato.cms.doc.SimplePDFOverlay.java

License:Open Source License

/**
 * Applies the instructions to the input PDF.
 *
 * @return      null if successful, otherwise error message
 *//*  w ww. java  2 s  .co m*/
public String execute() {
    String result;
    String line;
    BufferedReader breader;
    FileReader freader;
    int i;
    int lineNo;
    String units;
    int pageNo;
    PdfReader reader;
    PdfStamper stamper;
    PdfContentByte cb;
    ColumnText ct;
    Font font;
    String[] parts;
    StringBuilder text;

    result = null;

    freader = null;
    breader = null;
    try {
        reader = new PdfReader(new FileInputStream(m_Pdf.getAbsolutePath()));
        stamper = new PdfStamper(reader, new FileOutputStream(m_Output.getAbsolutePath()));
        freader = new FileReader(m_Instructions);
        breader = new BufferedReader(freader);
        lineNo = 0;
        units = "pt";
        pageNo = 1;
        cb = stamper.getOverContent(pageNo);
        font = null;
        while ((line = breader.readLine()) != null) {
            lineNo++;
            if (line.trim().startsWith(PREFIX_COMMENT))
                continue;
            if (line.trim().length() == 0)
                continue;
            if (line.startsWith(PREFIX_UNITS)) {
                units = line.substring(PREFIX_UNITS.length()).trim().toLowerCase();
            } else if (line.startsWith(PREFIX_PAGE)) {
                pageNo = Integer.parseInt(line.substring(PREFIX_PAGE.length()).trim());
                cb = stamper.getOverContent(pageNo);
            } else if (line.startsWith(PREFIX_FONT)) {
                parts = line.substring(PREFIX_FONT.length()).trim().split(" ");
                if (parts.length == 3)
                    font = FontFactory.getFont(parts[0], Float.parseFloat(parts[1]),
                            new BaseColor(parseColor(parts[2]).getRGB()));
                else
                    m_Logger.warning("Font instruction not in expected format (" + FORMAT_FONT + "):\n" + line);
            } else if (line.startsWith(PREFIX_TEXT)) {
                parts = line.substring(PREFIX_TEXT.length()).trim().split(" ");
                if (parts.length >= 7) {
                    ct = new ColumnText(cb);
                    ct.setSimpleColumn(parseLocation(parts[0], reader.getPageSize(pageNo).getWidth(), units), // llx
                            parseLocation(parts[1], reader.getPageSize(pageNo).getHeight(), units), // lly
                            parseLocation(parts[2], reader.getPageSize(pageNo).getWidth(), units), // urx
                            parseLocation(parts[3], reader.getPageSize(pageNo).getHeight(), units), // ury
                            Float.parseFloat(parts[4]), // leading
                            parseAlignment(parts[5])); // alignment
                    text = new StringBuilder();
                    for (i = 6; i < parts.length; i++) {
                        if (text.length() > 0)
                            text.append(" ");
                        text.append(parts[i]);
                    }
                    if (font == null)
                        ct.setText(new Phrase(text.toString()));
                    else
                        ct.setText(new Phrase(text.toString(), font));
                    ct.go();
                } else {
                    m_Logger.warning("Text instruction not in expected format (" + FORMAT_TEXT + "):\n" + line);
                }
            } else if (line.startsWith(PREFIX_LINE)) {
                parts = line.substring(PREFIX_LINE.length()).trim().split(" ");
                if (parts.length >= 6) {
                    cb.saveState();
                    cb.setLineWidth(Float.parseFloat(parts[4])); // line width
                    cb.setColorStroke(new BaseColor(parseColor(parts[5]).getRGB())); // color
                    cb.moveTo(parseLocation(parts[0], reader.getPageSize(pageNo).getWidth(), units), // x
                            parseLocation(parts[1], reader.getPageSize(pageNo).getWidth(), units)); // y
                    cb.lineTo(parseLocation(parts[2], reader.getPageSize(pageNo).getWidth(), units), // w
                            parseLocation(parts[3], reader.getPageSize(pageNo).getWidth(), units)); // h
                    cb.stroke();
                    cb.restoreState();
                } else {
                    m_Logger.warning("Line instruction not in expected format (" + FORMAT_LINE + "):\n" + line);
                }
            } else if (line.startsWith(PREFIX_RECT)) {
                parts = line.substring(PREFIX_RECT.length()).trim().split(" ");
                if (parts.length >= 6) {
                    cb.saveState();
                    cb.rectangle(parseLocation(parts[0], reader.getPageSize(pageNo).getWidth(), units), // x
                            parseLocation(parts[1], reader.getPageSize(pageNo).getWidth(), units), // y
                            parseLocation(parts[2], reader.getPageSize(pageNo).getWidth(), units), // w
                            parseLocation(parts[3], reader.getPageSize(pageNo).getWidth(), units) // h
                    );
                    cb.setLineWidth(Float.parseFloat(parts[4])); // line width
                    cb.setColorStroke(new BaseColor(parseColor(parts[5]).getRGB())); // stroke
                    if (parts.length >= 7) {
                        cb.setColorFill(new BaseColor(parseColor(parts[6]).getRGB())); // fill
                        cb.fillStroke();
                    } else {
                        cb.stroke();
                    }
                    cb.restoreState();
                } else {
                    m_Logger.warning(
                            "Rectangle instruction not in expected format (" + FORMAT_RECT + "):\n" + line);
                }
            } else if (line.startsWith(PREFIX_OVAL)) {
                parts = line.substring(PREFIX_OVAL.length()).trim().split(" ");
                if (parts.length >= 6) {
                    cb.saveState();
                    cb.ellipse(parseLocation(parts[0], reader.getPageSize(pageNo).getWidth(), units), // x1
                            parseLocation(parts[1], reader.getPageSize(pageNo).getWidth(), units), // y1
                            parseLocation(parts[2], reader.getPageSize(pageNo).getWidth(), units), // x2
                            parseLocation(parts[3], reader.getPageSize(pageNo).getWidth(), units) // y2
                    );
                    cb.setLineWidth(Float.parseFloat(parts[4])); // line width
                    cb.setColorStroke(new BaseColor(parseColor(parts[5]).getRGB())); // stroke
                    if (parts.length >= 7) {
                        cb.setColorFill(new BaseColor(parseColor(parts[6]).getRGB())); // fill
                        cb.fillStroke();
                    } else {
                        cb.stroke();
                    }
                    cb.restoreState();
                } else {
                    m_Logger.warning("Oval instruction not in expected format (" + FORMAT_OVAL + "):\n" + line);
                }
            } else {
                m_Logger.warning("Unknown command on line #" + lineNo + ":\n" + line);
            }
        }
        stamper.close();
    } catch (Exception e) {
        result = "Failed to process!\n" + Utils.throwableToString(e);
    } finally {
        FileUtils.closeQuietly(breader);
        FileUtils.closeQuietly(freader);
    }

    return result;
}

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

License:Open Source License

public void renderPDF(Item item, PDFTarget target, PreviewProperties properties) {
    Float x = item.getData(NodeItem.X);
    Float y = item.getData(NodeItem.Y);
    Float size = item.getData(NodeItem.SIZE);
    size /= 2f;// w  ww.ja  v  a  2s .  c  om
    Color color = item.getData(NodeItem.COLOR);
    Color borderColor = ((DependantColor) properties.getValue(PreviewProperty.NODE_BORDER_COLOR))
            .getColor(color);
    float borderSize = properties.getFloatValue(PreviewProperty.NODE_BORDER_WIDTH);
    float alpha = properties.getFloatValue(PreviewProperty.NODE_OPACITY) / 100f;

    PdfContentByte cb = target.getContentByte();
    cb.setRGBColorStroke(borderColor.getRed(), borderColor.getGreen(), borderColor.getBlue());
    cb.setLineWidth(borderSize);
    cb.setRGBColorFill(color.getRed(), color.getGreen(), color.getBlue());
    if (alpha < 1f) {
        cb.saveState();
        PdfGState gState = new PdfGState();
        gState.setFillOpacity(alpha);
        gState.setStrokeOpacity(alpha);
        cb.setGState(gState);
    }
    cb.circle(x, -y, size);
    if (borderSize > 0) {
        cb.fillStroke();
    } else {
        cb.fill();
    }
    if (alpha < 1f) {
        cb.restoreState();
    }
}

From source file:PDF.CrearPDF_Ficha.java

public static void drawRectangle(PdfContentByte content, float x, float y, float width, float height) {
    try {//from  w w  w.j a va 2 s.c o  m

        content.saveState();

        PdfGState state = new PdfGState();
        content.setGState(state);
        content.setRGBColorFill(232, 232, 232);
        content.setColorStroke(BaseColor.BLUE);
        content.setLineWidth((float) .5);
        content.rectangle(x, y, width, height);
        content.fillStroke();
        content.restoreState();

        BaseFont bf = BaseFont.createFont();
        float fontSize = 15f;
        Phrase phrase = new Phrase("Foto", new Font(bf, fontSize));
        ColumnText.showTextAligned(content, Element.ALIGN_CENTER, phrase, 475, 687, 0);
    } catch (DocumentException ex) {
        Logger.getLogger(CrearPDF_Ficha.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(CrearPDF_Ficha.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:se.inera.intyg.intygstyper.fk7263.pdf.PdfAbstractGenerator.java

License:Open Source License

protected void createSignatureNotRequiredField(PdfStamper pdfStamper, int lastPage)
        throws DocumentException, IOException {
    PdfContentByte addOverlay;
    addOverlay = pdfStamper.getOverContent(lastPage);
    addOverlay.saveState();/*  w  w w  .ja v a2  s  .co  m*/
    addOverlay.setColorFill(SIGNATURE_NOT_REQUIRED_COLOR);
    addOverlay.setColorStroke(CMYKColor.BLACK);
    addOverlay.rectangle(SIGNATURE_NOT_REQUIRED_START_X, SIGNATURE_NOT_REQUIRED_START_Y,
            SIGNATURE_NOT_REQUIRED_WIDTH, SIGNATURE_NOT_REQUIRED_HEIGHT);
    addOverlay.setLineWidth(LINE_WIDTH);
    addOverlay.fillStroke();
    addOverlay.restoreState();
    // Do text
    addOverlay = pdfStamper.getOverContent(lastPage);
    addOverlay.saveState();
    BaseFont bf = BaseFont.createFont();
    addOverlay.beginText();
    addOverlay.setFontAndSize(bf, SIGNATURE_NOT_REQUIRED_FONT_SIZE);
    addOverlay.setTextMatrix(SIGNATURE_NOT_REQUIRED_START_X + SIGNATURE_NOT_REQUIRED_PADDING_LEFT,
            SIGNATURE_NOT_REQUIRED_START_Y + SIGNATURE_NOT_REQUIRED_PADDING_BOTTOM);
    addOverlay.showText(SIGNATURE_NOT_REQUIRED_TEXT);
    addOverlay.endText();
    addOverlay.restoreState();
}

From source file:se.inera.intyg.intygstyper.fk7263.pdf.PdfAbstractGenerator.java

License:Open Source License

protected void maskSendToFkInformation(PdfStamper pdfStamper) {
    PdfContentByte addOverlay;
    addOverlay = pdfStamper.getOverContent(1);
    addOverlay.saveState();//from  www .j a v  a 2s  . c o  m
    addOverlay.setColorFill(CMYKColor.WHITE);
    addOverlay.setColorStroke(CMYKColor.WHITE);
    addOverlay.rectangle(MASK_START_X, MASK_START_Y, MASK_WIDTH, MASK_HEIGTH);
    addOverlay.fillStroke();
    addOverlay.restoreState();
}