Here's some working code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
using iTextSharp.tool.xml.pipeline;
namespace TestsXMLWorker {
public class MyHandler : IElementHandler {
public List<IElement> elements = new List<IElement>();
public void Add(IWritable w) {
if (w is WritableElement) {
elements.AddRange(((WritableElement)w).Elements());
}
}
}
public class TestListElements {
public static void Test() {
MyHandler mh = new MyHandler();
using (StreamReader sr = new StreamReader("/html/walden.html")) {
XMLWorkerHelper.GetInstance().ParseXHtml(mh, sr);
}
// use mh.elements
}
}
}
Paulo
----- Original Message -----
From: Balder VC
To: itext-questions@lists.sourceforge.net
Sent: Monday, February 06, 2012 9:21 PM
Subject: Re: [iText-questions] iTextSharp - XMLWorkerHelper - Parsing HTML
into a list of Element objects
Hi Keith, I read it like the OP wants a list of IElements. But only gave the
Java way of doing that.
Here is the C# way
Parsing to a list can be done. You have to create the list yourself by
implementing an IElementHandler, that receives IWritable
which contains IWritableElements that contains a list of elements. 1 HTML
Strinng/file can consists of multiple WritableElements
(http://itextsharp.svn.sourceforge.net/viewvc/itextsharp/trunk/src/extras/itextsharp.xmlworker/iTextSharp/tool/xml/pipeline/WritableElement.cs?revision=269&view=markup
) that each can contain a list of IElments which you can add to your own list.
and pass the IElementHandler to the appropriate method of
XMLWorkerHelper.ParseXHtml(IElementHandler d, TextReader inp) method.
I'll write pseudo code but it is with anonymous inner classes perhaps OP gets
more confused. I heard that anonymous inner classes don't work in C#. ;)
List myelements = new List();
XMLWorkerHelper.getInstance().parseXHtml(new ElementHandler() {
public void add(final Writable w) {
if (w instanceof WritableElement) {
List<Element> elements =
((WritableElement)w).elements();
for (Element e : elements) {
myelements.add(e); // in Java the Paragraph
extends from java.util.List so don't do addAll()
}
}
}
}, yourhtmlstringorfileresource);
// do stuff with your myelements
On 6/02/2012 19:45, Keith O wrote:
Hi Balder,
On Mon, Feb 6, 2012 at 1:14 PM, Balder VC <li...@redlab.be> wrote:
I don't really know C# or VB syntax, but the usage should be very
similar as in Java, normally the classes and such have the same name in
both Java or C#.
On 04/02/12 17:30, Dean McCarthy wrote:
Could somebody please provide an example of parsing HTML into a list of
elements using XMLWorkerHelper in iTextSharp (C# or VB).
The JAVA version as given in the documentation is:
XMLWorkerHelper.getInstance().parseXHtml(new ElementHandler() {
public void add(final Writable w) {
if (w instanceof WritableElement) {
List<Element> elements = ((WritableElement)w).elements();
// write class names of elements to file
}
}
}, HTMLParsingToList.class.getResourceAsStream("/html/walden.html"));
I can't speak for the OP, but the way I read (maybe to much into) the
question is "how can we create a PDF from the list of elements like we
did with previous versions that didn't have XMLWorker[Helper]". In
other words, we used to build PDF from HTML like (pseudo-code):
List<IElement> objects = HTMLWorker.ParseToList();
foreach (IElement element in objects) {
document.Add(element);
}
The section the OP quoted from the documentation states:
"Suppose you're not interested in creating PDF, but you just want
parse the HTML into a list of iText Element objects."
thanks - keith
e check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php
--
twitter
redlab-log
------------------------------------------------------------------------------
Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-dev2
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php