Example usage for com.itextpdf.text Rectangle setBackgroundColor

List of usage examples for com.itextpdf.text Rectangle setBackgroundColor

Introduction

In this page you can find the example usage for com.itextpdf.text Rectangle setBackgroundColor.

Prototype


public void setBackgroundColor(final BaseColor backgroundColor) 

Source Link

Document

Sets the backgroundcolor of the rectangle.

Usage

From source file:com.vectorprint.report.itext.debug.DebugHelper.java

License:Open Source License

public static void debugBackground(PdfContentByte canvas, Rectangle rect, BaseColor color, String prefix,
        EnhancedMap settings, LayerManager layerAware) {
    canvas = canvas.getPdfWriter().getDirectContentUnder();
    int rgb = color.getRed() + color.getBlue() + color.getGreen();
    rect.setBackgroundColor(color);
    canvas.rectangle(rect);//from ww w . j a  v a  2s. co  m
    layerAware.startLayerInGroup(DEBUG, canvas);
    debugFont(canvas, settings);
    BaseColor txtColor = (rgb < 150) ? color.brighter().brighter() : color.darker().darker();
    canvas.setColorFill(txtColor);
    canvas.setColorStroke(txtColor);
    canvas.beginText();
    canvas.showTextAligned(Element.ALIGN_LEFT, prefix + color.toString().replace(Color.class.getName(), ""),
            rect.getLeft() + rect.getWidth() / 2, rect.getTop() - rect.getHeight() / 2, 0);
    canvas.endText();
    canvas.endLayer();
}

From source file:com.vectorprint.report.itext.EventHelper.java

License:Open Source License

/**
 * prints debug info when property debug is true, calls renderHeader and renderFooter and
 * {@link Advanced#draw(com.itextpdf.text.Rectangle, java.lang.String) } with {@link Document#getPageSize() }
 * and null for {@link DefaultStylerFactory#PAGESTYLERS}.
 *
 * @param writer/*  w  w  w  . ja va 2  s.  c o  m*/
 * @param document
 */
@Override
public final void onEndPage(PdfWriter writer, Document document) {
    super.onEndPage(writer, document);
    sanitize(writer);
    try {
        if (failuresHereAfter || debugHereAfter) {
            PdfContentByte bg = writer.getDirectContentUnder();
            Rectangle rect = writer.getPageSize();
            rect.setBackgroundColor(itextHelper
                    .fromColor(getSettings().getColorProperty(new Color(240, 240, 240), "legendbackground")));
            bg.rectangle(rect);
            bg.closePathFillStroke();
        } else {
            for (Advanced a : doForAllPages) {
                try {
                    if (a.shouldDraw(null)) {
                        a.draw(document.getPageSize(), null);
                    }
                } catch (VectorPrintException ex) {
                    throw new VectorPrintRuntimeException(ex);
                }
            }
        }
        if (!debugHereAfter && getSettings().getBooleanProperty(false, DEBUG)) {

            PdfContentByte canvas = writer.getDirectContent();

            Rectangle rect = new Rectangle(document.leftMargin(), document.bottomMargin(),
                    document.right() - document.rightMargin(), document.top() - document.topMargin());

            DebugHelper.debugRect(canvas, rect, new float[] { 10, 2 }, 0.3f, getSettings(),
                    stylerFactory.getLayerManager());

        }

        renderHeader(writer, document);
        maxTagForGenericTagOnPage = ((DefaultElementProducer) elementProducer).getAdvancedTag();
        if (getSettings().getBooleanProperty(Boolean.FALSE, ReportConstants.PRINTFOOTER)) {
            renderFooter(writer, document);
        } else {
            log.warning("not printing footer, if you want page footers set " + ReportConstants.PRINTFOOTER
                    + " to true");
        }
        maxTagForGenericTagOnPage = Integer.MAX_VALUE;
    } catch (VectorPrintException | DocumentException | InstantiationException | IllegalAccessException e) {
        throw new VectorPrintRuntimeException("failed to create the report header or footer: ", e);
    }
}

From source file:com.vectorprint.report.itext.style.stylers.Background.java

License:Open Source License

@Override
public <E> E style(E text, Object data) throws VectorPrintException {
    Rectangle cell = (Rectangle) text;

    if (getBackgroundColor() != null) {
        cell.setBackgroundColor(itextHelper.fromColor(getBackgroundColor()));
    }/*ww  w  . j a va 2s .  com*/

    return text;
}

From source file:com.vectorprint.report.itext.style.stylers.Page.java

License:Open Source License

private void pageSettings() {
    if (getBackground() != null) {
        if (log.isLoggable(Level.FINE)) {
            log.fine("Possibly page background is written on top of page content making it invisible");
        }/*  w  ww  .  j  a  va  2  s .c  o m*/
        PdfContentByte bg = getWriter().getDirectContentUnder();
        Rectangle rect = getWriter().getPageSize();
        rect.setBackgroundColor(itextHelper.fromColor(getBackground()));
        bg.rectangle(rect);
        bg.closePathFillStroke();
    }
    getDocument().setPageSize(new Rectangle(getWidth(), getHeight()));
    getDocument().setMargins(getMargin_left(), getMargin_right(), getMargin_top(), getMargin_bottom());
}

From source file:com.vectorprint.report.itext.style.ZebraStripes.java

License:Open Source License

protected void paintRow(int row, Rectangle rect, PdfContentByte[] canvases, boolean last, boolean first) {
    if (settings.getBooleanProperty(false, DEBUG)) {
        DebugHelper.debugBackground(canvases[PdfPTable.BACKGROUNDCANVAS], rect,
                itextHelper.fromColor(rowBackgroundColor), "zebra", settings, layerManager);
        return;// w  w  w  .  j  a  va  2 s .c o  m
    }

    rect.setBackgroundColor(itextHelper.fromColor(rowBackgroundColor));
    rect.setBorder(Rectangle.NO_BORDER);
    canvases[PdfPTable.BACKGROUNDCANVAS].rectangle(rect);
}

From source file:Forms.AddCustomers.java

private void cardBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cardBtnActionPerformed
    String name = fNameTxt.getText() + " " + lNameTxt.getText();
    String cid = cusIdTxt.getText();
    Document document = new Document();
    Rectangle test = new Rectangle(350, 200);
    BaseColor color = new BaseColor(63, 72, 204);
    test.setBackgroundColor(color);
    document.setPageSize(test);/*from   w ww.  ja va 2s. co m*/

    File theDir = new File("E:\\CustomerCard");

    // if the directory does not exist, create it
    if (!theDir.exists()) {
        //System.out.println("creating directory: " + directoryName);
        boolean result = false;

        try {
            theDir.mkdir();
            result = true;
        } catch (SecurityException se) {
            System.out.println(se.getMessage());
        }
    }

    try {
        PdfWriter.getInstance(document, new FileOutputStream("E:\\CustomerCard\\card(" + cid + ").pdf"));

        document.open();
        //Image image;

        //image = Image.getInstance("api.png");

        //document.add(image);

        document.add(new Paragraph("           Customer Discount Card",
                FontFactory.getFont(FontFactory.TIMES_BOLD, 16, Font.BOLD, BaseColor.GRAY)));
        document.add(new Paragraph("     "));
        document.add(new Paragraph("              Name : " + name,
                FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.BLACK)));
        document.add(new Paragraph("     "));
        document.add(new Paragraph("              Card ID: " + cid,
                FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL, BaseColor.BLACK)));
        document.close();

        try {
            //Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "+"E:\\CustomerCard\\card("+cid+").pdf");
            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "
                    + "C:\\Users\\Vidu\\Desktop\\CustomerIDCard\\card(" + cid + ").pdf");
        } catch (Exception ex) {
            Logger.getLogger(SellPage.class.getName()).log(Level.SEVERE, null, ex);
        }
        //document.add(new Paragraph(date,FontFactory.getFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL,BaseColor.BLACK)));
        fNameTxt.setText("");
        lNameTxt.setText("");
        cusIdTxt.setText("");
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:info.longnetpro.examples.PdfLibExamples.java

public static void generatePdf()
        throws DocumentException, URISyntaxException, MalformedURLException, IOException {
    String licFile = getLicenseFilePath();
    loadLicenseFile(licFile);//from   ww  w .  ja  v a  2s.  c  o m

    String dest = getTargetFilePath();
    Document doc = new Document();
    PdfWriter.getInstance(doc, new FileOutputStream(dest));
    Rectangle pageSize = PageSize.LETTER;
    Rectangle rect = new Rectangle(0f, 0f, 50f, 100f);
    rect.setBorder(15);
    rect.setBorderColor(BaseColor.RED);
    rect.setBorderWidth(.5f);
    rect.setBackgroundColor(BaseColor.BLUE);
    doc.setPageSize(pageSize);
    doc.open();

    Page page = new Page(pageSize.getWidth(), pageSize.getHeight());
    ContentBox rpage = page.margin(new Float[] { 10f, 50f, 10f, 50f });

    for (Anchor anchor : Anchor.values()) {
        ContentBox box = new ContentBox(50f, 100f);
        float offx = anchor.equals(Anchor.CENTER) ? -50f : 0f;
        float offy = anchor.equals(Anchor.CENTER) ? -50f : 0f;

        ContentBox rr = anchor.anchorElement(rpage, box, offx, offy);

        if (anchor.equals(Anchor.BOTTOM_LEFT)) {
            float[] dim = box.scaleByPercentage(0.5f);
            //rr = rr.reposition(dim[0], dim[1], Anchor.TOP_RIGHT);
        }

        rect.setLeft(rr.getLeft());
        rect.setBottom(rr.getBottom());
        rect.setRight(rr.getRight());
        rect.setTop(rr.getTop());
        doc.add(rect);
    }
    String imageFile = getImageFilePath();
    Image image = Image.getInstance(imageFile);

    float width = Measurement.dotsToUserUnits(image.getWidth(), 1200);
    float height = Measurement.dotsToUserUnits(image.getHeight(), 1200);

    System.out.println(width + " " + height);

    ContentBox img = Anchor.BOTTOM_LEFT.anchorElement(rpage, width, height);

    image.scaleToFit(width, height);
    image.setAbsolutePosition(img.getLeft(), img.getBottom());

    System.out.println(image.getWidth() + " " + image.getHeight());
    System.out.println(image.getAbsoluteX() + " " + image.getAbsoluteY());

    doc.add(image);
    doc.close();
}

From source file:org.gephi.io.exporter.preview.PDFExporter.java

License:Open Source License

public boolean execute() {
    Progress.start(progress);/*from ww w.  j  av a2 s . c  o m*/

    PreviewController controller = Lookup.getDefault().lookup(PreviewController.class);
    controller.getModel(workspace).getProperties().putValue(PreviewProperty.VISIBILITY_RATIO, 1.0);
    controller.refreshPreview(workspace);
    PreviewProperties props = controller.getModel(workspace).getProperties();

    Rectangle size = new Rectangle(pageSize);
    if (landscape) {
        size = new Rectangle(pageSize.rotate());
    }
    Color col = props.getColorValue(PreviewProperty.BACKGROUND_COLOR);
    size.setBackgroundColor(new BaseColor(col.getRed(), col.getGreen(), col.getBlue()));

    Document document = new Document(size);
    PdfWriter pdfWriter = null;
    try {
        pdfWriter = PdfWriter.getInstance(document, stream);
        pdfWriter.setPdfVersion(PdfWriter.PDF_VERSION_1_5);
        pdfWriter.setFullCompression();

    } catch (DocumentException ex) {
        Exceptions.printStackTrace(ex);
    }
    document.open();
    PdfContentByte cb = pdfWriter.getDirectContent();
    cb.saveState();

    props.putValue(PDFTarget.LANDSCAPE, landscape);
    props.putValue(PDFTarget.PAGESIZE, size);
    props.putValue(PDFTarget.MARGIN_TOP, new Float((float) marginTop));
    props.putValue(PDFTarget.MARGIN_LEFT, new Float((float) marginLeft));
    props.putValue(PDFTarget.MARGIN_BOTTOM, new Float((float) marginBottom));
    props.putValue(PDFTarget.MARGIN_RIGHT, new Float((float) marginRight));
    props.putValue(PDFTarget.PDF_CONTENT_BYTE, cb);
    target = (PDFTarget) controller.getRenderTarget(RenderTarget.PDF_TARGET, workspace);
    if (target instanceof LongTask) {
        ((LongTask) target).setProgressTicket(progress);
    }

    try {
        controller.render(target, workspace);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    cb.restoreState();
    document.close();

    Progress.finish(progress);

    props.putValue(PDFTarget.PDF_CONTENT_BYTE, null);
    props.putValue(PDFTarget.PAGESIZE, null);

    return !cancel;
}