java 用itextpdf读写pdf文件

来源:互联网 发布:java decimalformat 编辑:程序博客网 时间:2024/06/07 19:25
import com.itextpdf.text.pdf.PdfReader;import com.itextpdf.text.pdf.parser.PdfTextExtractor;import java.io.IOException;public class PdfReadExample {private static final String FILE_NAME = "D:\\test\\11.pdf";    public static void main(String[] args) {        PdfReader reader;        try {            reader = new PdfReader(FILE_NAME);            // pageNumber = 1            String textFromPage = PdfTextExtractor.getTextFromPage(reader, 1);            System.out.println(textFromPage);            reader.close();        } catch (IOException e) {            e.printStackTrace();        }    }}
import com.itextpdf.text.*;import com.itextpdf.text.pdf.PdfWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class PdfWriteExample {    private static final String FILE_NAME = "D:\\test\\11.pdf";    public static void main(String[] args) throws IOException {        writeUsingIText();    }    private static void writeUsingIText() throws IOException {        Document document = new Document();        try {            PdfWriter.getInstance(document, new FileOutputStream(new File(FILE_NAME)));            //open            document.open();            Paragraph p = new Paragraph();            p.add("This is my paragraph 1");            p.setAlignment(Element.ALIGN_CENTER);            document.add(p);            Paragraph p2 = new Paragraph();            p2.add("This is my paragraph 2"); //no alignment            document.add(p2);            Font f = new Font();            f.setStyle(Font.BOLD);            f.setSize(8);            document.add(new Paragraph("This is my paragraph 3", f));            //close            document.close();            System.out.println("Done");        } catch (FileNotFoundException e ) {            e.printStackTrace();        } catch ( DocumentException e) {            e.printStackTrace();        }    }}

阅读全文
0 0
原创粉丝点击