Java using PDFBox to manipulate PDF files Sample _java

Source: Internet
Author: User
Tags gettext

There is also a project for creating a PDF file----iText.

PDFBox has two subprojects below: Fontbox is a Java class library that handles PDF fonts; Jempbox is a Java class library that handles XMP metadata.

A simple example:

To introduce Pdfbox-app-1.6.0.jar this package.

Copy Code code as follows:

Package PDF;

Import Java.io.File;
Import java.net.MalformedURLException;

Import org.apache.pdfbox.pdmodel.PDDocument;
Import Org.apache.pdfbox.util.PDFTextStripper;

public class Strippdfcontent {

public static String getText (file file) throws exception{
Boolean sort=false;
int startpage=1;
int endpage=10;
PDDocument Document=null;
try{
try{
Document=pddocument.load (file);
}catch (Malformedurlexception e) {

}
Pdftextstripper stripper=new pdftextstripper ();
Stripper.setsortbyposition (sort);
Stripper.setstartpage (StartPage);
Stripper.setendpage (EndPage);
return Stripper.gettext (document);
}catch (Exception e) {
E.printstacktrace ();
Return "";
}finally{
if (document!=null) {
Document.close ();
}
}
}

public static void Main (string[] args) {
File File=new file ("/home/orisun/123.pdf");
try{
String cont=gettext (file);
SYSTEM.OUT.PRINTLN (cont);
}catch (Exception e) {
System.out.println ("Strip failed.");
E.printstacktrace ();
}
}
}

Related Article

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.