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

@@ -198,7 +198,7 @@ public class PathUtil {
* @since 5.5.1 * @since 5.5.1
*/ */
public static Path copy(Path src, Path target, CopyOption... options) throws IORuntimeException { 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 copyFile(src, target, options);
} }
return copyContent(src, target.resolve(src.getFileName()), options); return copyContent(src, target.resolve(src.getFileName()), options);
@@ -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;
@@ -28,7 +34,7 @@ public class OfdWriter implements Serializable, Closeable {
* *
* @param file 生成的文件 * @param file 生成的文件
*/ */
public OfdWriter(File file){ public OfdWriter(File file) {
this(file.toPath()); this(file.toPath());
} }
@@ -37,16 +43,24 @@ 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);
}
}
/** /**
* 构造 * 构造
* *
* @param out 需要输出的流 * @param out 需要输出的流
*/ */
public OfdWriter(OutputStream out){ public OfdWriter(OutputStream out) {
this.doc = new OFDDoc(out); this.doc = new OFDDoc(out);
} }
@@ -57,9 +71,9 @@ public class OfdWriter implements Serializable, Closeable {
* @param texts 文本 * @param texts 文本
* @return this * @return this
*/ */
public OfdWriter addText(Font font, String... texts){ public OfdWriter addText(Font font, String... texts) {
final Paragraph paragraph = new Paragraph(); final Paragraph paragraph = new Paragraph();
if(null != font){ if (null != font) {
paragraph.setDefaultFont(font); paragraph.setDefaultFont(font);
} }
for (String text : texts) { for (String text : texts) {
@@ -68,16 +82,62 @@ 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 节点可以是段落、Canvas、Img或者填充 * @param div 节点可以是段落、Canvas、Img或者填充
* @return this * @return this
*/ */
public OfdWriter add(Div div){ public OfdWriter add(Div div) {
this.doc.add(div); this.doc.add(div);
return this; 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 @Override
public void close() { public void close() {
IoUtil.close(this.doc); IoUtil.close(this.doc);