add method

This commit is contained in:
Looly 2020-12-14 00:46:48 +08:00
parent 59d155e00f
commit 54fdf60bc3
2 changed files with 88 additions and 15 deletions

View File

@ -487,4 +487,17 @@ public class PathUtil {
public static boolean isSymlink(Path path) { public static boolean isSymlink(Path path) {
return Files.isSymbolicLink(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);
}
} }

View File

@ -1,13 +1,19 @@
package cn.hutool.poi.ofd; package cn.hutool.poi.ofd;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.PathUtil;
import org.ofdrw.font.Font; import org.ofdrw.font.Font;
import org.ofdrw.layout.OFDDoc; import org.ofdrw.layout.OFDDoc;
import org.ofdrw.layout.edit.Annotation;
import org.ofdrw.layout.element.Div; import org.ofdrw.layout.element.Div;
import org.ofdrw.layout.element.Img;
import org.ofdrw.layout.element.Paragraph; import org.ofdrw.layout.element.Paragraph;
import org.ofdrw.reader.OFDReader;
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.Serializable; import java.io.Serializable;
import java.nio.file.Path; import java.nio.file.Path;
@ -38,8 +44,16 @@ public class OfdWriter implements Serializable, Closeable {
* @param file 生成的文件 * @param file 生成的文件
*/ */
public OfdWriter(Path 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); this.doc = new OFDDoc(file);
} }
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
/** /**
* 构造 * 构造
@ -68,8 +82,39 @@ public class OfdWriter implements Serializable, Closeable {
return add(paragraph); return add(paragraph);
} }
/**
* 追加图片
*
* @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 节点可以是段落CanvasImg或者填充 * @param div 节点可以是段落CanvasImg或者填充
* @return this * @return this
*/ */
@ -78,6 +123,21 @@ public class OfdWriter implements Serializable, Closeable {
return this; return this;
} }
/**
* 增加节点
*
* @param annotation 节点可以是段落CanvasImg或者填充
* @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 @Override
public void close() { public void close() {
IoUtil.close(this.doc); IoUtil.close(this.doc);