使用iText裁剪PDF(java PDF库)

时间:2012-05-08 11:42:17

标签: java pdf itext

我需要使用此code裁剪PDF文件。 cb将输出裁剪所需的坐标。

如何有效地为PDF文件中的每个页面执行此操作?

是否有此代码,但不适用于每个pdf输入:

public class CropPages {

public static final String PREFACE = "input.pdf";
public static final String RESULT = "cropped.pdf";


public void addMarginRectangle(String src, String dest)
    throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfReaderContentParser parser = new PdfReaderContentParser(reader);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
    TextMarginFinder finder;
    PdfDictionary pageDict;
    for (int i = 1; i <= reader.getNumberOfPages(); i++) {
        finder = parser.processContent(i, new TextMarginFinder());
        //PdfContentByte cb = stamper.getOverContent(i);
        PdfRectangle rect = new PdfRectangle((finder.getLlx()-5), (finder.getLly()-5), (finder.getUrx()+5), (finder.getUry()+5));
        if (i <= 10){
                 System.out.println(rect);
                 }
        pageDict = reader.getPageN(i);
        pageDict.put(PdfName.CROPBOX, rect);
     }
    stamper.close();
}

public static void main(String[] args) throws IOException, DocumentException {
    new CropPages().addMarginRectangle(PREFACE, RESULT);
}

}

0 个答案:

没有答案