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

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

Introduction

In this page you can find the example usage for com.lowagie.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:classroom.newspaper_a.Newspaper01.java

public static void main(String[] args) {
    try {//from ww  w  . j a  v  a  2  s .c o  m
        PdfReader reader = new PdfReader(NEWSPAPER);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        PdfContentByte canvas = stamper.getOverContent(1);
        canvas.setRGBColorFill(0xC0, 0xC0, 0xC0);
        canvas.setRGBColorStroke(0xFF, 0x00, 0x00);
        canvas.rectangle(LLX1, LLY1, W1, H1);
        canvas.rectangle(LLX2, LLY2, W2, H2);
        canvas.fillStroke();
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:classroom.newspaper_a.Newspaper03.java

public static void main(String[] args) {
    try {// w w w .  ja va2  s .c o m
        PdfReader reader = new PdfReader(NEWSPAPER);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        PdfContentByte canvas = stamper.getOverContent(1);
        canvas.saveState();
        canvas.setRGBColorFill(0xFF, 0xFF, 0xFF);
        canvas.rectangle(LLX1, LLY1, W1, H1);
        canvas.rectangle(LLX2, LLY2, W2, H2);
        canvas.fillStroke();
        canvas.restoreState();
        BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
        putText(canvas, MESSAGE, bf, LLX1, LLY1, URX1, URY1);
        putText(canvas, MESSAGE, bf, LLX2, LLY2, URX2, URY2);
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:classroom.newspaper_a.Newspaper04.java

public static void main(String[] args) {
    try {/*from   w  w  w  .j  a  v a  2  s .  c om*/
        PdfReader reader = new PdfReader(NEWSPAPER);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        PdfContentByte canvas = stamper.getOverContent(1);
        canvas.saveState();
        canvas.setRGBColorFill(0xFF, 0xFF, 0xFF);
        canvas.rectangle(LLX1, LLY1, W1, H1);
        canvas.rectangle(LLX2, LLY2, W2, H2);
        canvas.fillStroke();
        canvas.restoreState();
        Phrase p = new Phrase(MESSAGE);
        putText(canvas, p, LLX1, LLY1, URX1, URY1);
        putText(canvas, p, LLX2, LLY2, URX2, URY2);
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:classroom.newspaper_a.Newspaper05.java

public static void main(String[] args) {
    try {/*w ww .j a  v a  2  s . c  o m*/
        PdfReader reader = new PdfReader(NEWSPAPER);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
        PdfContentByte canvas = stamper.getOverContent(1);
        canvas.saveState();
        canvas.setRGBColorFill(0xFF, 0xFF, 0xFF);
        canvas.rectangle(LLX1, LLY1, W1, H1);
        canvas.rectangle(LLX2, LLY2, W2, H2);
        canvas.fillStroke();
        canvas.restoreState();
        Phrase p = new Phrase(MESSAGE);
        putText(canvas, p, LLX1, LLY1, W1, H1);
        putText(canvas, p, LLX2, LLY2, W2, H2);
        stamper.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:com.actelion.research.spiritapp.print.CagePrinterPDF.java

License:Open Source License

public static void printCages(List<Container> cages, boolean printGroupsTreatments, boolean printTreatmentDesc,
        boolean whiteBackground) throws Exception {
    final int margin = 15;
    File f = File.createTempFile("cages__", ".pdf");
    Document doc = new Document(PageSize.A4.rotate());
    Image maleImg, femaleImg, nosexImg;
    try {//from   www  .j  ava 2 s . co  m
        maleImg = Image.getInstance(CagePrinterPDF.class.getResource("male.png"));
        femaleImg = Image.getInstance(CagePrinterPDF.class.getResource("female.png"));
        nosexImg = Image.getInstance(CagePrinterPDF.class.getResource("nosex.png"));
    } catch (Exception e) {
        throw new Exception("Could not find images in the classpath: " + e);
    }

    FileOutputStream os = new FileOutputStream(f);
    PdfWriter writer = PdfWriter.getInstance(doc, os);
    doc.open();

    PdfContentByte canvas = writer.getDirectContentUnder();

    float tileW = (doc.getPageSize().getWidth()) / 4;
    float tileH = (doc.getPageSize().getHeight()) / 2;
    for (int i = 0; i < cages.size(); i++) {
        Container cage = cages.get(i);

        Study study = cage.getStudy();
        Set<Group> groups = cage.getGroups();
        Group group = cage.getGroup();
        Set<Biosample> animals = new TreeSet<>(Biosample.COMPARATOR_NAME);
        animals.addAll(cage.getBiosamples());

        //Find the treatments applied to this group
        Set<NamedTreatment> allTreatments = new LinkedHashSet<>();
        for (Group gr : groups) {
            allTreatments.addAll(gr.getAllTreatments(-1));
        }

        //Draw
        if (i % 8 == 0) {
            if (i > 0)
                doc.newPage();
            drawCageSeparation(doc, canvas);
        }

        int col = (i % 8) % 4;
        int row = (i % 8) / 4;

        float x = margin + tileW * col;
        float x2 = x + tileW - margin;
        float baseY = tileH * row;
        float y;

        //Display Sex
        canvas.moveTo(tileW * col - 50, doc.getPageSize().getHeight() - (tileH * row + 50));
        Image img = null;
        String sex = getMetadata(animals, "Sex");
        if (study.isBlindAll()) {
            img = nosexImg;
        } else if (sex.equals("M")) {
            img = maleImg;
        } else if (sex.equals("F")) {
            img = femaleImg;
        } else if (sex.length() > 0) {
            img = nosexImg;
        }
        if (img != null) {
            img.scaleToFit(20, 20);
            img.setAbsolutePosition(tileW * (col + 1) - 33,
                    doc.getPageSize().getHeight() - (tileH * row + 12 + margin));
            doc.add(img);
        }
        if (group != null && group.getColor() != null && (study != null && !study.isBlind())
                && !whiteBackground) {
            Color c = group.getColor();
            canvas.saveState();
            canvas.setRGBColorFill(c.getRed() / 3 + 170, c.getGreen() / 3 + 170, c.getBlue() / 3 + 170);
            canvas.rectangle(x - margin + 1, doc.getPageSize().getHeight() - baseY - tileH + 1, tileW - 2,
                    tileH - 2);
            canvas.fill();
            canvas.restoreState();
        }
        Color treatmentColor = Color.BLACK;
        if (allTreatments.size() > 0 && !study.isBlind() && printGroupsTreatments) {
            int offset = 0;
            for (NamedTreatment t : allTreatments) {
                if (t.getColor() != null) {
                    if (allTreatments.size() == 1)
                        treatmentColor = new Color(t.getColor().getRed() / 2, t.getColor().getGreen() / 2,
                                t.getColor().getBlue() / 2);
                    canvas.saveState();
                    canvas.setColorStroke(Color.BLACK);
                    canvas.setRGBColorFill(t.getColor().getRed(), t.getColor().getGreen(),
                            t.getColor().getBlue());
                    y = baseY + 42 + 15 + 86 + 13 + 22 + 14 + 23 + 28;
                    canvas.rectangle(x + 20 + offset * 5, doc.getPageSize().getHeight() - y - (offset % 2) * 4,
                            22, 22);
                    canvas.fillStroke();
                    canvas.restoreState();
                    offset++;
                }
            }
        }

        canvas.beginText();
        canvas.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA_BOLD, "Cp1252", false), 16f);
        canvas.showTextAligned(Element.ALIGN_LEFT, cage.getContainerId(), x,
                doc.getPageSize().getHeight() - (baseY + 23), 0);

        canvas.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA_OBLIQUE, "Cp1252", false), 11f);
        y = 42 + baseY;
        canvas.showTextAligned(Element.ALIGN_LEFT, "Type: ", x, doc.getPageSize().getHeight() - y, 0);
        y += 15;
        canvas.showTextAligned(Element.ALIGN_LEFT, "Animals_ID: ", x, doc.getPageSize().getHeight() - y, 0);
        y += 86;
        canvas.showTextAligned(Element.ALIGN_LEFT, "Delivery date: ", x, doc.getPageSize().getHeight() - y, 0);
        y += 13;
        canvas.showTextAligned(Element.ALIGN_LEFT, "PO Number: ", x, doc.getPageSize().getHeight() - y, 0);
        y += 22;
        canvas.showTextAligned(Element.ALIGN_LEFT, "Study: ", x, doc.getPageSize().getHeight() - y, 0);
        y += 14;
        if (study != null && !study.isBlind() && printGroupsTreatments)
            canvas.showTextAligned(Element.ALIGN_LEFT, "Group: ", x, doc.getPageSize().getHeight() - y, 0);
        y += 23;
        if (study != null && !study.isBlind() && printGroupsTreatments)
            canvas.showTextAligned(Element.ALIGN_LEFT, "Treatment: ", x, doc.getPageSize().getHeight() - y, 0);
        y += 50;
        canvas.showTextAligned(Element.ALIGN_LEFT, "License: ", x, doc.getPageSize().getHeight() - y, 0);
        y += 13;
        canvas.showTextAligned(Element.ALIGN_LEFT, "Experimenter: ", x, doc.getPageSize().getHeight() - y, 0);
        canvas.endText();

        y = 42 + baseY;
        print(canvas, study.isBlindAll() ? "Blinded" : getMetadata(animals, "Type"), x + 65,
                doc.getPageSize().getHeight() - y, x2, 11, 11, FontFactory.HELVETICA, Color.BLACK, 11f);
        y += 15;

        if (animals.size() <= 6) {
            int n = 0;
            for (Biosample animal : animals) {
                print(canvas, animal.getSampleId(), x + 75, doc.getPageSize().getHeight() - y - 12 * n, x2 - 50,
                        12, 12, FontFactory.HELVETICA, Color.BLACK, 11f);
                if (animal.getSampleName() != null && animal.getSampleName().length() > 0) {
                    print(canvas, "[ " + animal.getSampleName() + " ]", x2 - 60,
                            doc.getPageSize().getHeight() - y - 12 * n, x2, 12, 12, FontFactory.HELVETICA_BOLD,
                            Color.BLACK, 11f);
                }
                n++;
            }
        } else {
            int nPerRow = animals.size() / 2;
            int n = 0;
            for (Biosample animal : animals) {
                int nx = n / nPerRow;
                int ny = n % nPerRow;
                print(canvas, animal.getSampleId(), x + nx * (x2 - x) / 2,
                        doc.getPageSize().getHeight() - y - 12 * ny - 12, x + (1 + nx) * (x2 - x) / 2, 12, 12,
                        FontFactory.HELVETICA, Color.BLACK, 10f);
                if (animal.getSampleName() != null && animal.getSampleName().length() > 0) {
                    print(canvas, "[ " + animal.getSampleName() + " ]", x + (1 + nx) * (x2 - x) / 2 - 35,
                            doc.getPageSize().getHeight() - y - 12 * ny - 12, x + (1 + nx) * (x2 - x) / 2, 12,
                            12, FontFactory.HELVETICA_BOLD, Color.BLACK, 10f);
                }
                n++;
            }
        }

        y += 86;
        print(canvas, getMetadata(animals, "Delivery Date"), x + 75, doc.getPageSize().getHeight() - y, x2, 0,
                10, FontFactory.HELVETICA, Color.BLACK, 10f);
        y += 13;
        print(canvas, getMetadata(animals, "PO Number"), x + 75, doc.getPageSize().getHeight() - y, x2, 0, 10,
                FontFactory.HELVETICA, Color.BLACK, 10f);
        y += 22;
        print(canvas, study.getStudyIdAndInternalId(), x + 40, doc.getPageSize().getHeight() - y, x2, 0, 11,
                BaseFont.HELVETICA_BOLD, Color.BLACK, 11f);
        y += 14;
        if (!study.isBlind() && printGroupsTreatments)
            print(canvas, getGroups(animals), x + 40, doc.getPageSize().getHeight() - y, x2, 22, 11,
                    BaseFont.HELVETICA_BOLD, Color.BLACK, 11f);
        y += 23;
        if (!study.isBlind() && printGroupsTreatments)
            print(canvas, getTreatments(animals, printTreatmentDesc), x + 62, doc.getPageSize().getHeight() - y,
                    x2, 50, printTreatmentDesc ? 9 : 12, FontFactory.HELVETICA, treatmentColor,
                    printTreatmentDesc ? 9f : 10f);
        y += 50;
        print(canvas, study.getMetadataMap().get("LICENSENO"), x + 74, doc.getPageSize().getHeight() - y, x2,
                15, 10, FontFactory.HELVETICA, Color.BLACK, 10f);
        y += 13;
        print(canvas, study.getMetadataMap().get("EXPERIMENTER"), x + 74, doc.getPageSize().getHeight() - y, x2,
                20, 10, FontFactory.HELVETICA, Color.BLACK, 10f);
    }

    doc.close();
    os.close();
    Desktop.getDesktop().open(f);
    try {
        Thread.sleep(500);
    } catch (Exception e) {
    }

}

From source file:com.virtusa.akura.student.controller.MessageBoardController.java

License:Open Source License

/**
 * Merge many pdf files into one file./*from  w  w w  .j a v  a  2  s .c o  m*/
 *
 * @param streamOfPDFFiles - a list of inputStreams
 * @param outputStream - an instance of outputStream
 * @param paginate - a boolean
 * @throws AkuraAppException - The exception details that occurred when processing
 */
public static void concatPDFs(List<InputStream> streamOfPDFFiles, OutputStream outputStream, boolean paginate)
        throws AkuraAppException {

    final int fontSize = 8, leftRightAlignment = 8;
    final int min = 0;
    final int max = 5;
    final int size = 300;
    final int xAxis = -150;
    final int pageHeight = 550;
    final int pageHieghtfromBottom = 16;
    final int pageRecHeight = 580;
    final int recHeightFromBottom = 14;
    Document document = new Document(PageSize.A4);
    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 output stream
        PdfWriter writer = null;
        BaseFont bf = null;
        try {
            writer = PdfWriter.getInstance(document, outputStream);

            document.open();

            bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        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()) {
                if (currentPageNumber != 2) {
                    document.newPage();
                }
                pageOfCurrentReaderPDF++;
                currentPageNumber++;
                page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF);
                if (currentPageNumber == 1) {
                    cb.addTemplate(page, 0, xAxis);
                } else if (currentPageNumber != 2) {
                    cb.addTemplate(page, 0, 0);
                }

                // Code for pagination.
                if (paginate) {
                    cb.beginText();
                    cb.setFontAndSize(bf, fontSize);
                    cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " " + AkuraWebConstant.REPORT_GPL, size,
                            max, min);
                    cb.newlineText();
                    cb.endText();

                    if (currentPageNumber != 2) {
                        int pageNo = currentPageNumber;
                        if (currentPageNumber != 1) {
                            pageNo = currentPageNumber - 1;
                        }

                        // write the page number inside a rectangle.
                        cb.fillStroke();
                        cb.rectangle(leftRightAlignment, recHeightFromBottom, pageRecHeight,
                                leftRightAlignment);
                        cb.beginText();
                        cb.showTextAligned(PdfContentByte.ALIGN_LEFT, PAGE + pageNo, pageHeight,
                                pageHieghtfromBottom, 0);
                        cb.endText();
                        cb.stroke();
                    }
                }
            }
            pageOfCurrentReaderPDF = 0;
        }

        outputStream.flush();

        document.close();

        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (document.isOpen()) {
            document.close();
        }
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

From source file:org.mapfish.print.map.renderers.vector.PointRenderer.java

License:Open Source License

protected void renderImpl(RenderingContext context, PdfContentByte dc, PJsonObject style, Point geometry) {
    PdfGState state = new PdfGState();
    final Coordinate coordinate = geometry.getCoordinate();
    float pointRadius = style.optFloat("pointRadius", 4.0f);
    final float f = context.getStyleFactor();

    String graphicName = style.optString("graphicName");
    float width = style.optFloat("graphicWidth", pointRadius * 2.0f);
    float height = style.optFloat("graphicHeight", pointRadius * 2.0f);
    float offsetX = style.optFloat("graphicXOffset", -width / 2.0f);
    float offsetY = style.optFloat("graphicYOffset", -height / 2.0f);
    // See Feature/Vector.js for more information about labels
    String label = style.optString("label");
    String labelAlign = style.optString("labelAlign", "lb");
    /*/*from   w  w  w.j  a  v  a 2 s  . c  o m*/
     * Valid values for horizontal alignment: "l"=left, "c"=center, "r"=right. 
     * Valid values for vertical alignment: "t"=top, "m"=middle, "b"=bottom.
     */
    float labelXOffset = style.optFloat("labelXOffset", (float) 0.0);
    float labelYOffset = style.optFloat("labelYOffset", (float) 0.0);
    String fontColor = style.optString("fontColor", "#000000");
    /* Supported itext fonts: COURIER, HELVETICA, TIMES_ROMAN */
    String fontFamily = style.optString("fontFamily", "HELVETICA");
    String fontSize = style.optString("fontSize", "12");
    String fontWeight = style.optString("fontWeight", "normal");

    if (style.optString("externalGraphic") != null) {
        float opacity = style.optFloat("graphicOpacity", style.optFloat("fillOpacity", 1.0f));
        state.setFillOpacity(opacity);
        state.setStrokeOpacity(opacity);
        dc.setGState(state);
        try {
            Image image = PDFUtils.createImage(context, width * f, height * f,
                    new URI(style.getString("externalGraphic")), 0.0f);
            image.setAbsolutePosition((float) coordinate.x + offsetX * f, (float) coordinate.y + offsetY * f);
            dc.addImage(image);
        } catch (BadElementException e) {
            context.addError(e);
        } catch (URISyntaxException e) {
            context.addError(e);
        } catch (DocumentException e) {
            context.addError(e);
        }

    } else if (graphicName != null && !graphicName.equalsIgnoreCase("circle")) {
        PolygonRenderer.applyStyle(context, dc, style, state);
        float[] symbol = SYMBOLS.get(graphicName);
        if (symbol == null) {
            throw new InvalidValueException("graphicName", graphicName);
        }
        dc.setGState(state);
        dc.moveTo((float) coordinate.x + symbol[0] * width * f + offsetX * f,
                (float) coordinate.y + symbol[1] * height * f + offsetY * f);
        for (int i = 2; i < symbol.length - 2; i += 2) {
            dc.lineTo((float) coordinate.x + symbol[i] * width * f + offsetX * f,
                    (float) coordinate.y + symbol[i + 1] * height * f + offsetY * f);

        }
        dc.closePath();
        dc.fillStroke();

    } else if (label != null && label.length() > 0) {
        BaseFont bf = PDFUtils.getBaseFont(fontFamily, fontSize, fontWeight);
        float fontHeight = (float) Double.parseDouble(fontSize.toLowerCase().replaceAll("px", "")) * f;
        dc.setFontAndSize(bf, fontHeight);
        dc.setColorFill(ColorWrapper.convertColor(fontColor));
        state.setFillOpacity((float) 1.0);
        dc.setGState(state);
        dc.beginText();
        dc.setTextMatrix((float) coordinate.x + labelXOffset * f, (float) coordinate.y + labelYOffset * f);
        dc.setGState(state);
        dc.showTextAligned(PDFUtils.getHorizontalAlignment(labelAlign), label,
                (float) coordinate.x + labelXOffset * f,
                (float) coordinate.y + labelYOffset * f - PDFUtils.getVerticalOffset(labelAlign, fontHeight),
                0);
        dc.endText();
    } else {
        PolygonRenderer.applyStyle(context, dc, style, state);
        dc.setGState(state);

        dc.circle((float) coordinate.x, (float) coordinate.y, pointRadius * f);
        dc.fillStroke();
    }
}