Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
sample-form-platform
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
赵啸非
sample-form-platform
Commits
07d5826f
Commit
07d5826f
authored
Jan 03, 2025
by
赵啸非
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改同步任务
parent
b4ee0703
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
129 additions
and
11 deletions
+129
-11
pom.xml
pom.xml
+4
-0
sample-form-manager/pom.xml
sample-form-manager/pom.xml
+8
-0
sample-form-manager/src/main/java/com/mortals/xhx/common/utils/WordUtil.java
.../src/main/java/com/mortals/xhx/common/utils/WordUtil.java
+117
-11
No files found.
pom.xml
View file @
07d5826f
...
...
@@ -85,6 +85,10 @@
<configuration>
<delimiters>
@
</delimiters>
<useDefaultDelimiters>
false
</useDefaultDelimiters>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>
exe
</nonFilteredFileExtension>
<nonFilteredFileExtension>
doc
</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>
...
...
sample-form-manager/pom.xml
View file @
07d5826f
...
...
@@ -235,6 +235,14 @@
<artifactId>
hutool-all
</artifactId>
<version>
5.7.14
</version>
</dependency>
<dependency>
<groupId>
net.coobird
</groupId>
<artifactId>
thumbnailator
</artifactId>
<version>
0.4.20
</version>
</dependency>
</dependencies>
<build>
...
...
sample-form-manager/src/main/java/com/mortals/xhx/common/utils/WordUtil.java
View file @
07d5826f
package
com.mortals.xhx.common.utils
;
/**
* @author: zxfei
* @date: 2021/10/14 15:44
* @description:
**/
import
com.aspose.words.Document
;
import
com.aspose.words.GraphicsQualityOptions
;
import
com.aspose.words.ImageSaveOptions
;
import
com.aspose.words.SaveFormat
;
import
com.mortals.framework.exception.AppException
;
import
com.mortals.framework.util.DateUtils
;
import
freemarker.template.Configuration
;
import
freemarker.template.Template
;
import
lombok.extern.apachecommons.CommonsLog
;
import
lombok.extern.slf4j.Slf4j
;
import
net.coobird.thumbnailator.Thumbnails
;
import
org.apache.pdfbox.pdmodel.PDDocument
;
import
org.apache.pdfbox.rendering.PDFRenderer
;
...
...
@@ -20,6 +27,8 @@ import java.io.*;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
@Slf4j
public
class
WordUtil
{
...
...
@@ -69,10 +78,9 @@ public class WordUtil {
}
public
static
String
convertWordToJPEG
(
String
inputFile
,
String
jpegPath
)
{
try
{
log
.
info
(
String
.
format
(
"文件转换开始:%s"
,
DateUtils
.
getCurrStrDateTime
()
));
log
.
info
(
"word to jpg文件转换开始,inputFile:{},jepgPath:{}:{}"
,
inputFile
,
jpegPath
,
DateUtils
.
getCurrStrDateTime
(
));
// 转换开始前时间
long
old
=
System
.
currentTimeMillis
();
// 新建的PDF文件路径
...
...
@@ -80,15 +88,23 @@ public class WordUtil {
//FileOutputStream os = new FileOutputStream(file);
// 要转换的word文档的路径
Document
doc
=
new
Document
(
inputFile
);
// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
//doc.save(os, SaveFormat.JPEG);
ImageSaveOptions
options
=
new
ImageSaveOptions
(
SaveFormat
.
JPEG
);
options
.
setResolution
(
256
);
GraphicsQualityOptions
qualityOptions
=
new
GraphicsQualityOptions
();
RenderingHints
renderingHints
=
new
RenderingHints
(
RenderingHints
.
KEY_ANTIALIASING
,
RenderingHints
.
VALUE_ANTIALIAS_ON
);
qualityOptions
.
setRenderingHints
(
renderingHints
);
options
.
setGraphicsQualityOptions
(
qualityOptions
);
options
.
setJpegQuality
(
10
);
options
.
setPrettyFormat
(
true
);
options
.
setUseAntiAliasing
(
true
);
options
.
setUseHighQualityRendering
(
true
);
int
pageCount
=
doc
.
getPageCount
();
log
.
info
(
"转换的页数:{}"
,
pageCount
);
List
<
BufferedImage
>
imageList
=
new
ArrayList
<
BufferedImage
>();
for
(
int
i
=
0
;
i
<
pageCount
;
i
++)
{
OutputStream
output
=
new
ByteArrayOutputStream
();
...
...
@@ -96,26 +112,44 @@ public class WordUtil {
doc
.
save
(
output
,
options
);
ImageInputStream
imageInputStream
=
ImageIO
.
createImageInputStream
(
parse
(
output
));
imageList
.
add
(
ImageIO
.
read
(
imageInputStream
));
log
.
info
(
"转换{}页完成!"
,
i
);
}
log
.
info
(
"转换结束,开始拼接图片:{}"
,
pageCount
);
//BufferedImage mergeImage = mergeImage(false, imageList);
BufferedImage
mergeImage
=
moreToOne
(
imageList
);
BufferedImage
mergeImage
=
mergeImage
(
false
,
imageList
);
Thumbnails
.
of
(
mergeImage
)
.
scale
(
1
f
)
.
outputQuality
(
0.8f
).
toFile
(
file
);
ImageIO
.
write
(
mergeImage
,
"pn
g"
,
file
);
//ImageIO.write(mergeImage, "jep
g", file);
// 转换结束后时间
/* doc.save(jpegPath);*/
doc
.
cleanup
();
long
now
=
System
.
currentTimeMillis
();
//os.close();
log
.
info
(
"文件转换结束,共耗时:"
+
((
now
-
old
)
/
1000.0
)
+
"秒"
);
return
jpegPath
;
}
catch
(
Exception
e
)
{
e
.
printStackTrace
(
);
throw
new
AppException
(
String
.
format
(
"文件转换异常!"
,
e
.
getMessage
()));
log
.
error
(
"文件转换异常!"
,
e
);
throw
new
AppException
(
String
.
format
(
"文件转换异常!
%s
"
,
e
.
getMessage
()));
}
}
public
static
Integer
getPageByDoc
(
String
inputFile
)
{
try
{
//FileOutputStream os = new FileOutputStream(file);
// 要转换的word文档的路径
Document
doc
=
new
Document
(
inputFile
);
return
doc
.
getPageCount
();
}
catch
(
Exception
e
)
{
log
.
error
(
"获取doc异常!"
,
e
);
return
0
;
}
}
public
static
void
pdfToImages
(
String
filePath
,
String
jpegPath
)
{
log
.
info
(
String
.
format
(
"文件转换开始:%s"
,
DateUtils
.
getCurrStrDateTime
()));
log
.
info
(
String
.
format
(
"
pdf to images
文件转换开始:%s"
,
DateUtils
.
getCurrStrDateTime
()));
// 转换开始前时间
long
old
=
System
.
currentTimeMillis
();
File
file
=
new
File
(
jpegPath
);
...
...
@@ -209,6 +243,60 @@ public class WordUtil {
return
destImage
;
}
/**
* 将多张图片拼接成一张长图
*
* @return 拼接后的图片BufferedImage
*/
public
static
BufferedImage
moreToOne
(
List
<
BufferedImage
>
imgs
)
{
//传入图片少于1张直接返回
int
len
=
imgs
.
size
();
//使用一个数组将图片装起来
BufferedImage
[]
bufferedImages
=
new
BufferedImage
[
len
];
int
[][]
imageArrays
=
new
int
[
len
][];
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
try
{
//文件地址,可直接转换
bufferedImages
[
i
]
=
Thumbnails
.
of
(
imgs
.
get
(
i
))
//.size(400, 600)
.
width
(
595
)
.
outputQuality
(
0.8f
)
.
asBufferedImage
();
}
catch
(
Exception
e
)
{
log
.
error
(
"图片转换失败"
,
e
);
}
//获取图片宽高
int
width
=
bufferedImages
[
i
].
getWidth
();
int
height
=
bufferedImages
[
i
].
getHeight
();
// 从图片中读取RGB
imageArrays
[
i
]
=
new
int
[
width
*
height
];
imageArrays
[
i
]
=
bufferedImages
[
i
].
getRGB
(
0
,
0
,
width
,
height
,
imageArrays
[
i
],
0
,
width
);
}
//拼接图片的宽高
int
dstHeight
=
0
;
int
dstWidth
=
bufferedImages
[
0
].
getWidth
();
for
(
BufferedImage
bufferedImage
:
bufferedImages
)
{
dstWidth
=
Math
.
max
(
dstWidth
,
bufferedImage
.
getWidth
());
dstHeight
+=
bufferedImage
.
getHeight
();
}
// 生成新图片
BufferedImage
imageNew
=
null
;
try
{
imageNew
=
new
BufferedImage
(
dstWidth
,
dstHeight
,
BufferedImage
.
TYPE_INT_RGB
);
int
newHeight
=
0
;
for
(
int
i
=
0
;
i
<
bufferedImages
.
length
;
i
++)
{
imageNew
.
setRGB
(
0
,
newHeight
,
dstWidth
,
bufferedImages
[
i
].
getHeight
(),
imageArrays
[
i
],
0
,
dstWidth
);
newHeight
+=
bufferedImages
[
i
].
getHeight
();
}
}
catch
(
Exception
e
)
{
log
.
error
(
"图片拼接出错"
,
e
);
}
return
imageNew
;
}
public
static
ByteArrayInputStream
parse
(
OutputStream
out
)
throws
Exception
{
ByteArrayOutputStream
baos
=
new
ByteArrayOutputStream
();
...
...
@@ -218,5 +306,23 @@ public class WordUtil {
}
public
static
void
main
(
String
[]
args
)
{
String
docPath
=
"E:\\pic\\doc\\3.docx"
;
String
jpgPath
=
"E:\\pic\\doc\\3.jpg"
;
WordUtil
.
convertWordToJPEG
(
docPath
,
jpgPath
);
/*
String pdfPath = "E:\\pic\\pdf\\1.pdf";
String jpgPath = "E:\\pic\\jpg\\1.jpg";
WordUtil.pdfToImages(pdfPath, jpgPath);
*/
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment