国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

香港云服务器
服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - java實(shí)現(xiàn)Img與PDF相互轉(zhuǎn)換

java實(shí)現(xiàn)Img與PDF相互轉(zhuǎn)換

2021-04-25 12:02田埂上的夢(mèng)想 Java教程

這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)Img與PDF相互轉(zhuǎn)換的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java實(shí)現(xiàn)img與pdf相互轉(zhuǎn)換的具體代碼,供大家參考,具體內(nèi)容如下

不善于表達(dá),就直接貼出代碼吧。請(qǐng)大牛忽視我。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import java.awt.image.bufferedimage;
import java.io.bytearrayoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.inputstream;
import java.io.randomaccessfile;
import java.nio.bytebuffer;
import java.nio.channels.filechannel;
import java.util.map;
import java.util.map.entry;
import java.util.treemap;
 
import com.utils.imgfiletool;
import com.lowagie.text.document;
import com.lowagie.text.image;
import com.lowagie.text.rectangle;
import com.lowagie.text.pdf.pdfcopy;
import com.lowagie.text.pdf.pdfimportedpage;
import com.lowagie.text.pdf.pdfreader;
import com.lowagie.text.pdf.pdfwriter;
import com.sun.image.codec.jpeg.jpegcodec;
import com.sun.image.codec.jpeg.jpegimageencoder;
import com.sun.pdfview.pdffile;
import com.sun.pdfview.pdfpage;
 
/**
 *
 * @author hubiao
 * @datetime 2014-06-07
 *   本工具對(duì)實(shí)現(xiàn)對(duì)img與pdf相互轉(zhuǎn)換。
 *   運(yùn)行測(cè)試需要導(dǎo)入以下2個(gè)jar包
 *     itext-2.0.2.jar  
 *     pdfrenderer.jar
 *
 */
@suppresswarnings("unused")
public class imgpdfutils {
  public static void main(string[] args) throws exception {
    //pdf包提取 pdf
    //pdfextraction();
     
    //pdf轉(zhuǎn)jpg
    //pdftojpg("e:\\java\\資料pdf\\1.pdf","e:\\java\\資料pdf\\1.jpg",1);
     
    //將多個(gè)jpg直接合并成pdf包
    //extractionpdf("f:\\temp\\project\\數(shù)據(jù)\\dfdsfds\\巴黎公社活動(dòng)家傳略_img","f:\\temp\\project\\數(shù)據(jù)\\dfdsfds\\巴黎公社活動(dòng)家傳略_img.pdf");
     
    //jpg轉(zhuǎn)pdf
    //jpgtopdf();
     
    //文件排序
    //listorder();
     
    imgfiletool.imgmeragetopdf(new file("f:\\temp\\project\\數(shù)據(jù)\\dfdsfds\\巴黎公社活動(dòng)家傳略_img").listfiles(),new file("f:\\temp\\project\\數(shù)據(jù)\\dfdsfds\\","巴黎公社活動(dòng)家傳略.pdf"));
  }
   
  private static void listorder() {
     
    file[] listfiles = new file("f:\\temp\\project\\數(shù)據(jù)\\dfdsfds\\巴黎公社活動(dòng)家傳略_img").listfiles();
    treemap<integer, file> tree = new treemap<integer, file>();
    for(file f : listfiles)
    {
      tree.put(integer.parseint(f.getname().replaceall(".jpg$", "")), f);
    }
    for(entry<integer, file> eif : tree.entryset())
    {
      system.out.println(eif.getkey()+"="+eif.getvalue().tostring());
    }
  }
  /**
   * @param list 圖片集合
   * @param file 保存路徑
   * @return true,合并完成
   *   如果文件名不是1.jpg,2.jpg,3.jpg,4.jpg這樣的。則需要自己重寫treemap的排序方式!
   */
  public static boolean imgmeragetopdf(file[] list, file file)throws exception {
    //1:對(duì)圖片文件通過(guò)treemap以名稱進(jìn)行自然排序
    map<integer,file> mif = new treemap<integer,file>();
    for(file f : list)
      mif.put(integer.parseint(f.getname().replaceall(".jpg$", "")), f);
     
    //2:獲取第一個(gè)img的寬、高做為pdf文檔標(biāo)準(zhǔn)
    bytearrayoutputstream baos = new bytearrayoutputstream(2048*3);
    inputstream is = new fileinputstream(mif.get(1));
    for(int len;(len=is.read())!=-1;)
      baos.write(len);
     
    baos.flush();
    image image = image.getinstance(baos.tobytearray());
    float width = image.width();
    float height = image.height();
    baos.close();
     
    //3:通過(guò)寬高 ,實(shí)例化pdf文檔對(duì)象。
    document document = new document(new rectangle(width,height));
    pdfwriter pdfwr = pdfwriter.getinstance(document, new fileoutputstream(file));
    document.open();
     
    //4:獲取每一個(gè)圖片文件,轉(zhuǎn)為img對(duì)象。裝載到document對(duì)象中
    for(entry<integer,file> eif : mif.entryset())
    {
      //4.1:讀取到內(nèi)存中
      baos = new bytearrayoutputstream(2048*3);
      is = new fileinputstream(eif.getvalue());
      for(int len;(len=is.read())!=-1;)
        baos.write(len);
      baos.flush();
       
      //4.2通過(guò)byte字節(jié)生成img對(duì)象
      image = image.getinstance(baos.tobytearray());
      image.getinstance(baos.tobytearray());
      image.setabsoluteposition(0.0f, 0.0f);
       
      //4.3:添加到document中
      document.add(image);
      document.newpage();
      baos.close();
    }
     
    //5:釋放資源
    document.close();
    pdfwr.close();
     
    return true;
  }
  /**
   *
   * @param source 源文件
   * @param target 目標(biāo)文件
   * @param x 讀取源文件中的第幾頁(yè)
   */
  private static void pdftojpg(string source,string target,int x) throws exception {
    //創(chuàng)建從中讀取和向其中寫入(可選)的隨機(jī)訪問(wèn)文件流,r表示對(duì)其只是訪問(wèn)模式
    randomaccessfile rea = new randomaccessfile(new file(source), "r");
 
    //將流讀取到內(nèi)存中,然后還映射一個(gè)pdf對(duì)象
    filechannel channel = rea.getchannel();
    bytebuffer buf = channel.map(filechannel.mapmode.read_only,0, channel.size());
    pdffile pdffile = new pdffile(buf);
    pdfpage page = pdffile.getpage(x); 
 
    // get the width and height for the doc at the default zoom 
    java.awt.rectangle rect = new java.awt.rectangle(0, 0, (int) page.getbbox() 
        .getwidth(), (int) page.getbbox().getheight()); 
 
    // generate the image 
    java.awt.image img = page.getimage(rect.width, rect.height, // width &
        rect, // clip rect
        null, // null for the imageobserver
        true, // fill background with white
        true // block until drawing is done
        ); 
 
    bufferedimage tag = new bufferedimage(rect.width, rect.height, 
        bufferedimage.type_int_rgb); 
     
    tag.getgraphics().drawimage(img, 0, 0, rect.width, rect.height, 
        null); 
    fileoutputstream out = new fileoutputstream(target); // 輸出到文件流 
    jpegimageencoder encoder = jpegcodec.createjpegencoder(out); 
    encoder.encode(tag); // jpeg編碼 
    out.close();    
  }
  /**
   * @param source 源pdf文件路徑
   * @param target 保存pdf文件路徑
   * @param pagenum 提取pdf中第pagenum頁(yè)
   * @throws exception 
   */
  private static void pdfextraction(string source,string target,int pagenum) throws exception{
    //1:創(chuàng)建pdf讀取對(duì)象
    pdfreader pr = new pdfreader(source);
    system.out.println("this document "+pr.getnumberofpages()+" page");
     
    //2:將第page頁(yè)轉(zhuǎn)為提取,創(chuàng)建document對(duì)象
    document doc = new document(pr.getpagesize(pagenum));
     
    //3:通過(guò)pdfcopy轉(zhuǎn)其單獨(dú)存儲(chǔ)
    pdfcopy copy = new pdfcopy(doc, new fileoutputstream(new file(target)));
    doc.open();
    doc.newpage();
     
    //4:獲取第1頁(yè),裝載到document中。
    pdfimportedpage page = copy.getimportedpage(pr,pagenum);
    copy.addpage(page); 
     
    //5:釋放資源
    copy.close();
    doc.close();
    pr.close();
  }
  /**
   * @param pdffile 源pdf文件
   * @param imgfile  圖片文件
   */
  private static void jpgtopdf(file pdffile,file imgfile) throws exception {
    //文件轉(zhuǎn)img
    inputstream is = new fileinputstream(pdffile);
    bytearrayoutputstream baos = new bytearrayoutputstream();
    for(int i;(i=is.read())!=-1;)
    {
      baos.write(i);
    }
    baos.flush();
     
    //取得圖像的寬和高。
    image img = image.getinstance(baos.tobytearray());
    float width = img.width();
    float height = img.height();
    img.setabsoluteposition(0.0f, 0.0f);//取消偏移
    system.out.println("width = "+width+"\theight"+height);
     
    //img轉(zhuǎn)pdf
    document doc = new document(new rectangle(width,height));
    pdfwriter pw = pdfwriter.getinstance(doc,new fileoutputstream(imgfile));
    doc.open();
    doc.add(img);
     
    //釋放資源
    system.out.println(doc.newpage());
    pw.flush();
    baos.close();
    doc.close();
    pw.close();
  }
   
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/hubiao_0618/article/details/29226883

延伸 · 閱讀

精彩推薦
1274
主站蜘蛛池模板: 国产精品区一区二区三含羞草 | 欧美日韩在线免费观看 | 国内久久精品 | 波多野结衣一二三四区 | 久久久久久免费毛片精品 | 99手机在线视频 | 中文字幕日韩欧美 | 黑人草逼视频 | 亚洲电影在线观看 | 欧美一区在线观看视频 | 婷婷精品久久久久久久久久不卡 | 欧美日韩在线一区 | 国产在线看片 | 欧美成年网站 | 久久99精品久久久久久 | 久草成人网 | 成人精品在线观看 | 亚洲欧美中文字幕 | 欧美成人精品一区二区 | 国产日 | 日本中文字幕在线免费观看 | 日韩中文在线 | 一区视频网站 | 日韩欧美中文字幕在线观看 | 午夜精品久久久久久 | 秋霞电影院午夜伦 | 影音先锋男人网 | 青青久久av北条麻妃黑人 | 欧美日韩成人 | 黄色片免费在线观看 | 中文在线播放 | 久久不卡| 亚洲电影一区二区 | 婷婷丁香综合 | 韩国三级午夜理伦三级三 | 久久亚洲二区 | 亚洲天天干| 精品国产成人 | 黄篇免费观看 | 国产黄色av| 久久久91精品国产一区二区三区 |