pdf、ofd、png合并pdf文件

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;
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值