itext垂直拆分PDF [英] itext Split PDF Vertically
问题描述
我有要求我必须在中心垂直分割PDF页面。
我通过各种帖子进行搜索,无法确定正确的方法
I have requirement where I have to split a PDF page right at center vertically. I searched through various posts and could not identify the right way to do it
我想使用Java来使用iText库。
I want to use iText Library using Java.
我使用了来自
iText:将PDF分成几个PDF(每页1个)
并修改如下,但页面没有拆分,但它复制整个页面。
and modified it like below, but page not getting split but it copies entire page.
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
public class SplitPDFFile {
/**
* @param args
*/
public static void main(String[] args) {
try {
String inFile = "C:/input.pdf";
System.out.println ("Reading " + inFile);
PdfReader reader = new PdfReader(inFile);
Rectangle cropBox = reader.getCropBox(1);
Rectangle psize = reader.getPageSize(1);
cropBox.setRight(psize.getWidth()/2);
System.out.println(psize.getWidth());
System.out.println(psize.getHeight());
int n = reader.getNumberOfPages();
System.out.println ("Number of pages : " + n);
int i = 0;
while ( i < n ) {
String outFile = inFile.substring(0, inFile.indexOf(".pdf"))
+ "-" + String.format("%03d", i + 1) + ".pdf";
System.out.println ("Writing " + outFile);
Document document = new Document(cropBox);
PdfCopy writer = new PdfCopy(document, new FileOutputStream(outFile));
writer.setCropBoxSize(cropBox);
document.open();
PdfImportedPage page = writer.getImportedPage(reader, ++i);
writer.addPage(page);
document.close();
writer.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
推荐答案
您可以使用 PdfCopy
尝试此方法,间歇性地操作从以下位置复制的 PdfReader
:
You can try this method using PdfCopy
, intermittently manipulating the PdfReader
copied from:
void splitIntoHalfPages(InputStream source, File target) throws IOException, DocumentException
{
final PdfReader reader = new PdfReader(source);
try ( OutputStream targetStream = new FileOutputStream(target) )
{
Document document = new Document();
PdfCopy copy = new PdfCopy(document, targetStream);
document.open();
for (int page = 1; page <= reader.getNumberOfPages(); page++)
{
PdfDictionary pageN = reader.getPageN(page);
Rectangle cropBox = reader.getCropBox(page);
PdfArray leftBox = new PdfArray(new float[]{cropBox.getLeft(), cropBox.getBottom(), (cropBox.getLeft() + cropBox.getRight()) / 2.0f, cropBox.getTop()});
PdfArray rightBox = new PdfArray(new float[]{(cropBox.getLeft() + cropBox.getRight()) / 2.0f, cropBox.getBottom(), cropBox.getRight(), cropBox.getTop()});
PdfImportedPage importedPage = copy.getImportedPage(reader, page);
pageN.put(PdfName.CROPBOX, leftBox);
copy.addPage(importedPage);
pageN.put(PdfName.CROPBOX, rightBox);
copy.addPage(importedPage);
}
document.close();
}
finally
{
reader.close();
}
}
此方法创建包含每个页面两次的源文档的副本,一次将 CropBox 限制为左半页,一次到右边。
This methods creates a copy of the source document containing each page twice, once with the CropBox limited to the left half page, once to the right one.
注意:此方法仅拆分页面内容。如果源PDF包含注释,您可能还需要处理它们。
Beware: This method only splits the page content. If your source PDFs have annotations, you might want to also process them.
这篇关于itext垂直拆分PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!