pdf拆分工具类

目录

 描述:

 测试代码:

 工具类:


 

 描述:

        将本地pdf文件按找页码拆分为多个jpg文件,并保存到本地文件

 测试代码:

 @Test
    public void testString() {

        try {
            byte[] pdfbytes = DownloadFileUtil.getBytes("D:\\jfapp\\file\\pdf\\temp.pdf");
            DownloadFileUtil.getFile(pdfbytes, "D:\\jfapp\\file\\pdf\\", "tempppp.pdf");
            DownloadFileUtil.pdfToImages("D:\\jfapp\\file\\pdf\\tempppp.pdf", "D:\\jfapp\\file\\pdf\\");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

    }

 工具类:

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpStatus;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.jfsoft.interfaces.entity.AdiconResultImg;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 下载工具类
 *
 * @author JAVA
 * @date 2020/12/18.
 */
@Slf4j
public class DownloadFileUtil {

    // Base64 编码与解码
    private static final java.util.Base64.Decoder DECODER_64 = java.util.Base64.getDecoder();
    private static final java.util.Base64.Encoder ENCODER_64 = java.util.Base64.getEncoder();


    /**
     * 文件下载
     *
     * @param path     文件绝对路径
     * @param fileName 文件名
     * @return
     * @throws Exception
     */
    public static ResponseEntity download(String path, String fileName) {
        try {
            File file = new File(path);
            if (!FileUtil.exist(path)) {
                return ResponseEntity.status(HttpStatus.HTTP_NOT_FOUND).build();
            }
            HttpHeaders headers = new HttpHeaders();
            headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
            headers.add("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
            headers.add("Pragma", "no-cache");
            headers.add("Expires", "0");
            headers.add("Last-Modified", new Date().toString());
            headers.add("ETag", String.valueOf(System.currentTimeMillis()));
            return ResponseEntity
                    .ok()
                    .headers(headers)
                    .contentLength(file.length())
                    .contentType(MediaType.parseMediaType("application/octet-stream"))
                    .body(new FileSystemResource(file));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.HTTP_INTERNAL_ERROR).build();
        }
    }

    /**
     * 文件下载
     *
     * @param base64Str base64字符串
     * @param fileName  文件名
     * @return
     * @throws Exception
     */
    public static ResponseEntity downloadBase64(String base64Str, String fileName) {
        ResponseEntity<InputStreamResource> body = null;
        InputStream is = null;
        try {
            byte[] bytes = new Base64().decode(base64Str);
            is = new ByteArrayInputStream(bytes);
            HttpHeaders headers = new HttpHeaders();
            headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
            headers.add("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            headers.add("Pragma", "no-cache");
            headers.add("Expires", "0");

            body = ResponseEntity
                    .ok()
                    .headers(headers)
                    .contentLength(is.available())
                    .contentType(MediaType.parseMediaType("application/octet-stream"))
                    .body(new InputStreamResource(is));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != is) {
                    is.close();
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        return body;
    }

    public static void base64ContentToFile(String base64Content, String filePath) throws IOException {
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            // Base64解码到字符数组
            byte[] bytes = DECODER_64.decode(base64Content);
            ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
            bis = new BufferedInputStream(byteInputStream);
            File file = new File(filePath);
            File path = file.getParentFile();
            if (!path.exists()) {
                path.mkdirs();
            }
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            // io
            byte[] buffer = new byte[1024];
            int length = bis.read(buffer);
            while (length != -1) {
                bos.write(buffer, 0, length);
                length = bis.read(buffer);
            }
            // 刷新此输出流,强制写出所有缓冲的输出字节
            bos.flush();
        } catch (IOException e) {
            e.getMessage();
        } finally {
            try {
                bis.close();
                fos.close();
                bos.close();
            } catch (IOException e) {
                e.getMessage();
            }
        }
    }

    public static byte[] byte2Hex(byte[] bt) {
        String jpg_base64 = null;
        try {
            PDDocument pdf = PDDocument.load(bt);
            int size = pdf.getNumberOfPages();
            //定义宽度
            int width = 0;
            //保存一张图片中的RGB数据
            int[] singleImagRGB;
            //定义高度,后买你用于叠加
            int shiftHeight = 0;
            //保存每张图片的像素值
            BufferedImage imageResult = null;
            //利用pdfBOX生成图像
            PDDocument pdDocument = pdf;
            PDFRenderer pdfRenderer = new PDFRenderer(pdDocument);
            int pageLength = size;
            //总计循环次数
            int totalCount = pdDocument.getNumberOfPages() / pageLength + 1;
            for (int m = 0; m < totalCount; m++) {
                for (int x = 0; x < pageLength; x++) {
                    int pageIndex = x + (m * pageLength);
                    if (pageIndex == pdDocument.getNumberOfPages()) {
                        //System.out.println("循环次数 m = " + m);
                        break;
                    }
                    // dpi为图片的dpi,dpi越大,则图片越清晰,图片越大,转换耗费的时间也越多
                    BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, 86f, ImageType.RGB);
                    int imageHeight = image.getHeight();
                    int imageWidth = image.getWidth();
                    if (x == 0) {
                        //计算高度和偏移量
                        //使用第一张的宽度
                        width = imageWidth;
                        if ((pdDocument.getNumberOfPages() - m * pageLength) < pageLength) {
                            imageResult = new BufferedImage(width, imageHeight * (pdDocument.getNumberOfPages() - m * pageLength), BufferedImage.TYPE_INT_RGB);
                        } else {
                            imageResult = new BufferedImage(width, imageHeight * pageLength, BufferedImage.TYPE_INT_RGB);
                        }
                    } else {
                        // 将高度不断累加
                        shiftHeight += imageHeight;
                    }
                    singleImagRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);
                    imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImagRGB, 0, width);
                }
                shiftHeight = 0;
            }
            pdDocument.close();
            ByteArrayOutputStream baos;//io流
            baos = new ByteArrayOutputStream();
            ImageIO.write(imageResult, "jpg", baos);//写入流中
            byte[] jpg_Bytes = baos.toByteArray();//转换成字节
//        BASE64Encoder encoder = new BASE64Encoder();
//        jpg_base64 = encoder.encodeBuffer(jpg_Bytes).trim();//转换成base64串
//        jpg_base64 = jpg_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
//            log.info("图片base64"+jpg_base64);
//        baos.close();
//        pdf.close();
//        String photoShiLiu = toShiliu(jpg_base64);
//        System.out.println(photoShiLiu);
            return jpg_Bytes;
        } catch (IOException exp) {
            exp.printStackTrace();
            System.out.println("转失败了!");
        }
        return null;
    }

    /**
     * 获得指定文件的byte数组
     **/
    public static byte[] getBytes(String filePath) {
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }


    /**
     * 根据byte数组,生成文件
     **/
    public static void getFile(byte[] bfile, String filePath, String fileName) {
        BufferedOutputStream bos = null;
        FileOutputStream fos = null;
        File file = null;
        try {
            File dir = new File(filePath);
            if (!dir.exists() && dir.isDirectory()) {//判断文件目录是否存在
                dir.mkdirs();
            }
            file = new File(filePath + fileName);
            fos = new FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bfile);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        log.info("filePath   >>>" + file.getPath());
    }


    /**
     *
     * @param pdfFilePath : pdf文件地址
     * @param destDirFile : 最终jpg图片生成保存的目录
     * @return java.util.List<com.jfsoft.interfaces.entity.AdiconResultImg>
     * @描述: pdf 拆分转为jpg 图片  并返回
     * @Date: 2023/6/12 11:49
     **/
    public static void pdfToImages( String pdfFilePath, String destDirFile) throws Exception {

        final String destFormat = "jpg";//最终希望PDF文件转成的文件格式
        final File file = new File(pdfFilePath);

        PDDocument doc = null;
        try {
            doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pages = doc.getNumberOfPages();//获取到当前PDF内的总页数
            for (int i = 0; i < pages; i++) { //遍历每一页

                //拼接生成的图片路径->D:/test/月份/月份(PDF后缀)-页数.jpg
                int act = i + 1;//实际计数比计算机计数多一,因为计算机是从0计数
                String destPath = destDirFile + file.getName() + "_" + act + "." + destFormat;
                //BufferedImage image = renderer.renderImage(i);
                BufferedImage image = renderer.renderImageWithDPI(i, 200f, ImageType.RGB);
                File filepath = new File(destPath);
                if (!filepath.exists()) {
                    filepath.mkdirs();//生成文件夹来存储输出的图片
                }
                File jpgFile = new File(destPath);//在此路劲内生成图片
                ImageIO.write(image, destFormat, jpgFile);//传入参数,生成图片
            }
        } finally {
            if (doc != null) {
                doc.close();
            }
        }

    }


    /**
     * @param path       源PDF路径
     * @param fileName   源PDF文件名
     * @param outputPath 拆分后输出的PDF路径
     * @author Reverse_XML
     * 把PDF 按页(逐页、单页)拆分(一页一页拆分,一页为一个新PDF文件)
     */
    public static void splitPDFOneByOne(String path, String fileName, String outputPath) {
        String sep = File.separator;
        PdfReader reader = null;
        int numberOfPages = 0;
        try {
            reader = new PdfReader(path + sep + fileName);
            //reader = new PdfReader();
            numberOfPages = reader.getNumberOfPages();
            for (int i = 1; i <= numberOfPages; i++) {
                Document document = null;
                PdfCopy copy = null;
                try {
                    document = new Document(reader.getPageSize(1));
                    String savePath = outputPath + sep +
                            fileName.substring(0, fileName.lastIndexOf(".")) + "_" + i + ".pdf";
                    copy = new PdfCopy(document, new FileOutputStream(savePath));
                    document.open();
                    document.newPage();
                    PdfImportedPage page = copy.getImportedPage(reader, i);
                    copy.addPage(page);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (document != null)
                        document.close();
                    if (copy != null)
                        copy.close();
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            if (reader != null)
                reader.close();
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Xamarin是一种跨平台移动应用开发框架,可用于开发iOS、Android和Windows手机应用程序。开发人员可以使用C#语言和.Net框架来编写应用程序,并可以共享大部分代码来构建跨平台的移动应用。 要在Xamarin中开发PDF功能,可以使用现有的PDF库来处理和生成PDF文档。一种常用的库是iTextSharp,它是iText PDF库的一个C#端口。iTextSharp可以用于创建、编辑和处理PDF文档。开发人员可以使用iTextSharp的API来实现在Xamarin中生成PDF和处理PDF的功能。 首先,开发人员需要将iTextSharp库添加到Xamarin应用程序的项目中。然后,可以使用iTextSharp的类和方法来创建PDF文档、添加文本、图像、表格等内容,并对PDF进行格式化和编辑。 例如,开发人员可以使用iTextSharp的PdfWriter类来创建一个新的PDF文档,然后使用其Document类来添加内容和格式化页面布局。可以使用PdfPTable和PdfPCell类来创建表格,使用Paragraph和Chunk类来添加文本内容,使用Image类来添加图像等。 在生成PDF文档的过程中,开发人员还可以使用iTextSharp的其他功能,如添加书签、目录、页眉页脚等。还可以设置PDF文档的加密、权限和元数据。 总的来说,使用Xamarin和iTextSharp库可以在移动应用中方便地开发PDF功能。开发人员可以利用Xamarin的跨平台能力和C#语言的简洁性,并充分利用iTextSharp库的功能来生成和处理PDF文档。这种结合可以提供丰富的PDF功能,以满足不同的应用需求。 ### 回答2: Xamarin是一个跨平台的移动应用开发框架,可以使用C#语言进行开发,并支持使用共享代码库在多个平台上构建原生应用程序。在Xamarin中,我们可以使用一些库或插件来实现PDF文件的操作和处理。 要在Xamarin中开发PDF,我们可以使用一些第三方库,如iTextSharp或PDFSharp。这些库提供了一组API和功能,可以让我们创建、操纵和呈现PDF文件。 首先,我们需要导入适用于Xamarin的iTextSharp或PDFSharp库。然后,我们可以使用这些库来创建PDF文档,添加文本、图像、表格和其他元素,设置样式和格式等。 要创建一个PDF文档,我们可以实例化一个Document对象,并设置其属性,例如页面尺寸、边距等。然后,我们可以使用这个文档对象添加内容,例如添加文本或图像。 对于文本内容,我们可以使用Paragraph对象来创建段落,然后将其添加到文档中。我们可以设置字体、字号和对齐方式等样式。 对于图像内容,我们可以使用Image对象来加载图像文件,并将其添加到文档中的指定位置。 除了添加内容,我们还可以使用这些库来处理PDF文件,例如合并多个PDF文件、拆分PDF文件、提取特定页面或文本等。 因此,使用Xamarin开发PDF可以通过使用iTextSharp或PDFSharp等第三方库来实现。我们可以利用这些库提供的API和功能来创建、操纵和呈现PDF文件,实现我们所需的功能和需求。 ### 回答3: Xamarin是一款跨平台移动应用开发框架,可以用于开发iOS、Android和Windows Phone等多个平台的移动应用程序。在Xamarin开发中,我们可以使用C#语言和.NET框架来开发应用。 要在Xamarin中开发PDF,我们可以利用现有的PDF库,例如iTextSharp或Syncfusion等,这些库是用于处理PDF文档的强大工具。 首先,我们需要将PDF库添加到Xamarin项目中。我们可以使用NuGet包管理器来安装所需的库。安装后,我们可以使用C#语言和.NET框架的功能来读取、编辑和创建PDF文档。 在Xamarin中,我们可以使用PDF库提供的API来进行各种操作,如创建新的PDF文档、添加文本、图像或表格、设置页面布局、添加书签和超链接等。 例如,如果我们想要在应用中显示一个PDF文档,我们可以使用PDF库的API加载该文档,并将其显示在应用的界面上。我们还可以为用户提供一些功能,如在PDF文档中搜索关键字、放大或缩小页面、旋转页面等。 另外,我们还可以使用PDF库的API来生成PDF报表或合并多个PDF文档。这对于一些需要生成和处理PDF文档的应用程序非常有用,如电子商务应用中的订单发票、金融应用中的账单报表等。 总之,Xamarin提供了一个强大的开发框架,可以用于开发跨平台的移动应用。通过使用合适的PDF库,我们可以在Xamarin中进行PDF开发,并实现各种功能,如读取、编辑、创建和显示PDF文档等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值