Free Support Forum - aspose.com

Aspose HTML


#1

Dear Team,

I have worked with the samples provided in the github for ASPOSE HTML. I need to explore more about converting HTML with CSS to other formats. Kindly provide some example code.I also need to explore about the nodes and their usages other than that specified in Github. Please also shed some light on XPATH,browsing context,node filter,svg with codes.It also offers the scripting which allows to manipulate HTML DOM via JavaScript. As well as HTML, this API also provides the capabilities to load EPUB and MHTML. Please provide example for these.


#2

@Saranya_Sekar

Thank you for contacting support.

Please visit below documentation articles for your kind reference.

Moreover, you may iterate through nodes with INodeIterator interface while ignoring style and script elements as in the code snippet below:

/// <summary>
///  Represents a user filter created in order to ignore content of the 'style' and 'script' element.
/// </summary>
class StyleFilter : Aspose.Html.Dom.Traversal.Filters.NodeFilter
{
    public override short AcceptNode(Aspose.Html.Dom.Node n)
    {
        return (n.ParentElement.TagName == "STYLE" || n.ParentElement.TagName == "SCRIPT" ? FILTER_REJECT : FILTER_ACCEPT);
    }
}

// Create an instance of HTML document
using (var document = new HTMLDocument(dataDir + "simple.html"))
{
    // Initialize the instance of node iterator 
    (https://apireference.aspose.com/net/html/aspose.html.dom.traversal/inodeiterator) that allows to navigate across HTML DOM    
    Aspose.Html.Dom.Traversal.INodeIterator iterator = document.CreateNodeIterator(document, 
    Aspose.Html.Dom.Traversal.Filters.NodeFilter.SHOW_TEXT, new StyleFilter());
    StringBuilder sb = new StringBuilder();
    Aspose.Html.Dom.Node node;
    while ((node = iterator.NextNode()) != null)
        sb.Append(node.NodeValue);
    Console.WriteLine(sb.ToString());
}

We hope this will be helpful. Please feel free to contact us if you need any further assistance.


#3

Dear Farhan,

Kindly please share the code in java.

Thank you.


#4

@Saranya_Sekar

We are working on it and will update you soon.


#5

@Saranya_Sekar

Thank you for being patient.

We have converted respective code to Java as per your requirements. Please try using it and then share your kind feedback with us.

For EPUB to PDF conversion:

Stream stream = new FileStream(dataDir + "Test.epub", FileMode.Open);
PdfDevice device = new PdfDevice(dataDir + "EPUBtoPDF.pdf");
EpubRenderer renderer = new EpubRenderer();
{
    renderer.render(device, stream);
}

For MHTML to PDF conversion:

Stream stream = new FileStream(dataDir + "Test.mht", FileMode.Open);
PdfDevice device = new PdfDevice(dataDir + "MHTMLtoPDF.pdf");
MhtmlRenderer renderer = new MhtmlRenderer();
{
    renderer.render(device, stream);
}

To iterate nodes with INodeIterator, please use below code:

HTMLDocument document = new HTMLDocument(dataDir + "Expected Demo.html");
// Initialize the instance of node iterator 
//(https://apireference.aspose.com/java/html/com.aspose.html.dom.traversal/INodeIterator) that allows to navigate across HTML DOM    
com.aspose.html.dom.traversal.INodeIterator iterator;
iterator = document.createNodeIterator(document, com.aspose.html.dom.traversal.filters.NodeFilter.SHOW_TEXT, new StyleFilter());
        
StringBuilder sb = new StringBuilder();
com.aspose.html.dom.Node node;
while ((node = iterator.nextNode()) != null)
    sb.append(node.getNodeValue());
System.out.println(sb.toString());

/// <summary>
///  Represents a user filter created in order to ignore content of the 'style' and 'script' element.
/// </summary>
public class StyleFilter extends com.aspose.html.dom.traversal.filters.NodeFilter {
    public short acceptNode( com.aspose.html.dom.Node n)    {
        return (n.getParentElement().getTagName() == "STYLE" || n.getParentElement().getTagName() == "SCRIPT" ? FILTER_REJECT : FILTER_ACCEPT);
    }
}

Moreover, manipulating DOM via JavaScript means that JS-scripts inside the document effects on document itself (manipulate with DOM structure). For instance:

<p id="demo">A Paragraph.</p>
<script>
document.getElementById("demo").setInnerHTML("Paragraph Changed");
</script>

We hope this will be helpful. Please feel free to contact us if you need any further assistance.