调整pdf大小并保留原始元数据

时间:2017-03-02 05:14:29

标签: java pdf resize itext

我正在使用以下代码将PDF的大小调整为width = 553和 高度= 714:

[...]
public static void main(String[] args) {
    try {
        if (args.length < 2) {
            System.out.println("Pdf resizer");
            System.out.println("Args: pdf_in pdf_out");
            System.exit(2);
        }
        float Width = 553;
        float Height = 714;
        PdfReader reader = new PdfReader(args[0]);
        Document doc = new Document(new Rectangle(Width, Height));
        PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(args[1]));
        doc.open();
        PdfContentByte cb = writer.getDirectContent();

        PdfImportedPage tp;
        int pageNumber;
        int NumberOfPages = reader.getNumberOfPages();
        float Width2, Height2, WidthFactor, HeightFactor, factor;
        float offsetX, offsetY;

        for (pageNumber = 1; pageNumber <= NumberOfPages; pageNumber++) {
            tp = writer.getImportedPage(reader, pageNumber);
            Width2 = reader.getPageSize(pageNumber).getWidth();
            Height2 = reader.getPageSize(pageNumber).getHeight();
            WidthFactor = Width / Width2;
            HeightFactor = Height / Height2;
            factor = Math.min(WidthFactor, HeightFactor);
            offsetX = (Width - (Width2 * factor)) / 2;
            offsetY = (Height - (Height2 * factor)) / 2;
            cb.addTemplate(tp, factor, 0, 0, factor, offsetX, offsetY);
            doc.newPage();
        }
        doc.close();
        System.exit(0);
    } catch (Exception e) {
        e.printStackTrace(System.out);
        System.exit(1);
    }
}

问题是元数据丢失了。我怎么能保留原件 调整大小的PDF上的元数据?

非常感谢,

塞萨尔

0 个答案:

没有答案