Convert pdf to rtf file using Java


 

Convert pdf to rtf file using Java

In this section, you will learn how to convert pdf file to rtf file using Java.

In this section, you will learn how to convert pdf file to rtf file using Java.

Convert pdf to rtf file using Java

In this section, you will learn how to convert .pdf file to .rtf file in java programming language. We have used itext api for this purpose. To read resume.pdf  file, we have used PDFReader class. The data is first converted into bytes and then with the use of StringBuffer, it will again converted into string and write into the resume.rtf file.

Here is the code:

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

public class ConvertPDFToRTF {
        public static void main(String[] argsthrows IOException {
        try {
        Document document = new Document();
        document.open();
        PdfReader reader = new PdfReader("C:\\resume.pdf");
        PdfDictionary dictionary = reader.getPageN(1);
        PRIndirectReference reference = (PRIndirectReferencedictionary.get(PdfName.CONTENTS);
        PRStream stream = (PRStreamPdfReader.getPdfObject(reference);
        byte[] bytes = PdfReader.getStreamBytes(stream);
        PRTokeniser tokenizer = new PRTokeniser(bytes);
        FileOutputStream fos=new FileOutputStream("resume.rtf");
        StringBuffer buffer = new StringBuffer();
        while (tokenizer.nextToken()) {
        if (tokenizer.getTokenType() == PRTokeniser.TK_STRING) {
        buffer.append(tokenizer.getStringValue());
         }
           }
        String test=buffer.toString();
        StringReader stReader = new StringReader(test);
        int t;
        while((t=stReader.read())>0)
        fos.write(t);
        document.close();
    System.out.println("Converted Successfully");
    }
         catch (Exception e) {}
     }
     }
 

Ads