当前位置: 移动技术网 > IT编程>开发语言>Java > java使用pdfbox操作pdf文件示例

java使用pdfbox操作pdf文件示例

2019年07月22日  | 移动技术网IT编程  | 我要评论

还有一个用于创建pdf文件的项目----itext。

pdfbox下面有两个子项目:fontbox是一个处理pdf字体的java类库;jempbox是一个处理xmp元数据的java类库。

一个简单示例:

要引入pdfbox-app-1.6.0.jar这个包。

复制代码 代码如下:

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();
        }
    }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网