Package com.pd4ml.dom

Class Document


  • public class Document
    extends Node
    An HTML document parsed by the v42 layout engine, as PD4ML.getDocument() returns it after readHTML(). Changes made to it before the document is written (writePDF(), writeRTF(), renderAsImages() etc.) are laid out as if the HTML had been written that way: elements, attributes, inline styles and <style>/<link> style sheets, images. No external libraries are involved.
     PD4ML pd4ml = new PD4ML();
     pd4ml.useHtmlRenderer(Constants.LayoutEngine.v42);
     pd4ml.readHTML(url);
     Document doc = pd4ml.getDocument();
     for (Element e : doc.querySelectorAll("table.prices td.amount")) {
         e.addClass("right");
     }
     doc.getBody().appendChild(doc.createElement("p")).setTextContent("Printed " + new Date());
     pd4ml.writePDF(out);
     
    Since:
    v4.2.0
    • Method Detail

      • getDocumentElement

        public Element getDocumentElement()
        The root element (<html>).
      • getHead

        public Element getHead()
        The <head> element, or null.
      • getBody

        public Element getBody()
        The <body> (or <frameset>) element, or null.
      • getDoctype

        public DocumentType getDoctype()
        The doctype, or null if the document has none (it is laid out in quirks mode then).
      • isQuirksMode

        public boolean isQuirksMode()
        True if the document is laid out in quirks mode (no or a legacy doctype).
      • getBaseURI

        public java.lang.String getBaseURI()
        The base URI relative links and images are resolved against.
      • getTitle

        public java.lang.String getTitle()
        The text of the <title> element, or an empty string.
      • getElementById

        public Element getElementById​(java.lang.String id)
        The first element with the id, or null.
      • getElementsByTagName

        public java.util.List<Element> getElementsByTagName​(java.lang.String name)
        The elements with the tag name, in document order (*: all).
      • getElementsByClassName

        public java.util.List<Element> getElementsByClassName​(java.lang.String names)
        The elements that have all the (space separated) class names, in document order.
      • querySelector

        public Element querySelector​(java.lang.String selectors)
        The first element that matches the CSS selector (list), or null.
        Throws:
        java.lang.IllegalArgumentException - if the selector is not valid
      • querySelectorAll

        public java.util.List<Element> querySelectorAll​(java.lang.String selectors)
        The elements that match the CSS selector (list), in document order.
        Throws:
        java.lang.IllegalArgumentException - if the selector is not valid
      • createElement

        public Element createElement​(java.lang.String tagName)
        A new element of this document, not yet in the tree.
        Parameters:
        tagName - the tag name (made lower case)
      • createTextNode

        public Text createTextNode​(java.lang.String data)
        A new text node of this document, not yet in the tree.
      • createComment

        public Comment createComment​(java.lang.String data)
        A new comment of this document, not yet in the tree.
      • setTextContent

        public void setTextContent​(java.lang.String text)
        Comments and text cannot be direct children of a document here: only the doctype and the root.
        Overrides:
        setTextContent in class Node
      • of

        public static Document of​(java.lang.Object parsedDocument)
        The document object for the parsed v42 document. Internal: use PD4ML.getDocument().