java实现Img与PDF相互转换

2025-05-29 0 90

本文实例为大家分享了java实现img与pdf相互转换的具体代码,供大家参考,具体内容如下

不善于表达,就直接贴出代码吧。请大牛忽视我。

?

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

* 本工具对实现对img与pdf相互转换。

* 运行测试需要导入以下2个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转jpg

//pdftojpg("e:\\\\java\\\\资料pdf\\\\1.pdf","e:\\\\java\\\\资料pdf\\\\1.jpg",1);

//将多个jpg直接合并成pdf包

//extractionpdf("f:\\\\temp\\\\project\\\\数据\\\\dfdsfds\\\\巴黎公社活动家传略_img","f:\\\\temp\\\\project\\\\数据\\\\dfdsfds\\\\巴黎公社活动家传略_img.pdf");

//jpg转pdf

//jpgtopdf();

//文件排序

//listorder();

imgfiletool.imgmeragetopdf(new file("f:\\\\temp\\\\project\\\\数据\\\\dfdsfds\\\\巴黎公社活动家传略_img").listfiles(),new file("f:\\\\temp\\\\project\\\\数据\\\\dfdsfds\\\\","巴黎公社活动家传略.pdf"));

}

private static void listorder() {

file[] listfiles = new file("f:\\\\temp\\\\project\\\\数据\\\\dfdsfds\\\\巴黎公社活动家传略_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:对图片文件通过treemap以名称进行自然排序

map<integer,file> mif = new treemap<integer,file>();

for(file f : list)

mif.put(integer.parseint(f.getname().replaceall(".jpg$", "")), f);

//2:获取第一个img的宽、高做为pdf文档标准

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:通过宽高 ,实例化pdf文档对象。

document document = new document(new rectangle(width,height));

pdfwriter pdfwr = pdfwriter.getinstance(document, new fileoutputstream(file));

document.open();

//4:获取每一个图片文件,转为img对象。装载到document对象中

for(entry<integer,file> eif : mif.entryset())

{

//4.1:读取到内存中

baos = new bytearrayoutputstream(2048*3);

is = new fileinputstream(eif.getvalue());

for(int len;(len=is.read())!=-1;)

baos.write(len);

baos.flush();

//4.2通过byte字节生成img对象

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 目标文件

* @param x 读取源文件中的第几页

*/

private static void pdftojpg(string source,string target,int x) throws exception {

//创建从中读取和向其中写入(可选)的随机访问文件流,r表示对其只是访问模式

randomaccessfile rea = new randomaccessfile(new file(source), "r");

//将流读取到内存中,然后还映射一个pdf对象

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页

* @throws exception

*/

private static void pdfextraction(string source,string target,int pagenum) throws exception{

//1:创建pdf读取对象

pdfreader pr = new pdfreader(source);

system.out.println("this document "+pr.getnumberofpages()+" page");

//2:将第page页转为提取,创建document对象

document doc = new document(pr.getpagesize(pagenum));

//3:通过pdfcopy转其单独存储

pdfcopy copy = new pdfcopy(doc, new fileoutputstream(new file(target)));

doc.open();

doc.newpage();

//4:获取第1页,装载到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 {

//文件转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转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();

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java实现Img与PDF相互转换 https://www.kuaiidc.com/111790.html

相关文章

发表评论
暂无评论