import com.aspose.pdf.SaveFormat;
import com.aspose.pdf.TextAbsorber;
import com.aspose.words.LoadFormat;
import com.aspose.words.LoadOptions;
import java.io.File;
import org.apache.commons.io.FileUtils;
public class PdfToText {
public static void main(String[] args) throws Exception {
PdfTxtRtf();
}
public static void PdfTxtRtf() throws Exception {
//init license keys
final com.aspose.pdf.License pdfLicense = new com.aspose.pdf.License();
pdfLicense.setLicense("Aspose.Total.Java.lic.xml");
final com.aspose.words.License docLicense = new com.aspose.words.License();
docLicense.setLicense("Aspose.Total.Java.lic.xml");
File fPdf = new File("c:\\temp\\file.pdf");
File fTxt = new File("c:\\temp\\file.txt");
File fRtf = new File("c:\\temp\\file.rtf");
//convert pdf to txt
com.aspose.pdf.Document pdf = new com.aspose.pdf.Document(fPdf.getAbsolutePath());
TextAbsorber textAbsorber = new TextAbsorber();
pdf.getPages().accept(textAbsorber);
String fucked = textAbsorber.getText();
//fix fucked up text
String fixed = stripFuckers(fucked);
FileUtils.writeStringToFile(fTxt, fixed);
//covnert text to rtf
LoadOptions options = new LoadOptions();
options.setLoadFormat(LoadFormat.TEXT);
com.aspose.words.Document doc = new com.aspose.words.Document(fTxt.getAbsolutePath(), options);
doc.save(fRtf.getAbsolutePath());
}
private static String stripFuckers(String fucked) {
StringBuilder fixed = new StringBuilder();
for (char c : fucked.toCharArray()) {
if (c == (char)0) {
continue;
}
fixed.append(c);
}
return fixed.toString();
}
}
Comments