Browse Source

add method

Looly 5 years ago
parent
commit
54fdf60bc3

+ 20 - 7
hutool-core/src/main/java/cn/hutool/core/io/file/PathUtil.java

@@ -94,8 +94,8 @@ public class PathUtil {
 	/**
 	 * 遍历指定path下的文件并做处理
 	 *
-	 * @param start    起始路径,必须为目录
-	 * @param visitor  {@link FileVisitor} 接口,用于自定义在访问文件时,访问目录前后等节点做的操作
+	 * @param start   起始路径,必须为目录
+	 * @param visitor {@link FileVisitor} 接口,用于自定义在访问文件时,访问目录前后等节点做的操作
 	 * @see Files#walkFileTree(Path, java.util.Set, int, FileVisitor)
 	 * @since 5.5.2
 	 */
@@ -169,7 +169,7 @@ public class PathUtil {
 	 * 通过JDK7+的 {@link Files#copy(Path, Path, CopyOption...)} 方法拷贝文件
 	 *
 	 * @param src     源文件路径,如果为目录只在目标中创建新目录
-	 * @param target    目标文件或目录,如果为目录使用与源文件相同的文件名
+	 * @param target  目标文件或目录,如果为目录使用与源文件相同的文件名
 	 * @param options {@link StandardCopyOption}
 	 * @return Path
 	 * @throws IORuntimeException IO异常
@@ -191,14 +191,14 @@ public class PathUtil {
 	 * 拷贝文件或目录
 	 *
 	 * @param src     源文件路径,如果为目录只在目标中创建新目录
-	 * @param target    目标文件或目录,如果为目录使用与源文件相同的文件名
+	 * @param target  目标文件或目录,如果为目录使用与源文件相同的文件名
 	 * @param options {@link StandardCopyOption}
 	 * @return Path
 	 * @throws IORuntimeException IO异常
 	 * @since 5.5.1
 	 */
 	public static Path copy(Path src, Path target, CopyOption... options) throws IORuntimeException {
-		if(isFile(src, false)){
+		if (isFile(src, false)) {
 			return copyFile(src, target, options);
 		}
 		return copyContent(src, target.resolve(src.getFileName()), options);
@@ -208,7 +208,7 @@ public class PathUtil {
 	 * 拷贝目录下的所有文件或目录到目标目录中
 	 *
 	 * @param src     源文件路径,如果为目录只在目标中创建新目录
-	 * @param target    目标文件或目录,如果为目录使用与源文件相同的文件名
+	 * @param target  目标文件或目录,如果为目录使用与源文件相同的文件名
 	 * @param options {@link StandardCopyOption}
 	 * @return Path
 	 * @throws IORuntimeException IO异常
@@ -227,7 +227,7 @@ public class PathUtil {
 	 * 判断是否为目录,如果file为null,则返回false<br>
 	 * 此方法不会追踪到软链对应的真实地址,即软链被当作文件
 	 *
-	 * @param path          {@link Path}
+	 * @param path {@link Path}
 	 * @return 如果为目录true
 	 * @since 5.5.1
 	 */
@@ -487,4 +487,17 @@ public class PathUtil {
 	public static boolean isSymlink(Path path) {
 		return Files.isSymbolicLink(path);
 	}
+
+	/**
+	 * 判断文件或目录是否存在
+	 *
+	 * @param path          文件
+	 * @param isFollowLinks 是否跟踪软链(快捷方式)
+	 * @return 是否存在
+	 * @since 5.5.3
+	 */
+	public static boolean exists(Path path, boolean isFollowLinks) {
+		final LinkOption[] options = isFollowLinks ? new LinkOption[0] : new LinkOption[]{LinkOption.NOFOLLOW_LINKS};
+		return Files.exists(path, options);
+	}
 }

+ 68 - 8
hutool-poi/src/main/java/cn/hutool/poi/ofd/OfdWriter.java

@@ -1,13 +1,19 @@
 package cn.hutool.poi.ofd;
 
+import cn.hutool.core.io.IORuntimeException;
 import cn.hutool.core.io.IoUtil;
+import cn.hutool.core.io.file.PathUtil;
 import org.ofdrw.font.Font;
 import org.ofdrw.layout.OFDDoc;
+import org.ofdrw.layout.edit.Annotation;
 import org.ofdrw.layout.element.Div;
+import org.ofdrw.layout.element.Img;
 import org.ofdrw.layout.element.Paragraph;
+import org.ofdrw.reader.OFDReader;
 
 import java.io.Closeable;
 import java.io.File;
+import java.io.IOException;
 import java.io.OutputStream;
 import java.io.Serializable;
 import java.nio.file.Path;
@@ -28,7 +34,7 @@ public class OfdWriter implements Serializable, Closeable {
 	 *
 	 * @param file 生成的文件
 	 */
-	public OfdWriter(File file){
+	public OfdWriter(File file) {
 		this(file.toPath());
 	}
 
@@ -37,8 +43,16 @@ public class OfdWriter implements Serializable, Closeable {
 	 *
 	 * @param file 生成的文件
 	 */
-	public OfdWriter(Path file){
-		this.doc = new OFDDoc(file);
+	public OfdWriter(Path file) {
+		try {
+			if(PathUtil.exists(file, true)){
+				this.doc = new OFDDoc(new OFDReader(file), file);
+			} else{
+				this.doc = new OFDDoc(file);
+			}
+		} catch (IOException e) {
+			throw new IORuntimeException(e);
+		}
 	}
 
 	/**
@@ -46,20 +60,20 @@ public class OfdWriter implements Serializable, Closeable {
 	 *
 	 * @param out 需要输出的流
 	 */
-	public OfdWriter(OutputStream out){
+	public OfdWriter(OutputStream out) {
 		this.doc = new OFDDoc(out);
 	}
 
 	/**
 	 * 增加文本内容
 	 *
-	 * @param font 字体
+	 * @param font  字体
 	 * @param texts 文本
 	 * @return this
 	 */
-	public OfdWriter addText(Font font, String... texts){
+	public OfdWriter addText(Font font, String... texts) {
 		final Paragraph paragraph = new Paragraph();
-		if(null != font){
+		if (null != font) {
 			paragraph.setDefaultFont(font);
 		}
 		for (String text : texts) {
@@ -69,15 +83,61 @@ public class OfdWriter implements Serializable, Closeable {
 	}
 
 	/**
+	 * 追加图片
+	 *
+	 * @param picFile 图片文件
+	 * @param width   宽度
+	 * @param height  高度
+	 * @return this
+	 */
+	public OfdWriter addPicture(File picFile, int width, int height) {
+		return addPicture(picFile.toPath(), width, height);
+	}
+
+	/**
+	 * 追加图片
+	 *
+	 * @param picFile 图片文件
+	 * @param width   宽度
+	 * @param height  高度
+	 * @return this
+	 */
+	public OfdWriter addPicture(Path picFile, int width, int height) {
+		final Img img;
+		try {
+			img = new Img(width, height, picFile);
+		} catch (IOException e) {
+			throw new IORuntimeException(e);
+		}
+		return add(img);
+	}
+
+	/**
 	 * 增加节点,
+	 *
 	 * @param div 节点,可以是段落、Canvas、Img或者填充
 	 * @return this
 	 */
-	public OfdWriter add(Div div){
+	public OfdWriter add(Div div) {
 		this.doc.add(div);
 		return this;
 	}
 
+	/**
+	 * 增加节点,
+	 *
+	 * @param annotation 节点,可以是段落、Canvas、Img或者填充
+	 * @return this
+	 */
+	public OfdWriter add(int page, Annotation annotation) {
+		try {
+			this.doc.addAnnotation(page, annotation);
+		} catch (IOException e) {
+			throw new IORuntimeException(e);
+		}
+		return this;
+	}
+
 	@Override
 	public void close() {
 		IoUtil.close(this.doc);