杩欎釜鎯虫硶鐨勮捣鍥犳槸锛屽疄楠屾姤鍛婅鍙岄潰鎵撳嵃锛岀劧鑰屽鑸嶇殑鎵撳嵃鏈哄彧鑳芥墦鍗板崟闈㈢殑楦紝濂藉惂鍏跺疄涓昏鍘熷洜鏄懆鏈お鏃犺亰浜�(鐪嬩簡鍗佸嚑闆�铚$瑪灏忔柊锛燂紵锛屼箣鍚庢兂鎵剧偣浜嬫儏鍋�
鎬庝箞鍋氬憿锛屾敼閫犳墦鍗版満鏄笉鍙兘鐨勪簡銆�
閭e氨鍙兘浠庢墦鍗扮殑鎶ュ憡涓婇潰鎼炵偣浜嬫儏浜嗐��
寰堝鏄撴兂鍒颁簡锛屽彲浠ュ厛鎵撳嵃濂囨暟椤碉紝鍐嶆妸鎵撳嵃濂藉鏁伴〉鐨勭焊鏀捐繘鎵撳嵃鏈猴紝鏉ユ墦鍗板伓鏁伴潰銆�
閭e氨鎶婃姤鍛婃敼鎴愬鏁伴〉鍦ㄥ墠锛屽伓鏁伴〉鏀惧湪鍚庨潰灏卞彲浠ヤ簡鍚э紝
姣斿鎶婂師鏉ュ疄楠屾姤鍛婄殑绗�1銆�3銆�5椤垫斁鍦ㄦ柊鎶ュ憡鏈�鍓嶉潰锛涚2銆�4銆�6鏀惧湪鏂版姤鍛婂悗闈紝鎵撳嵃鐨勬椂鍊欏垎涓ゆ鎵擄紝鍏堟墦鍗板墠涓�鍗婏紝鍐嶇敤杩欎簺绾告墦鍗板悗涓�鍗娿��
鏈変簡鎯虫硶锛屽氨鍙互寮�濮嬪啓浠g爜鏉ュ疄鐜颁簡銆傚啓涓�涓皬绋嬪簭鎶奝DF鏂囨。鏀归�犱竴涓嬨��
鐧惧害浜嗕笅濂藉儚鐢╬ython鏇村鏄擄紝浣嗘槸鐜板湪灏忔湅鍙嬮兘瀛ython浜嗭紝鎴戝凡缁忔槸澶ф湅鍙嬩簡锛屽簲璇ョ敤java鍐欙紒(澶ф湅鍙嬭繕鐪嬭湣绗斿皬鏂帮紵锛�
鐢╦ava鍐欙紝涔熷緢绠�鍗曪紝鏌ヤ簡涓�涓嬩娇鐢╥textpdf鍖呭氨鍙互浜嗭紱
itextpdf鍖�锛屽彲浠ュ緢瀹规槗瀵筽df鏂囦欢杩涜澧炲垹鏀癸紱
鍙渶瑕佹寜鐜板湪鐨勯渶姹傦紝鍏堟妸鍘熷疄楠屾姤鍛婄殑濂囨暟椤靛姞鍏ュ埌鏂皃df鏂囦欢锛屽啀鎶婂伓鏁伴〉鍔犲叆鍒版柊pdf鏂囦欢鏈熬锛屽舰鎴愪竴涓柊鐨刾df鏂囦欢锛屽氨鍙互浜嗭紱
寰堢畝鍗曠殑浠g爜濡備笅馃憞
package pdfTransform;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;
public class PdfTransform {
//鏈�缁坧df淇濆瓨鐨勮矾寰�
public static String final_path = "C:\\pdf\\outPdf2.pdf";
/*
*@desc:鍒涘缓涓�涓柊鐨刾df,浼犲叆鍙傛暟(闇�瑕佹墦鍗扮殑鏃df鎶ュ憡),鍏堢瓫閫夋棫pdf鎶ュ憡鐨勫鏁伴〉鍔犲叆鏂皃df,鍐嶇瓫閫夊伓鏁伴〉鍔犲叆鏂皃df
*@parm:myReader:PdfReader,old pdf
*@return out:FileOutputStream,a new pdf
*/
public static FileOutputStream pdfSolve(PdfReader myReader) {
Document document = new Document();
PdfWriter writer;
FileOutputStream out = null;
try {
out = new FileOutputStream(final_path);
writer = PdfWriter.getInstance(document, out);
document.open();
PdfContentByte contentByte = writer.getDirectContent();
int currentPage = 1;
//鍔犲叆濂囨暟椤靛埌鏂皃df
while(currentPage <= myReader.getNumberOfPages()) {
document.newPage();
if(currentPage % 2 != 0) {
PdfImportedPage page = writer.getImportedPage(myReader, currentPage);
contentByte.addTemplate(page, 0,0);
}
currentPage++;
}
currentPage = 1;
//鍐嶅姞鍏ュ伓鏁伴〉鍒版柊pdf
while(currentPage <= myReader.getNumberOfPages()) {
document.newPage();
if(currentPage % 2 == 0) {
PdfImportedPage page = writer.getImportedPage(myReader, currentPage);
contentByte.addTemplate(page, 0,0);
}
currentPage++;
}
document.close();
out.flush();
out.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return out;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
InputStream myStream = null;
PdfReader myReader;
try {
myStream = new FileInputStream("C:\\pdf\\inputPdf2.pdf"); //鍘熸棫pdf鎶ュ憡鐨勮矾寰�
myReader = new PdfReader(myStream);
PdfTransform.pdfSolve(myReader);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
褰撶劧杩欐牱鎵撳嵃瀹屽鏁伴〉鍚庯紝杩橀渶瑕佹墜鍔ㄦ斁绾稿紶,閲嶆柊鎵撳嵃鍋舵暟椤碉紱鎵嬪姩鏀剧焊杩欑浜嬶紝浜ょ粰鑸嶅弸鍋氬氨濂戒簡銆�
鐒跺悗鑸嶅弸璇村嚭浜嗙湡鐩革紝鎵撳嵃鐨勬椂鍊欑洿鎺ュ厛閫夊鏁伴〉锛屽啀閫夊伓鏁伴〉灏卞ソ浜嗭紝骞插槢杩樹慨鏀筽df銆傘��
濂藉儚寰堟湁閬撶悊锛屼笉绠′簡鍙嶆閮藉仛浜嗐��
鏈�鍚庢兂璇达紝铚$瑪灏忔柊鐪熺殑濂界湅锛屽皬鏃跺�欐病鐪嬭繃锛岀幇鍦ㄨ琛ュ洖鏉�