上網路,好像有代碼的,你去查查我也記得不是很清楚那個代碼叫什麼了
『貳』 java 解析pdf表格
最近在幫公司做工具,需要讀取PDF中表格的數據。網上查了,大部分PDFBox讀取的代碼都大致相同,一行一行從頭讀到尾。嘗試讀取PDF表格的人可能會遇到表格有空數據時,列與列就會對不齊,這樣就不能很好地進行數據的處理了。網上看到一個例子,用iText坐標精確讀取的例子,參考以後出現了亞洲語種字體不支持,添加了語言包iTextAsian.jar導入字體後,結果發現列印的都是空格無法處理。後找到了PDFBox坐標讀取的方法,相當給力。在此過程中了解到有很多人遇到了我這樣的問題。所以寫下來望對現在還未解決問題還有以後遇到此問題的人提供幫助。
上代碼:
package com.pdfbox.util.test;
import org.apache.pdfbox.exceptions.InvalidPasswordException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.util.PDFTextStripperByArea;
import java.awt.Rectangle;
import java.util.List;
public class ExtractTextByArea
{
『叄』 JAVA實現讀取pdf模板,替換內容後生成新pdf文檔
為什麼要採用替換的方法呢?
讀取後經過處理,重新新建一個PDF,把內容寫到新的PDF裡面就行了,用這些jar可以實現了
我的意思是,你有PDF模板,然後用java讀取模板的內容,然後按你的要求對讀取的內容進行處理(這里的處理是根據你自己的需求而定,如:從資料庫讀取數據,然後填充到從模板讀取到的表格中),再把處理完後的內容重新寫到一個新建的PDF中
『肆』 java如何讓pdf模板中的數據網格的行數根據傳入數據進行變化
import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.ArrayList;import javax.swing.*;public class DemoJText extends JFrame {public DemoJText() {ArrayList jtfs = new ArrayList();//用於保存文本框Container c = this.getContentPane();JPanel jp = new JPanel();int row = 10;//行數int col = 9;//列數jp.setLayout(new GridLayout(row,col, 5, 5));for (int i = 0; i < 90; i++) {JTextField jtf= new JTextField("");jtfs.add(jtf);jp.add(jtf);}c.add(jp);JPanel jp1 = new JPanel();final JButton jb = new JButton("清除");final JButton jbOut = new JButton("控制台輸出");jp1.add(jb);jp1.add(jbOut);c.add(jp1, BorderLayout.SOUTH);jb.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {for (int i = 0; i < jtfs.size(); i++) {jtfs.get(i).setText("");}}});jbOut.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {for (int i = 0; i < jtfs.size(); i++) {String temp = jtfs.get(i).getText();//可以不用判斷全部輸出.我這里就先判斷下,如果不為空才輸出if(!temp.equals("")){//如果不為空就輸出int r = i/col+1;//行int c = i%col+1;//列System.out.println("第"+r+"行"+"第"+c+"列"+temp);}}}});this.setBounds(160, 250, 300, 350);this.setTitle("測試");this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setVisible(true);}public static void main(String[] args) {new DemoJText();}}輸出
『伍』 怎麼用java代碼生成pdf文檔
package com.qhdstar.java.pdf;
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfWriter;
/**
* 描述:TODO 【JAVA生成PDF】
* <p>
*
* @title GeneratePDF
* @author SYJ
* @email [email protected]
* @date 2013-4-6
* @version V1.0
*/
public class GeneratePDF {
public static void main(String[] args) {
//調用第一個方法,向C盤生成一個名字為ITextTest.pdf 的文件
try {
writeSimplePdf();
}
catch (Exception e) { e.printStackTrace(); }
//調用第二個方法,向C盤名字為ITextTest.pdf的文件,添加章節。
try {
writeCharpter();
}
catch (Exception e) { e.printStackTrace(); }
}
public static void writeSimplePdf() throws Exception {
// 1.新建document對象
// 第一個參數是頁面大小。接下來的參數分別是左、右、上和下頁邊距。
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
// 2.建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁碟中。
// 創建 PdfWriter 對象 第一個參數是對文檔對象的引用,第二個參數是文件的實際名稱,在該名稱中還會給出其輸出路徑。
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\ITextTest.pdf"));
// 3.打開文檔
document.open();
// 4.向文檔中添加內容
// 通過 com.lowagie.text.Paragraph 來添加文本。可以用文本及其默認的字體、顏色、大小等等設置來創建一個默認段落
document.add(new Paragraph("First page of the document."));
document.add(new
Paragraph("Some more text on the first page with different color and
font type.", FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new
Color(255, 150, 200))));
// 5.關閉文檔
document.close();
}
/**
* 添加含有章節的pdf文件
*
* @throws Exception
*/
public static void writeCharpter() throws Exception {
// 新建document對象 第一個參數是頁面大小。接下來的參數分別是左、右、上和下頁邊距。
Document document = new Document(PageSize.A4, 20, 20, 20, 20);
// 建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁碟中。
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:\\ITextTest.pdf"));
// 打開文件
document.open();
// 標題
document.addTitle("Hello mingri example");
// 作者
document.addAuthor("wolf");
// 主題
document.addSubject("This example explains how to add metadata.");
document.addKeywords("iText, Hello mingri");
document.addCreator("My program using iText");
// document.newPage();
// 向文檔中添加內容
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new
Paragraph("Some more text on the first page with different color and
font type.", FontFactory.getFont(FontFactory.defaultEncoding, 10,
Font.BOLD, new Color(0, 0, 0))));
Paragraph title1 = new
Paragraph("Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 18,
Font.BOLDITALIC, new Color(0, 0, 255)));
// 新建章節
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
Paragraph
title11 = new Paragraph("This is Section 1 in Chapter 1",
FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255,
0, 0)));
Section section1 = chapter1.addSection(title11);
Paragraph someSectionText = new Paragraph("This text comes as part of section 1 of chapter 1.");
section1.add(someSectionText);
someSectionText = new Paragraph("Following is a 3 X 2 table.");
section1.add(someSectionText);
document.add(chapter1);
// 關閉文檔
document.close();
}
}
『陸』 Java如何使用Java創建一個空的PDF文檔
package com.yii;import java.io.IOException;import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.pdmodel.PDPage;// 需要下 apache pdfbox包和apache.commons.loggin烏,下載地址:http://pdfbox.apache.org/download.cgi 和 http://commons.apache.org/proper/commons-logging/download_logging.cgi// 在本示例中下載使用的是:pdfbox-2.0.7.jar // 將下載的pdfbox-2.0.7.jar添加到Eclipse項目依懶庫中。// 右鍵點擊:"java_apache_pdf_box"->"Bulid Path"->"Add External Artchives...",然後選篤下載的"pdfbox-2.0.7.jar"和"commons-logging-1.2.jar"文件 public class CreatingEmptyPdf {
public static void main(String args[]) throws IOException {
// Creating PDF document object
PDDocument document = new PDDocument();
// Add an empty page to it
document.addPage(new PDPage());
// Saving the document
document.save("F:/worksp/javaexamples/java_apache_pdf_box/BlankPdf.pdf");
System.out.println("PDF created");
// Closing the document
document.close();
}}
『柒』 怎麼用java動態生成pdf文檔
Flying-Saucer + iText + Velocity
1. 第一步
將jar包放到你的工程里,需要的jar如下:
bcprov-jdk15-140.jar
core-renderer.jar
iText-2.0.8.jar
iTextAsian.jar
velocity-1.4.jar
Jar包下載地址:http://code.google.com/p/flying-saucer/downloads/list
2. 第二步
設計模版,進行排版調整樣式,css樣式也可以導入@import 等,通過Velocity模版引擎動態替換 頁面內容,以下是模版內容:
<?xml version="1.0" encoding="UTF-8" ?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PDF模版</title>
<style type="text/css">
<!--
body {
font: 100% Verdana, Arial, Helvetica, sans-serif;
margin: 0;
padding: 0;
text-align: center;
color: #000000;
}
.oneColLiqCtrHdr #container {
width: 100%;
margin: 0 auto;
text-align: left;
}
div.header-left {display: none}
div.header-right {display: none}
div.footer-left {display: none}
div.footer-right {display: none}
『捌』 java生成pdf幾種常見方式
用Spire.PDF for Java來生成PDF文檔的效果不錯,支持格式化操作比較多,可以參考下 Java 中創建 PDF 文檔
『玖』 Java能直接修改pdf文件嗎
貌似沒見過這個插件,還有pdf有一部分是根本不能修改的,哪怕用專業工具都不行,如果pdf裡面的文字可以讀取出來,倒是可以導入txt然後就很好修改了,pdf圖文混排,覺得java修改難度較大