12/12/2010
Konversi PDF ke txt menggunakan PDFBox di Java
Wah, uda lama juga g update tulisan ni blog… Saking sibuknya soalnya
(alibi mode on)…
Hehehe
Btw, ini aku mau share tentang PDFBox, suatu librari di Java yang berguna buat mengkonersi file PDF menjadi sebuat file teks. Yup, teks. PDFbox ini sangat powefull karena bisa meng-”handle” berbagai macam tipe pdf. Benernya ini bukan aku yang buat (ya iyalah
), didapet dari hasil “googling”. Hehehe maklum ilmu aku masi cupu ini… Di bawah ini ada source code yang kudapat dan aku modif sedikit untuk memakai PDFBox. PDFBox yang aku pakai ini adalah PDFBox 0.7.3.
Kelebihan dari PDFBox ini adalah:
- waktu prosesnya relatif cepat
- bisa meng-”convert” file PDF yang isinya terdiri atas 2 kolom (yang sering ditemui di jurnal-jurnal)
Kekurangan PDFBox ini adalah:
- Tidak bisa memasukkan gambar (ya iyalah, becanda yang ini
) ) - untuk sementara ini belum saya temukan kelemahan
PDFtoText.java
/*
* PDFtoText.java
*
* Created on January 24, 2009, 11:55 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author prasanna
*
*modified by Boy Nurah Mustafa
*/
import org.pdfbox.cos.COSDocument;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.pdmodel.PDDocument;
import org.pdfbox.pdmodel.PDDocumentInformation;
import org.pdfbox.util.PDFTextStripper;
import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;
public class PDFtoText {
PDFParser parser;
String parsedText;
PDFTextStripper pdfStripper;
PDDocument pdDoc;
COSDocument cosDoc;
PDDocumentInformation pdDocInfo;
// PDFtoText Constructor
public PDFtoText() {
}
// Extract text from PDF Document
String pdftoText(String fileName) {
System.out.println("Parsing text from PDF file " + fileName + "....");
File f = new File(fileName);
if (!f.isFile()) {
System.out.println("File " + fileName + " does not exist.");
return null;
}
try {
parser = new PDFParser(new FileInputStream(f));
} catch (Exception e) {
System.out.println("Unable to open PDF Parser.");
return null;
}
try {
parser.parse();
cosDoc = parser.getDocument();
pdfStripper = new PDFTextStripper();
pdDoc = new PDDocument(cosDoc);
parsedText = pdfStripper.getText(pdDoc);
} catch (Exception e) {
System.out.println("An exception occured in parsing the PDF Document.");
e.printStackTrace();
try {
if (cosDoc != null) cosDoc.close();
if (pdDoc != null) pdDoc.close();
} catch (Exception e1) {
e.printStackTrace();
}
return null;
}
System.out.println("Done.");
return parsedText;
}
// Write the parsed text from PDF to a file
void writeTexttoFile(String pdfText, String fileName) {
System.out.println("\nWriting PDF text to output text file " + fileName + "....");
try {
PrintWriter pw = new PrintWriter(fileName);
pw.print(pdfText);
pw.close();
} catch (Exception e) {
System.out.println("An exception occured in writing the pdf text to file.");
e.printStackTrace();
}
System.out.println("Done.");
}
//Extracts text from a PDF Document and writes it to a text file
public static void main(String args[]) {
PDFtoText pdf = new PDFtoText();
String input = pdf.pdftoText("Makalah0607-121.pdf");
System.out.println(input);
pdf.writeTexttoFile(input, "coba.txt");
PDFtoText PDFtoTextObj = new PDFtoText();
String pdfToText = PDFtoTextObj.pdftoText(args[0]);
if (pdfToText == null) {
System.out.println("PDF to Text Conversion failed.");
}
else {
System.out.println("\nThe text parsed from the PDF Document....\n" + pdfToText);
PDFtoTextObj.writeTexttoFile(pdfToText, args[1]);
}
}
}
Untuk memasukkan file PDF yang akan dikonversi menjadi text, pada baris 098 isikan nama file PDFnya. Kemudian untk file tujuannya, pada baris 100.
Untuk menggunakan PDFBox ini ada langkah-langkahnya:
- Download PDFBox
- Kemudia ekstrak file tersebut
- Tambah file yang berada di direktori “lib” dan “external directory” ke dalam path project anda. kemudian compile dan jalankan project.
- Berhasil
Sumber: http://www.prasannatech.net/2009/01/convert-pdf-text-parser-java-api-pdfbox.html

arumsha said,
12/12/2010 at 9:35 PM
Nice inpo mas… n keep posting… ^_^
sekalian materi skripsinya sak koding2 e..kuabeh wes….hwkwkwk…
boynurah said,
12/12/2010 at 9:56 PM
walah pertamax direbut km sar
ngko wes ta kasi materi skripsi lek wes mari yo,, hehehe
km lho jauh lebih master daripada aku, masa minta coding aku,, heheh
Shary_Tux said,
13/12/2010 at 7:56 PM
huwaaa??? master…. hmmm berat nih…hehe….
referensi lah mas….hwkwkwk… ***alibi
ndang mari yoh mas skripsine….selak pengen nyicipi rasane skripsi….hwkwkwkkwk ***lagi gaya mode…
boynurah said,
13/12/2010 at 9:54 PM
amiin thanks ya sar, moga cepet nikah
edgo said,
12/12/2010 at 10:17 PM
wah ini sih agak repot, lbh praktis pake app pdf to txt
boynurah said,
12/12/2010 at 10:20 PM
ya ini bukan untuk langsung dipakai mas, ini sebagai referensi klu ingin membuat program convert pdf menggunakan java