i tried with itext and could not get xml format
i want to convert pdf to xml
i just want text(location,size) which is available in XML format and so any one help ne in conversion of PDF to XML using java
2 Answers
There is a library pdf2htmlEX which can convert the pdf to html without losing text or format.
Hope this can help you.
This is the code I use in my own applications. I don't remember where I got it from, but it sure works like a charm.
public class ConvertPDFToXML {
static StreamResult streamResult;
static TransformerHandler handler;
static AttributesImpl atts;
public static void main(String[] args) throws IOException {
try {
Document document = new Document();
document.open();
PdfReader reader = new PdfReader("C:\\hello.pdf");
PdfDictionary page = reader.getPageN(1);
PRIndirectReference objectReference = (PRIndirectReference) page
.get(PdfName.CONTENTS);
PRStream stream = (PRStream) PdfReader
.getPdfObject(objectReference);
byte[] streamBytes = PdfReader.getStreamBytes(stream);
PRTokeniser tokenizer = new PRTokeniser(streamBytes);
StringBuffer strbufe = new StringBuffer();
while (tokenizer.nextToken()) {
if (tokenizer.getTokenType() == PRTokeniser.TK_STRING) {
strbufe.append(tokenizer.getStringValue());
}
}
String test = strbufe.toString();
streamResult = new StreamResult("data.xml");
initXML();
process(test);
closeXML();
document.add(new Paragraph(".."));
document.close();
} catch (Exception e) {
}
}
public static void initXML() throws ParserConfigurationException,
TransformerConfigurationException, SAXException {
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory
.newInstance();
handler = tf.newTransformerHandler();
Transformer serializer = handler.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
serializer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "4");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
handler.setResult(streamResult);
handler.startDocument();
atts = new AttributesImpl();
handler.startElement("", "", "data", atts);
}
public static void process(String s) throws SAXException {
String[] elements = s.split("\\|");
atts.clear();
handler.startElement("", "", "Message", atts);
handler.characters(elements[0].toCharArray(), 0, elements[0].length());
handler.endElement("", "", "Message");
}
public static void closeXML() throws SAXException {
handler.endElement("", "", "data");
handler.endDocument();
}
}
-
but i'm not able to get data from the above code (by the way this code from roseindia)– nikhilJun 7, 2013 at 6:17
-
Oh oh! yeah! It's from roseindia. I just couldn't remember where I got it from. Anyway, you are not able to get data? I don't understand.– SwayamJun 7, 2013 at 6:21
-
i just want to read pdf file entire text in the form of co-ordinates and the text, but the text could not be retrieved using above code can u help me– nikhilJun 7, 2013 at 6:29
-
-
1java.lang.ClassCastException: com.itextpdf.text.pdf.PdfArray cannot be cast to com.itextpdf.text.pdf.PRIndirectReference at ConvertPDFToXML.main(ConvertPDFToXML.java:24) this is the error i got– nikhilJun 7, 2013 at 6:37