Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
education
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
李丛阳
education
Commits
828ac26d
Commit
828ac26d
authored
Mar 31, 2020
by
luzhuang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cutPdf工具修改
parent
1147eebc
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
17 deletions
+40
-17
CutPdfUtil.java
src/main/java/org/rcisoft/core/util/CutPdfUtil.java
+40
-17
No files found.
src/main/java/org/rcisoft/core/util/CutPdfUtil.java
View file @
828ac26d
...
...
@@ -5,60 +5,83 @@ import com.itextpdf.text.DocumentException;
import
com.itextpdf.text.pdf.PdfCopy
;
import
com.itextpdf.text.pdf.PdfImportedPage
;
import
com.itextpdf.text.pdf.PdfReader
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Component
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
/**
* Created by yangzhaojun on 2018/2/6.
*/
@Component
@Slf4j
public
class
CutPdfUtil
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
public
static
void
main
(
String
[]
args
)
{
try
{
List
<
String
>
pdfList
=
toCutPdf
(
"F:\\桌面整理\\文件归纳\\QQ下载\\xx文字+图片-100页.pdf"
,
30
);
log
.
debug
(
pdfList
.
toString
());
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
}
/**
* 切割pdf 返回list
*
* @param filePath 源文件路径
* @param page 每个pdf页数
* @return
* @throws Exception
*/
public
static
List
<
String
>
toCutPdf
(
String
filePath
,
int
page
)
throws
Exception
{
List
<
String
>
pdfList
=
new
ArrayList
<>();
Date
dd
=
new
Date
();
long
l1
=
dd
.
getTime
();
int
i
=
0
;
int
j
=
1
;
boolean
finish
=
false
;
String
filePath
=
"E:\\pdfTest\\a9c9c7916e424c3aa44255564c31bbe9.pdf"
;
PdfReader
reader
=
new
PdfReader
(
filePath
);
int
pageNum
=
reader
.
getNumberOfPages
();
do
{
String
newFile
=
"E:\\pdfTest\\cutPdf\\a9c9c7916e424c3aa44255564c31bbe9"
+
j
+
".pdf"
;
int
end
=
i
+
30
;
if
(
end
>=
pageNum
)
{
do
{
String
newFile
Path
=
filePath
.
substring
(
0
,
filePath
.
lastIndexOf
(
"."
))
+
"-"
+
j
+
".pdf"
;
int
end
=
i
+
page
;
if
(
end
>=
pageNum
)
{
end
=
pageNum
;
finish
=
true
;
}
partitionPdfFile
(
reader
,
newFile
,
i
+
1
,
end
);
System
.
out
.
println
(
"第"
+
j
+
"个pdf转换完成"
);
i
+=
30
;
partitionPdfFile
(
reader
,
newFile
Path
,
i
+
1
,
end
);
log
.
debug
(
"第"
+
j
+
"个pdf转换完成"
);
i
+=
page
;
j
++;
}
while
(!
finish
);
pdfList
.
add
(
newFilePath
);
}
while
(!
finish
);
Date
dd2
=
new
Date
();
long
l2
=
dd2
.
getTime
();
System
.
out
.
println
(
"耗时:"
+(
l2
-
l1
));
log
.
debug
(
"耗时:"
+
(
l2
-
l1
));
return
pdfList
;
}
/**
* 截取pdfFile的第from页至第end页,组成一个新的文件名
*
* @param reader
* @param newFile
* @param newFile
Path
* @param from
* @param end
*/
public
static
void
partitionPdfFile
(
PdfReader
reader
,
String
newFile
,
int
from
,
int
end
)
{
String
newFile
Path
,
int
from
,
int
end
)
{
Document
document
=
null
;
PdfCopy
copy
=
null
;
try
{
document
=
new
Document
(
reader
.
getPageSize
(
1
));
copy
=
new
PdfCopy
(
document
,
new
FileOutputStream
(
newFile
));
copy
=
new
PdfCopy
(
document
,
new
FileOutputStream
(
newFile
Path
));
document
.
open
();
for
(
int
j
=
from
;
j
<=
end
;
j
++)
{
for
(
int
j
=
from
;
j
<=
end
;
j
++)
{
document
.
newPage
();
PdfImportedPage
page
=
copy
.
getImportedPage
(
reader
,
j
);
copy
.
addPage
(
page
);
...
...
@@ -67,7 +90,7 @@ public class CutPdfUtil {
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
catch
(
DocumentException
e
)
{
}
catch
(
DocumentException
e
)
{
e
.
printStackTrace
();
}
}
...
...
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