title: Using Java to convert PDF to XML
body: <p>I'm trying to create a prototype to convert a PDF file to an XML file. The result is a little strange, all the characters becomes symbols. I think the bug is where the <code>StringBuffer</code> takes the data from the byte array. Can somebody with Java knowledge please help?</p>
  
  <p>This prototype software is using the <code>iText</code> API. To read the PDF file, we have used the<code>PDFReader</code> class. The data is first converted into a byte array, then with the use of <code>Stringbuffer</code>, it will again converted into string. Then we have used <code>StreamResult</code> wich acts as an holder for the transformed result in XML.</p>
  
  <p>After that, the <code>Transformer</code> class processes XML from a variety of sources and writes the transformation output to a variety of sinks. Then <code>TransformerHandler</code> listens for SAX <code>ContentHandler</code>, parse events and transforms them to a result.</p>
  
  <p>The methods <code>startElement()</code> and <code>endElement()</code> of <code>TransformerHandler</code> class have created the tags in the xml file. The Parser invoked <code>startElement()</code> method at the beginning of every element and <code>endElement()</code> at the end of every element in the XML document.</p>
  
  <pre><code>import com.lowagie.text.*;
  import com.lowagie.text.pdf.*;
  import java.io.*;
  import javax.xml.parsers.*;
  import javax.xml.transform.*;
  import javax.xml.transform.sax.*;
  import javax.xml.transform.stream.*;
  import org.xml.sax.*;
  import org.xml.sax.helpers.*;
  
  public class Cp2x {
  
          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:\\helloworld.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 tokeniser = new PRTokeniser(streamBytes);
  
                          StringBuffer string_buffer = new StringBuffer();
                          while (tokeniser.nextToken()) {
                                  if (tokeniser.getTokenType() == PRTokeniser.TK_STRING) {
                                          string_buffer.append(tokeniser.getStringValue());
                                  }
                          }
                          String test = string_buffer.toString();
                          streamResult = new StreamResult("test.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, "UTF-8");
                  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("", "", "Document", atts);
          }
  
          public static void process(String s) throws SAXException {
                  String[] elements = s.split("\\|");
                  atts.clear();
                  handler.startElement("", "", "Note", atts);
                  handler.characters(elements[0].toCharArray(), 0, elements[0].length());
                  handler.endElement("", "", "Note");
          }
  
          public static void closeXML() throws SAXException {
                  handler.endElement("", "", "Document");
                  handler.endDocument();
          }
  }
  </code></pre>