Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Leon Abdillah
LeoAndre
1 Answer(s)      3 years and 3 months ago
Posted in : Java Beginners


How to extract reference from PDF file using Java? Cheers


Advertisement
View Answers

August 25, 2011 at 12:12 PM


Use itext api.

import java.io.*;
import java.util.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class ReadPDF {
    public static void main(String[] args) throws IOException {
        try {
            Document document = new Document();
            document.open();
            PdfReader reader = new PdfReader("file.pdf");
            PdfDictionary dictionary = reader.getPageN(1);
            PRIndirectReference reference = (PRIndirectReference) dictionary.get(PdfName.CONTENTS);
            PRStream stream = (PRStream) PdfReader.getPdfObject(reference);
            byte[] bytes = PdfReader.getStreamBytes(stream);
            PRTokeniser tokenizer = new PRTokeniser(bytes);
            StringBuffer buffer = new StringBuffer();
            while (tokenizer.nextToken()) {
                if (tokenizer.getTokenType() == PRTokeniser.TK_STRING) {
                    buffer.append(tokenizer.getStringValue());
                }
            }
            String test = buffer.toString();
            System.out.println(test);
        } catch (Exception e) {
        }
    }
}


Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com