java 用itextpdf读写pdf文件

xiaoxiao2021-02-28  48

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(); } } }
转载请注明原文地址: https://www.6miu.com/read-74876.html

最新回复(0)