Java PDF File Operations

Source: Internet
Author: User

To read a PDF file, you must have export box1.7.1.jar and fontbox1.7.1.jar. You can go to the official website or my resources. The following code

Import java. io. fileWriter; import java. io. IOException; import java. io. printWriter; import org.apachedomainbox. pdmodel. PDDocument; import org.apachedomainbox. util. extends textstripper; public class Mytest {public static String getText (String file) {String s = ""; String pdffile = file; PDDocument upload Doc = null; try {export Doc = PDDocument. load (pdffile); extends textstripper stripper = new extends textstripper (); s = stripper. getTe Xt (release DOC);} catch (IOException e) {e. printStackTrace ();} finally {try {if (release Doc! = Null) {Doc. close () ;}} catch (IOException e) {e. printStackTrace () ;}} return s;} public static void toTextFile (String doc, String filename) throws Exception {String pdffile = doc; PDDocument doc = null; PrintWriter pw = null; try {export Doc = PDDocument. load (pdffile); inclutextstripper stripper = new inclutextstripper (); pw = new PrintWriter (new FileWriter (filename); stripper. writeText (writable doc, pw);} catch (IOException e) {e. printStackTrace ();} finally {try {if (pw! = Null) {pw. close () ;}if (batch Doc! = Null) {Doc. close () ;}} catch (IOException e) {e. printStackTrace () ;}} public static void main (String [] args) {try {String SC = getText ("Enter the path of the PDF file to be converted here"); System. out. print (SC); toTextFile ("Enter the path of the PDF file to be converted", "Path of the txt file to be saved");} catch (Exception e) {e. printStackTrace ();}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.