使用java代码对pdf进行切割

使用java代码对pdf进行切割

起因,pdf下载的太大了,无法上传有道云笔记,切割成上下两部分

代码

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;

public class PDF {
	public static void main(String[] args) {
		partitionPdfFile("D:/Java 8编程官方参考教程(第9版).pdf","D:/Java 8编程官方参考教程(第9版).pdf)下.pdf", 700,1281);
	}
	
	/**
	 * 截取pdfFile的第from页至第end页,组成一个新的文件名
	 * @param pdfFile
	 * @param subfileName
	 * @param from
	 * @param end
	 */
	public static void partitionPdfFile(String pdfFile,
			String newFile, int from, int end) {
		Document document = null;
		PdfCopy copy = null;		
		try {
			PdfReader reader = new PdfReader(pdfFile);			
			int n = reader.getNumberOfPages();			
			if(end==0){
				end = n;
			}
			ArrayList<String> savepaths = new ArrayList<String>();
			String staticpath = pdfFile.substring(0, pdfFile.lastIndexOf("\\")+1);
			String savepath = staticpath+ newFile;
			savepaths.add(savepath);
			document = new Document(reader.getPageSize(1));
			copy = new PdfCopy(document, new FileOutputStream(savepaths.get(0)));
			document.open();
			for(int j=from; j<=end; j++) {
				document.newPage(); 
				PdfImportedPage page = copy.getImportedPage(reader, j);
				copy.addPage(page);
			}
			document.close();

		} catch (IOException e) {
			e.printStackTrace();
		} catch(DocumentException e) {
			e.printStackTrace();
		}
	}
}

所需jar包

bcprov-jdk15-141.jar
iText-2.1.4.jar
itext-2.0.2.jar

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用开源的 PDFBox 库来进行 PDF 文件的处理,其中就包括了 PDF 文件的切割。 具体实现步骤如下: 1. 读取 PDF 文件,获取每一页的内容和坐标信息; 2. 根据指定的 y 坐标,确定需要切割的位置; 3. 遍历每一页的坐标信息,将在切割位置上方的内容放入一个新的 PDF 文件中,并将其保存。 下面是一个简单的 Java 代码示例,实现了根据 y 坐标切割 PDF 文件的功能: ```java import java.io.File; import java.io.IOException; import java.util.List; import org.apache.pdfbox.cos.COSArray; import org.apache.pdfbox.cos.COSBase; import org.apache.pdfbox.cos.COSNumber; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.text.PDFTextStripperByArea; public class PDFCutter { public static void main(String[] args) throws IOException { // 读取 PDF 文件 PDDocument document = PDDocument.load(new File("example.pdf")); // 指定切割位置的 y 坐标 float y = 500; // 创建一个新的 PDF 文件 PDDocument newDocument = new PDDocument(); // 遍历每一页 for (PDPage page : document.getPages()) { // 获取页面的大小 PDRectangle mediaBox = page.getMediaBox(); // 获取页面内容 PDFTextStripperByArea stripper = new PDFTextStripperByArea(); stripper.setSortByPosition(true); stripper.addRegion("content", new PDRectangle(mediaBox.getLowerLeftX(), 0, mediaBox.getWidth(), mediaBox.getHeight())); stripper.extractRegions(page); // 获取页面内容的坐标信息 List<PDFTextStripperByArea.TextChunk> textChunks = stripper.getTextChunks(); for (PDFTextStripperByArea.TextChunk textChunk : textChunks) { COSArray array = (COSArray) textChunk.getTextMatrix().getCOSObject(); COSBase baseY = array.getObject(5); if (baseY instanceof COSNumber) { float chunkY = ((COSNumber) baseY).floatValue(); if (chunkY >= y) { // 将在切割位置上方的内容放入新的 PDF 文件中 PDPage newPage = new PDPage(); newPage.setMediaBox(mediaBox); newPage.setCropBox(mediaBox); newDocument.addPage(newPage); newPage.getContentStream().appendRawCommands(page.getContents(), 0, textChunk.getEnd()); break; } } } } // 保存新的 PDF 文件 newDocument.save("example_cut.pdf"); // 关闭文件 document.close(); newDocument.close(); } } ``` 需要注意的是,上述代码中仅实现了在切割位置上方的内容放入新的 PDF 文件中,如果需要将切割位置下方的内容也保留,可以参考代码进行修改。此外,PDF 文件的切割还有其他方法,可以根据具体需求选择适合的方法。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值