maven
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc.free</artifactId>
<version>3.9.0</version>
</dependency>
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
正式代码
public static String fileUrlToPdf(String fileUrl, String pdfName, Integer type) {
URL url = null;
InputStream is = null;
HttpURLConnection httpUrl = null;
try {
url = new URL(fileUrl);
httpUrl = (HttpURLConnection) url.openConnection();
httpUrl.connect();
httpUrl.getInputStream();
is = httpUrl.getInputStream();
Document doc = new Document();
if (type == 1) {
doc.loadFromStream(is, FileFormat.Auto);
} else {
doc.loadFromStream(is, FileFormat.Docx);
}
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(doc);
if (type == 1) {
bookmarksNavigator.moveToBookmark("书签");
if (null == bookmarksNavigator.getCurrentBookmark()) {
bookmarksNavigator.moveToBookmark("签章");
}
} else {
bookmarksNavigator.moveToBookmark("签章");
if (null == bookmarksNavigator.getCurrentBookmark()) {
bookmarksNavigator.moveToBookmark("书签");
}
}
if (null != bookmarksNavigator.getCurrentBookmark()) {
Paragraph paragraph = new Paragraph(doc);
DocPicture picture = paragraph.appendPicture("cqjtj.png");
paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
picture.setTextWrappingStyle(TextWrappingStyle.Behind);
Integer high = -50;
Integer width = 0;
picture.setHorizontalPosition(width);
picture.setVerticalPosition(high);
TextBodyPart bodyPart = new TextBodyPart(doc);
bodyPart.getBodyItems().add(paragraph);
bookmarksNavigator.insertTextBodyPart(bodyPart);
doc.saveToFile(pdfName, com.spire.doc.FileFormat.PDF);
doc.close();
StorageClient1 fastClient = EditFileUtil.getFastClient();
File file1 = new File(pdfName);
FileInputStream fis = new FileInputStream(file1);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = fis.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
String fastId = fastClient.upload_file1(outStream.toByteArray(), "pdf", null);
fis.close();
outStream.close();
FileUtils.deleteFile(pdfName);
return fastId;
} else {
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (httpUrl != null) {
httpUrl.disconnect();
}
}
}
测试代码
public static void word(String path, String name) throws IOException {
Document doc = new Document();
doc.loadFromFile(path + name);
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(doc);
bookmarksNavigator.moveToBookmark("签章");
Paragraph paragraph = new Paragraph(doc);
DocPicture picture = paragraph.appendPicture("C:\\Users\\11296\\Desktop\\cqjtj.png");
paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
picture.setTextWrappingStyle(TextWrappingStyle.Behind);
Integer high = 0;
Integer width = 0;
high = -50;
picture.setHorizontalPosition(width);
picture.setVerticalPosition(high);
TextBodyPart bodyPart = new TextBodyPart(doc);
bodyPart.getBodyItems().add(paragraph);
bookmarksNavigator.insertTextBodyPart(bodyPart);
doc.saveToFile("C:\\Users\\11296\\Desktop\\word\\" + name.split("\\.")[0] + ".pdf", com.spire.doc.FileFormat.PDF);
doc.dispose();
System.out.println("完成");
}