Example usage for org.w3c.dom DOMImplementation createDocument

List of usage examples for org.w3c.dom DOMImplementation createDocument

Introduction

In this page you can find the example usage for org.w3c.dom DOMImplementation createDocument.

Prototype

public Document createDocument(String namespaceURI, String qualifiedName, DocumentType doctype)
        throws DOMException;

Source Link

Document

Creates a DOM Document object of the specified type with its document element.

Usage

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

private void displaySVG(URI u, int i) throws MalformedURLException, IOException {
    PDDocument doc = getPages(u, null);/* w w  w . j  a  v a2s.c  o 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

@Override
public void generateSVG(URI u, File f, int w, int h, int pn, Writer out)
        throws MalformedURLException, IOException {
    PDDocument doc = getPages(u, f);/*w  w  w .  jav  a2s . c o 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;
}