gpt4 book ai didi

java - itext 将 PDF 分割成多个 PDF,但大小相同

转载 作者:行者123 更新时间:2023-12-01 18:47:51 28 4
gpt4 key购买 nike

这是我将单个 PDF 拆分为按页面拆分的多个 PDF 的代码:

public static String splitAndRenamePdf(InputStream file, String targetDir) {
try {
PdfReader reader = new PdfReader(file);
int n = reader.getNumberOfPages();
for (int i=1; i <= n; i++) {
Document document = new Document(reader.getPageSizeWithRotation(i)); //I tried with 1 too
PdfCopy writer = new PdfCopy(document, new FileOutputStream(targetDir+File.separatorChar+i+".pdf"));
document.open();
PdfImportedPage page = writer.getImportedPage(reader, i);
writer.addPage(page);
document.close();
writer.close();
}
return "from 01 to "+n;
} catch (IOException | DocumentException exc) {
System.out.println("splitAndRenamePdf Exception: "+exc.getMessage());
return null;
}
}

内容是正确的,但生成的 n 个文件的大小与原始文件的大小相同。

有人可以帮助我吗?我可以更改库,因为我不再使用 iText。

最佳答案

我写解决方案...我希望它可以帮助别人。

private final static RenderListener nopListener = new RenderListener() {
@Override
public void renderText(TextRenderInfo renderInfo) { }

@Override
public void renderImage(ImageRenderInfo renderInfo) { }

@Override
public void endTextBlock() { }

@Override
public void beginTextBlock() { }
};

static class Do implements ContentOperator {
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
PdfName xobjectName = (PdfName)operands.get(0);
names.add(xobjectName);
}

final List<PdfName> names = new ArrayList<>();
}

private static void fixPdfReader(PdfReader reader) throws IOException {
PdfContentStreamProcessor processor = new PdfContentStreamProcessor(nopListener);
Do doOp = new Do();
processor.registerContentOperator("Do", doOp);
int totPages = reader.getNumberOfPages();
for (int page = 1; page <= totPages; page++) {
PdfDictionary resources = reader.getPageResources(page);
if (resources == null) {
System.out.printf("!!! page %d has no resources\n", page);
continue;
}
doOp.names.clear();
processor.processContent(ContentByteUtils.getContentBytesForPage(reader, page), resources);
PdfDictionary newResources = new PdfDictionary();
newResources.putAll(resources);
PdfDictionary xobjects = newResources.getAsDict(PdfName.XOBJECT);
PdfDictionary newXobjects = new PdfDictionary();
for (PdfName key: doOp.names) {
newXobjects.put(key, xobjects.get(key));
}
newResources.put(PdfName.XOBJECT, newXobjects);
reader.getPageN(page).put(PdfName.RESOURCES, newResources);
}
reader.removeUnusedObjects();
}

public static String fixAndSplitPDF(InputStream inputStream, String targetDir) {
try {
PdfReader reader = new PdfReader(inputStream);
fixPdfReader(reader);
//this method is in the question!
return splitAndRenamePdf(reader, targetDir);
} catch (IOException exc) {
//LOG Exception...
return null;
}
}

关于java - itext 将 PDF 分割成多个 PDF,但大小相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59794056/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com