Example usage for org.apache.pdfbox.pdmodel.common PDRectangle getLowerLeftY

List of usage examples for org.apache.pdfbox.pdmodel.common PDRectangle getLowerLeftY

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.common PDRectangle getLowerLeftY.

Prototype

public float getLowerLeftY() 

Source Link

Document

This will get the lower left y coordinate.

Usage

From source file:com.synopsys.integration.blackduck.report.pdf.RiskReportPdfWriter.java

License:Apache License

public File createPDFReportFile(final File outputDirectory, final ReportData report)
        throws RiskReportException {
    final IntegrationEscapeUtil escapeUtil = new IntegrationEscapeUtil();
    final String escapedProjectName = escapeUtil.escapeForUri(report.getProjectName());
    final String escapedProjectVersionName = escapeUtil.escapeForUri(report.getProjectVersion());
    final File pdfFile = new File(outputDirectory,
            escapedProjectName + "_" + escapedProjectVersionName + "_BlackDuck_RiskReport.pdf");
    if (pdfFile.exists()) {
        pdfFile.delete();//from   w w  w.j a va 2 s .  co  m
    }
    final PDDocument document = new PDDocument();
    document.getDocumentInformation().setAuthor("Black Duck Software");
    document.getDocumentInformation().setCreator("Integrations");
    document.getDocumentInformation().setSubject("Hub Risk Report");

    try (PDFBoxManager pdfManager = new PDFBoxManager(pdfFile, document)) {
        this.pdfManager = pdfManager;
        final PDRectangle pageBox = pdfManager.currentPage.getMediaBox();
        final float pageWidth = pageBox.getWidth();
        final float pageHeight = pageBox.getHeight();

        final PDRectangle headerRectangle = writeHeader(pageWidth, pageHeight);
        final PDRectangle bottomOfProjectInfoRectangle = writeProjectInformation(pageWidth,
                headerRectangle.getLowerLeftY(), report);
        final PDRectangle bottomOfSummaryTableRectangle = writeSummaryTables(pageWidth,
                bottomOfProjectInfoRectangle.getLowerLeftY(), report);
        final PDRectangle bottomOfComponentTableRectangle = writeComponentTable(pageWidth,
                bottomOfSummaryTableRectangle.getLowerLeftY(), report);

        return pdfFile;
    } catch (final IOException | URISyntaxException e) {
        final String errorString = "Couldn't create the report: ";
        logger.trace(errorString + e.getMessage(), e);
        throw new RiskReportException(errorString + e.getMessage(), e);
    }
}

From source file:com.synopsys.integration.blackduck.report.pdf.RiskReportPdfWriter.java

License:Apache License

private PDRectangle writeHeader(final float pageWidth, final float startingHeight)
        throws IOException, URISyntaxException {
    final PDRectangle rectangle = pdfManager.drawRectangle(0, startingHeight - 100, pageWidth, 100,
            Color.BLACK);/*from w  w  w  .  j  ava2  s. c o m*/
    pdfManager.drawImage(pageWidth - 220, rectangle.getLowerLeftY() + 27.5F, 203, 45,
            "/riskreport/web/images/Hub_BD_logo.png");
    pdfManager.writeText(5, rectangle.getLowerLeftY() + 40F, "Black Duck Risk Report",
            PDFBoxManager.DEFAULT_FONT_BOLD, 20, Color.WHITE);
    logger.trace("Finished writing the pdf header.");
    return rectangle;
}

From source file:com.synopsys.integration.blackduck.report.pdf.RiskReportPdfWriter.java

License:Apache License

private PDRectangle writeProjectInformation(final float pageWidth, final float startingHeight,
        final ReportData reportData) throws IOException {
    final float height = startingHeight - 18;
    PDRectangle rectangle = pdfManager.writeWrappedLink(5, height, 280, reportData.getProjectName(),
            reportData.getProjectURL(), PDFBoxManager.DEFAULT_FONT, 18);
    final String dash = " - ";
    rectangle = pdfManager.writeText(5 + rectangle.getUpperRightX(), height, dash, PDFBoxManager.DEFAULT_FONT,
            18, Color.BLACK);//from  www  .j a  va  2  s  . c om
    rectangle = pdfManager.writeWrappedLink(5 + rectangle.getUpperRightX(), height, 280 - rectangle.getWidth(),
            reportData.getProjectVersion(), reportData.getProjectVersionURL(), PDFBoxManager.DEFAULT_FONT, 18);

    final String projectAttributesString = "Phase:  " + reportData.getPhase() + "    |    Distribution:  "
            + reportData.getDistribution();
    rectangle = pdfManager.writeWrappedText(5, rectangle.getLowerLeftY() - 18, 300, projectAttributesString);
    logger.trace("Finished writing the project information.");
    return rectangle;
}

From source file:com.synopsys.integration.blackduck.report.pdf.RiskReportPdfWriter.java

License:Apache License

private PDRectangle writeSummaryTable(final float centerX, final float y, final String title,
        final int highCount, final int mediumCount, final int lowCount, final int noneCount,
        final int totalCount) throws IOException {
    PDRectangle rectangle = pdfManager.writeTextCentered(centerX, y, title, PDFBoxManager.DEFAULT_FONT_BOLD, 14,
            Color.BLACK);/*w w w  .  ja v  a2  s  . c o m*/

    rectangle = writeSummaryTableRow(centerX, rectangle.getLowerLeftY() - 14, HIGH_RISK, highCount, totalCount,
            decode("#b52b24"));
    rectangle = writeSummaryTableRow(centerX, rectangle.getLowerLeftY() - 14, MED_RISK, mediumCount, totalCount,
            decode("#eca4a0"));
    rectangle = writeSummaryTableRow(centerX, rectangle.getLowerLeftY() - 14, LOW_RISK, lowCount, totalCount,
            new Color(153, 153, 153));
    return writeSummaryTableRow(centerX, rectangle.getLowerLeftY() - 14, NO_RISK, noneCount, totalCount,
            new Color(221, 221, 221));
}

From source file:com.synopsys.integration.blackduck.report.pdf.RiskReportPdfWriter.java

License:Apache License

private PDRectangle writeComponentTable(final float pageWidth, final float startingHeight,
        final ReportData reportData) throws IOException, URISyntaxException {
    // new Color(221, 221, 221)
    final float height = startingHeight - 20;

    final PDRectangle rectangle = pdfManager.writeText(30, height,
            "BOM Entries " + reportData.getTotalComponents());

    // header row
    PDRectangle rowRectangle = pdfManager.drawRectangle(10, rectangle.getLowerLeftY() - 22, pageWidth - 20, 18,
            new Color(221, 221, 221));
    final float rowY = rowRectangle.getLowerLeftY() + 5;
    pdfManager.writeText(50, rowY, "Component", PDFBoxManager.DEFAULT_FONT_BOLD, 12,
            PDFBoxManager.DEFAULT_COLOR);
    pdfManager.writeText(190, rowY, "Version", PDFBoxManager.DEFAULT_FONT_BOLD, 12,
            PDFBoxManager.DEFAULT_COLOR);
    pdfManager.writeText(310, rowY, "License", PDFBoxManager.DEFAULT_FONT_BOLD, 12,
            PDFBoxManager.DEFAULT_COLOR);
    pdfManager.writeText(430, rowY, "H", PDFBoxManager.DEFAULT_FONT_BOLD, 12, PDFBoxManager.DEFAULT_COLOR);
    pdfManager.writeText(470, rowY, "M", PDFBoxManager.DEFAULT_FONT_BOLD, 12, PDFBoxManager.DEFAULT_COLOR);
    pdfManager.writeText(510, rowY, "L", PDFBoxManager.DEFAULT_FONT_BOLD, 12, PDFBoxManager.DEFAULT_COLOR);
    pdfManager.writeText(550, rowY, "Opt R", PDFBoxManager.DEFAULT_FONT_BOLD, 12, PDFBoxManager.DEFAULT_COLOR);

    boolean isOdd = false;
    for (final BomComponent component : reportData.getComponents()) {
        if (null != component) {
            rowRectangle = writeComponentRow(pageWidth, rowRectangle.getLowerLeftY(), component, isOdd);
            isOdd = !isOdd;//  w  w w.j a v a2s. co  m
        }
    }
    logger.trace("Finished writing the component table.");
    return rowRectangle;
}

From source file:com.synopsys.integration.blackduck.report.pdf.RiskReportPdfWriter.java

License:Apache License

private PDRectangle writeComponentRow(final float pageWidth, final float y, final BomComponent component,
        final boolean isOdd) throws IOException, URISyntaxException {
    final float componentNameWidth = 125F;
    final float componentVersionWidth = 115F;
    final float componentLicenseWidth = 150F;

    List<String> componentNameTextLines = new ArrayList<>();
    List<String> componentVersionTextLines = new ArrayList<>();
    List<String> componentLicenseTextLines = new ArrayList<>();

    if (StringUtils.isNotBlank(component.getComponentName())) {
        componentNameTextLines = StringManager.wrapToCombinedList(component.getComponentName(),
                Math.round(componentNameWidth));
    }/*  w  w w .j av  a 2  s  .  co m*/
    if (StringUtils.isNotBlank(component.getComponentVersion())) {
        componentVersionTextLines = StringManager.wrapToCombinedList(component.getComponentVersion(),
                Math.round(componentNameWidth));
    }
    if (StringUtils.isNotBlank(component.getLicense())) {
        componentLicenseTextLines = StringManager.wrapToCombinedList(component.getLicense(),
                Math.round(componentNameWidth));
    }

    float rowHeight = pdfManager.getApproximateWrappedStringHeight(componentNameTextLines.size(),
            PDFBoxManager.DEFAULT_FONT_SIZE);
    final float componentVersionHeight = pdfManager.getApproximateWrappedStringHeight(
            componentVersionTextLines.size(), PDFBoxManager.DEFAULT_FONT_SIZE);
    final float componentLicenseHeight = pdfManager.getApproximateWrappedStringHeight(
            componentLicenseTextLines.size(), PDFBoxManager.DEFAULT_FONT_SIZE);
    if (componentVersionHeight > rowHeight) {
        rowHeight = componentVersionHeight;
    }
    if (componentLicenseHeight > rowHeight) {
        rowHeight = componentLicenseHeight;
    }

    PDRectangle rowRectangle = null;
    Color rowColor = Color.WHITE;
    if (isOdd) {
        rowColor = new Color(221, 221, 221);
        rowRectangle = pdfManager.drawRectangle(10, y - rowHeight, pageWidth - 20, rowHeight, rowColor);
    } else {
        rowRectangle = pdfManager.drawRectangle(10, y - rowHeight, pageWidth - 20, rowHeight, rowColor);
    }

    final float rowUpperY = rowRectangle.getUpperRightY();
    if (StringUtils.isNotBlank(component.getPolicyStatus())
            && component.getPolicyStatus().equalsIgnoreCase("IN_VIOLATION")) {
        pdfManager.drawImageCentered(15, rowUpperY, 8, 8, 0, rowHeight,
                "/riskreport/web/images/cross_through_circle.png");
    }
    String componentURL = "";
    if (StringUtils.isNotBlank(component.getComponentURL())) {
        componentURL = component.getComponentURL();
    }
    String componentVersionURL = "";
    if (StringUtils.isNotBlank(component.getComponentVersionURL())) {
        componentVersionURL = component.getComponentVersionURL();
    }
    pdfManager.writeWrappedVerticalCenteredLink(30F, rowUpperY, componentNameWidth, rowHeight,
            componentNameTextLines, componentURL, PDFBoxManager.DEFAULT_COLOR);
    pdfManager.writeWrappedCenteredLink(210, rowUpperY, componentVersionWidth, rowHeight,
            componentVersionTextLines, componentVersionURL, PDFBoxManager.DEFAULT_COLOR);

    final Risk licenseRisk = getLicenseRisk(component, rowColor);

    if (StringUtils.isNotBlank(licenseRisk.riskShortString)) {
        pdfManager.drawRectangleCentered(282, rowUpperY - 1, 12, 12, rowHeight, licenseRisk.riskColor);
        pdfManager.writeTextCentered(282, rowUpperY, rowHeight, licenseRisk.riskShortString);
    }

    pdfManager.writeWrappedVerticalCenteredText(290, rowUpperY, componentLicenseWidth, rowHeight,
            componentLicenseTextLines);

    pdfManager.writeTextCentered(434, rowUpperY, rowHeight,
            String.valueOf(component.getSecurityRiskHighCount()));
    pdfManager.writeTextCentered(477, rowUpperY, rowHeight,
            String.valueOf(component.getSecurityRiskMediumCount()));
    pdfManager.writeTextCentered(520, rowUpperY, rowHeight,
            String.valueOf(component.getSecurityRiskLowCount()));

    final Risk operationalRisk = getOperationalRisk(component, rowColor);

    pdfManager.drawRectangle(545, rowRectangle.getLowerLeftY(), 60, rowHeight, operationalRisk.riskColor);
    pdfManager.writeTextCentered(575, rowUpperY, rowHeight, operationalRisk.riskShortString,
            PDFBoxManager.DEFAULT_FONT_BOLD, 12, PDFBoxManager.DEFAULT_COLOR);

    return rowRectangle;
}

From source file:com.synopsys.integration.blackduck.service.model.pdf.RiskReportPdfWriter.java

License:Apache License

public File createPDFReportFile(final File outputDirectory, final ReportData report)
        throws RiskReportException {
    final IntegrationEscapeUtil escapeUtil = new IntegrationEscapeUtil();
    final String escapedProjectName = escapeUtil.escapeForUri(report.getProjectName());
    final String escapedProjectVersionName = escapeUtil.escapeForUri(report.getProjectVersion());
    final File pdfFile = new File(outputDirectory,
            escapedProjectName + "_" + escapedProjectVersionName + "_BlackDuck_RiskReport.pdf");
    if (pdfFile.exists()) {
        pdfFile.delete();//from w ww .java2 s .  c om
    }
    final PDDocument document = new PDDocument();
    document.getDocumentInformation().setAuthor("Black Duck Software");
    document.getDocumentInformation().setCreator("Integrations");
    document.getDocumentInformation().setSubject("Black Duck Risk Report");

    try (PDFBoxManager pdfManager = new PDFBoxManager(pdfFile, document)) {
        this.pdfManager = pdfManager;
        final PDRectangle pageBox = pdfManager.currentPage.getMediaBox();
        final float pageWidth = pageBox.getWidth();
        final float pageHeight = pageBox.getHeight();

        final PDRectangle headerRectangle = writeHeader(pageWidth, pageHeight);
        final PDRectangle bottomOfProjectInfoRectangle = writeProjectInformation(pageWidth,
                headerRectangle.getLowerLeftY(), report);
        final PDRectangle bottomOfSummaryTableRectangle = writeSummaryTables(pageWidth,
                bottomOfProjectInfoRectangle.getLowerLeftY(), report);
        final PDRectangle bottomOfComponentTableRectangle = writeComponentTable(pageWidth,
                bottomOfSummaryTableRectangle.getLowerLeftY(), report);

        return pdfFile;
    } catch (final IOException | URISyntaxException e) {
        final String errorString = "Couldn't create the report: ";
        logger.trace(errorString + e.getMessage(), e);
        throw new RiskReportException(errorString + e.getMessage(), e);
    }
}

From source file:com.synopsys.integration.blackduck.service.model.pdf.RiskReportPdfWriter.java

License:Apache License

private PDRectangle writeHeader(final float pageWidth, final float startingHeight)
        throws IOException, URISyntaxException {
    final PDRectangle rectangle = pdfManager.drawRectangle(0, startingHeight - 100, pageWidth, 100,
            Color.BLACK);//w  w  w  .j av  a  2s.  c o m
    pdfManager.drawImage(pageWidth - 220, rectangle.getLowerLeftY() + 27.5F, 203, 45,
            "/riskreport/web/images/Black_Duck_BD_logo.png");
    pdfManager.writeText(5, rectangle.getLowerLeftY() + 40F, "Black Duck Risk Report",
            PDFBoxManager.DEFAULT_FONT_BOLD, 20, Color.WHITE);
    logger.trace("Finished writing the pdf header.");
    return rectangle;
}

From source file:com.yiyihealth.tools.test.DrawPrintTextLocations.java

License:Apache License

private void stripPage(int page) throws IOException {
    PDFRenderer pdfRenderer = new PDFRenderer(document);
    image = pdfRenderer.renderImage(page, SCALE);

    PDPage pdPage = document.getPage(page);
    PDRectangle cropBox = pdPage.getCropBox();

    // flip y-axis
    flipAT = new AffineTransform();
    flipAT.translate(0, pdPage.getBBox().getHeight());
    flipAT.scale(1, -1);/*from  w  w w .j  a  va  2 s  .c o m*/

    // page may be rotated
    rotateAT = new AffineTransform();
    int rotation = pdPage.getRotation();
    if (rotation != 0) {
        PDRectangle mediaBox = pdPage.getMediaBox();
        switch (rotation) {
        case 90:
            rotateAT.translate(mediaBox.getHeight(), 0);
            break;
        case 270:
            rotateAT.translate(0, mediaBox.getWidth());
            break;
        case 180:
            rotateAT.translate(mediaBox.getWidth(), mediaBox.getHeight());
            break;
        default:
            break;
        }
        rotateAT.rotate(Math.toRadians(rotation));
    }

    g2d = image.createGraphics();
    g2d.setStroke(new BasicStroke(0.1f));
    g2d.scale(SCALE, SCALE);

    setStartPage(page + 1);
    setEndPage(page + 1);

    Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
    writeText(document, dummy);

    // beads in green
    g2d.setStroke(new BasicStroke(0.4f));
    List<PDThreadBead> pageArticles = pdPage.getThreadBeads();
    for (PDThreadBead bead : pageArticles) {
        PDRectangle r = bead.getRectangle();
        GeneralPath p = r
                .transform(Matrix.getTranslateInstance(-cropBox.getLowerLeftX(), cropBox.getLowerLeftY()));

        Shape s = flipAT.createTransformedShape(p);
        s = rotateAT.createTransformedShape(s);
        g2d.setColor(Color.green);
        g2d.draw(s);
    }

    g2d.dispose();

    String imageFilename = filename;
    int pt = imageFilename.lastIndexOf('.');
    imageFilename = imageFilename.substring(0, pt) + "-marked-" + (page + 1) + ".png";
    ImageIO.write(image, "png", new File(imageFilename));
}

From source file:com.zilbo.flamingSailor.TE.PDFParser.java

License:Apache License

@Override
protected void endPage(PDPage page) throws IOException {
    super.endPage(page);
    int pieceID = 0;
    Map<String, Map<Integer, Long>> fontCounts = new HashMap<>();
    List<TextPiece> wordsOfThisPage = new ArrayList<>();

    for (List<TextPosition> aCharactersByArticle : charactersByArticle) {
        //   int len = aCharactersByArticle.size();
        for (TextPosition t : aCharactersByArticle) {
            // copy information
            TextPiece w = new TextPiece(pieceID++);
            PDFont font = t.getFont();//from   w ww  .j a v  a2 s .c o  m
            PDFontDescriptor fontDescriptor = font.getFontDescriptor();

            //   w.setFontDescriptor(fontDescriptor);
            if (fontDescriptor == null) {
                w.setFontName("UNKNOWN");
            } else {
                w.setFontName(fontDescriptor.getFontName());
            }

            /*
            * 100: a simple step to fix the font size to the normal range, for those documents in unknown codes that PDFBox can not process now
            */
            if (t.getFontSize() < 0.3 && t.getYScale() <= 1.0) {
                w.setFontSize(t.getFontSize() * 100);
                w.setHeight(Math.max(t.getYScale(), t.getFontSize()) * 100);
                w.setXScale(t.getXScale());
                w.setYScale(t.getYScale());
            } else {
                if (t.getYScale() < 0.3 && t.getFontSize() <= 1.0) {
                    w.setYScale(t.getYScale() * 100);
                    w.setXScale(t.getXScale() * 100);
                    w.setHeight(Math.max(t.getYScale() * 100, t.getFontSize()));
                } else {
                    w.setFontSize(t.getFontSize());
                    w.setHeight(Math.max(t.getYScale(), t.getFontSize()));
                    w.setXScale(t.getXScale());
                    w.setYScale(t.getYScale());
                }
            }

            Map<Integer, Long> counts = fontCounts.get(w.getFontName());
            if (counts == null) {
                counts = new HashMap<>();
                fontCounts.put(w.getFontName(), counts);
            }
            Long count = counts.get((int) Math.round(w.getHeight()));
            if (count == null) {
                count = 1L;
            } else {
                count += 1L;
            }
            counts.put((int) Math.round(w.getHeight()), count);

            w.setWidth(Math.abs(t.getWidth()));
            w.setGeom(t.getX(), t.getY(), w.getWidth(), w.getHeight());

            w.setText(t.getCharacter());

            w.setWidthOfSpace(t.getWidthOfSpace());
            wordsOfThisPage.add(w);
        }
    }
    currentPage.processPage(wordsOfThisPage, fontCounts);
    currentPage.setText(outString.getBuffer().toString());
    outString.getBuffer().setLength(0);
    List<PDAnnotation> annotations = page.getAnnotations();

    for (PDAnnotation annotation : annotations) {
        if (annotation instanceof PDAnnotationLink) {
            PDAnnotationLink l = (PDAnnotationLink) annotation;
            PDRectangle rect = l.getRectangle();
            PDDestination dest = l.getDestination();
            if (dest instanceof PDPageXYZDestination) {
                PDPageXYZDestination xyzDestination = (PDPageXYZDestination) dest;
                PDPage pageDest = ((PDPageXYZDestination) dest).getPage();

                if (rect != null) {
                    if (xyzDestination.getPageNumber() < 0) {
                        int pageNumber = allpages.indexOf(pageDest) + 1;
                        Rectangle2D hotbox = new Rectangle2D.Double(rect.getLowerLeftX(), rect.getLowerLeftY(),
                                (rect.getUpperRightX() - rect.getLowerLeftX()),
                                (rect.getUpperRightY() - rect.getLowerLeftY()));
                        Point2D toPoint = new Point2D.Double(xyzDestination.getLeft(), xyzDestination.getTop());
                        currentPage.addLink(new PDLink(hotbox, pageNumber, toPoint));
                    }
                }
            }
        }
    }

    /*
     The following code is REALLY raw.
     initial testing seemed to show memory leaks, and was REALLY slow.
            
    PDResources r = page.getResources();
    Map<String, PDXObjectImage> images = r.getImages();
    for (Map.Entry<String, PDXObjectImage> e : images.entrySet()) {
    BufferedImage bi = null;
    try {
            
        //   currentPage.addImage(bi);
            
        //    (e.getValue()).write2file("/tmp/II" + e.getKey());
        if (e.getValue() instanceof PDJpeg) {
            PDJpeg jpg = (PDJpeg) e.getValue();
            bi = jpg.getRGBImage();
            ColorSpace cs = bi.getColorModel().getColorSpace();
            File jpgFile = new File("/tmp/II" + e.getKey() + ".jpg");
            
            if (cs instanceof ColorSpaceCMYK) {
            
                logger.info("Ignoring image with CMYK color space");
            } else {
               // ImageIO.write(bi, "jpg", jpgFile);
                jpg.write2file("/tmp/II"+ e.getKey());
            }
            
        } else {
            (e.getValue()).write2file("/tmp/II" + e.getKey());
        }
    } catch (Exception ee) {
        logger.info("can't read image ;-(", ee);
    }
            
    }
    */

    textPageList.add(currentPage);
    currentPage = null;
}