ossiejhmoore

PDF to Word (hard coded)

Oct 20th, 2017
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. import com.aspose.pdf.SaveFormat;
  2. import com.aspose.pdf.TextAbsorber;
  3. import com.aspose.words.LoadFormat;
  4. import com.aspose.words.LoadOptions;
  5. import java.io.File;
  6. import org.apache.commons.io.FileUtils;
  7.  
  8. public class PdfToText {
  9.  
  10.     public static void main(String[] args) throws Exception {
  11.  
  12.         PdfTxtRtf();
  13.  
  14.     }
  15.  
  16.     public static void PdfTxtRtf() throws Exception {
  17.         //init license keys
  18.         final com.aspose.pdf.License pdfLicense = new com.aspose.pdf.License();
  19.         pdfLicense.setLicense("Aspose.Total.Java.lic.xml");
  20.         final com.aspose.words.License docLicense = new com.aspose.words.License();
  21.         docLicense.setLicense("Aspose.Total.Java.lic.xml");
  22.  
  23.         File fPdf = new File("c:\\temp\\file.pdf");
  24.         File fTxt = new File("c:\\temp\\file.txt");
  25.         File fRtf = new File("c:\\temp\\file.rtf");
  26.  
  27.         //convert pdf to txt
  28.         com.aspose.pdf.Document pdf = new com.aspose.pdf.Document(fPdf.getAbsolutePath());
  29.         TextAbsorber textAbsorber = new TextAbsorber();
  30.         pdf.getPages().accept(textAbsorber);
  31.         String fucked = textAbsorber.getText();
  32.  
  33.         //fix fucked up text
  34.         String fixed = stripFuckers(fucked);
  35.         FileUtils.writeStringToFile(fTxt, fixed);
  36.  
  37.         //covnert text to rtf
  38.         LoadOptions options = new LoadOptions();
  39.         options.setLoadFormat(LoadFormat.TEXT);
  40.         com.aspose.words.Document doc = new com.aspose.words.Document(fTxt.getAbsolutePath(), options);
  41.         doc.save(fRtf.getAbsolutePath());
  42.  
  43.     }
  44.    
  45.     private static String stripFuckers(String fucked) {
  46.         StringBuilder fixed = new StringBuilder();
  47.         for (char c : fucked.toCharArray()) {
  48.             if (c == (char)0) {
  49.                 continue;
  50.             }
  51.             fixed.append(c);
  52.         }
  53.         return fixed.toString();
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment