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

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

Introduction

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

Prototype

public float getWidth() 

Source Link

Document

This will get the width of this rectangle as calculated by upperRightX - lowerLeftX.

Usage

From source file:uk.ac.liverpool.thumbnails.PDFService.java

License:Open Source License

private void displaySVG(URI u, int i) throws MalformedURLException, IOException {
    PDDocument doc = getPages(u, null);//  w  w w.  j  a va2  s .co m
    List pages = doc.getDocumentCatalog().getAllPages();
    int pagen = doc.getNumberOfPages();

    PDPage page = (PDPage) pages.get(i);
    PDRectangle mBox = page.findMediaBox();
    float widthPt = mBox.getWidth();
    float heightPt = mBox.getHeight();
    float sx = widthPt / (float) 600;
    float sy = heightPt / (float) 800;

    // Get a DOMImplementation
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    // Create an instance of org.w3c.dom.Document
    //   String svgNS = "http://www.w3.org/2000/svg";
    //        org.w3c.dom.Document document = domImpl.createDocument(svgNS, "svg",
    //                null);
    DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
    String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
    document = (SVGDocument) impl.createDocument(svgNS, "svg", null);

    // Create an instance of the SVG Generator
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
    svgGenerator.getGeneratorContext().setComment("Test");
    svgGenerator.getGeneratorContext().setEmbeddedFontsOn(true);

    // Ask the test to render into the SVG Graphics2D implementation

    Dimension pageDimension = new Dimension((int) widthPt, (int) heightPt);

    svgGenerator.setBackground(new Color(255, 255, 255, 0));
    svgGenerator.scale(sx, sy);
    svgGenerator.setSVGCanvasSize(pageDimension);
    PageDrawer drawer = new PageDrawer();
    drawer.drawPage(svgGenerator, page, pageDimension);

    JSVGCanvas canvas = new JSVGCanvas();
    JFrame f = new JFrame();
    f.getContentPane().add(canvas);
    canvas.setSVGDocument(document);
    f.pack();
    f.setVisible(true);
}

From source file:uk.ac.liverpool.thumbnails.PDFService.java

License:Open Source License

@Override
public void generateSVG(URI u, File f, int w, int h, int pn, Writer out)
        throws MalformedURLException, IOException {
    PDDocument doc = getPages(u, f);//from   w ww. j av  a2s . co m
    List pages = doc.getDocumentCatalog().getAllPages();
    int pagen = doc.getNumberOfPages();
    int i = 0;
    if (pn < pages.size())
        i = pn;
    PDPage page = (PDPage) pages.get(i);
    PDRectangle mBox = page.findMediaBox();
    float widthPt = mBox.getWidth();
    float heightPt = mBox.getHeight();
    float sx = widthPt / (float) w;
    float sy = heightPt / (float) h;

    // Get a DOMImplementation
    DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
    // Create an instance of org.w3c.dom.Document
    //   String svgNS = "http://www.w3.org/2000/svg";
    //        org.w3c.dom.Document document = domImpl.createDocument(svgNS, "svg",
    //                null);
    DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
    String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
    document = (SVGDocument) impl.createDocument(svgNS, "svg", null);

    // Create an instance of the SVG Generator
    SVGGraphics2D svgGenerator = new SVGGraphics2D(document);
    svgGenerator.getGeneratorContext().setComment("Test");
    svgGenerator.getGeneratorContext().setEmbeddedFontsOn(true);

    // Ask the test to render into the SVG Graphics2D implementation

    Dimension pageDimension = new Dimension((int) widthPt, (int) heightPt);

    svgGenerator.setBackground(new Color(255, 255, 255, 0));
    svgGenerator.scale(sx, sy);
    svgGenerator.setSVGCanvasSize(pageDimension);
    PageDrawer drawer = new PageDrawer();
    drawer.drawPage(svgGenerator, page, pageDimension);

    //        Element root = document.getDocumentElement();
    //        svgGenerator.getRoot(root);

    // Finally, stream out SVG to the standard output using UTF-8
    // character to byte encoding
    boolean useCSS = true; // we want to use CSS style attribute
    svgGenerator.stream(out, useCSS, false);

    return;
}

From source file:webhooks.core.services.util.ThumbnailUtil.java

License:Apache License

private static BufferedImage createPDFThumbnail(String filename, int thumbWidth, int thumbHeight)
        throws IOException {
    PDDocument document = PDDocument.load(new File(filename));
    List<?> pages = document.getDocumentCatalog().getAllPages();

    // get first page
    PDPage page = (PDPage) pages.get(0);

    PDRectangle mBox = page.findMediaBox();
    float scaling = thumbWidth / mBox.getWidth();

    Dimension pageDimension = new Dimension((int) mBox.getWidth(), (int) mBox.getHeight());

    BufferedImage thumbImg = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = (Graphics2D) thumbImg.getGraphics();
    graphics.setBackground(new Color(255, 255, 255, 0)); // TRANSPARENT_WHITE
    graphics.clearRect(0, 0, thumbImg.getWidth(), thumbImg.getHeight());
    graphics.scale(scaling, scaling);//from www.  j  ava  2 s  . c  o  m
    PageDrawer drawer = new PageDrawer();
    drawer.drawPage(graphics, page, pageDimension);

    return thumbImg;
}