pdf、ofd、png合并pdf文件
pdf、ofd、png转pdf
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.URLUtil;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import org.ofdrw.converter.ConvertHelper;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MergePdfUtils {
public static void main(String[] args) {
List<String> allNetFiles = Arrays.asList(
"https://aaa.com/xxx/a.pdf",
"https://aaa.com/xxx/b.png",
"https://aaa.com/xxx/b.ofd"
);
ByteArrayOutputStream output = mergePdfByNetFiles(allNetFiles);
}
public static ByteArrayOutputStream mergePdfByNetFiles(List<String> allNetFiles) {
Document document = new Document(PageSize.A5);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
PdfCopy copy = new PdfCopy(document, outputStream);
document.open();
ArrayList<PdfReader> readerList = new ArrayList<PdfReader>();
for (String file : allNetFiles) {
if (file.contains(".pdf")) {
PdfReader reader = new PdfReader(URLUtil.url(file));
readerList.add(reader);
} else if (file.contains(".ofd")) {
InputStream ins = URLUtil.getStream(URLUtil.url(file));
Document documentOfd = new Document(PageSize.A5);
ByteArrayOutputStream fos = new ByteArrayOutputStream();
//输出方式为流
PdfWriter pdfWriter = PdfWriter.getInstance(documentOfd, fos);
pdfWriter.setViewerPreferences(PdfWriter.PageModeUseThumbs);
ConvertHelper.toPdf(ins, fos);//ofd转pdf
PdfReader reader = new PdfReader(fos.toByteArray());
readerList.add(reader);
IoUtil.close(fos);
IoUtil.close(ins);
} else if (file.contains(".png")) {//A5 大小尺寸需要自己设定
PdfReader reader = new PdfReader(createPdfBytesByPngUrl(file));
readerList.add(reader);
}
}
for (PdfReader reader : readerList) {
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
copy.addPage(copy.getImportedPage(reader, i));
}
reader.close();
}
copy.close();
document.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
return outputStream;
}
public static byte[] createPdfBytesByPngUrl(String pngUrl) {
Rectangle pageSize = new Rectangle(PageSize.A5.getHeight(), PageSize.A5.getWidth());
pageSize.rotate();
Document document = new Document(pageSize);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
//输出方式为流
PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
pdfWriter.setViewerPreferences(PdfWriter.PageModeUseThumbs);
Image image = Image.getInstance(URLUtil.url(pngUrl));
document.open();
// 图片居中
image.setAlignment(Image.ALIGN_CENTER);
image.scaleAbsolute(PageSize.A5.getHeight(), PageSize.A5.getWidth());
document.add(image);
document.close();
return outputStream.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭文档
try {
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}