我如何使用iText在A4的一半上使用美化PDF,在A4上使用完整尺寸

时间:2016-02-12 23:42:58

标签: itext

我在A4页面的上半部分有一个美化形式,我希望它可以旋转并放大到纵向布局大小,填充A4然后在传真之前保存。否则,传真服务程序将仅使用部分信息将其传真出去。这是我的尝试,结果与输入pdf相同。这是我使用iText编程的第一天,所有谷歌都没有得到我想要的东西。如果你能提供帮助,请告诉我。谢谢,

public class CopeALandscapePdfFiletoPortraitPdfFile {
//public static final String SRC = "resources/pdfs/landscapeForm.pdf";
public static final String SRC = "resources/pdfs/potraitForm.pdf";
public static final String DEST = "results/stamper/portraitFormAfterCopy.pdf";

public static void main(String[] args) throws IOException, DocumentException
{   
    copyPdf();
}

private static void copyPdf() throws IOException, DocumentException
{
    Document document = new Document(PageSize.A4);

    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
    document.open();
    PdfContentByte cb = writer.getDirectContent();

    PdfReader reader = new PdfReader(SRC);
    document.newPage();
    int n = reader.getNumberOfPages();
    PdfDictionary page;
    PdfNumber rotate;
    for (int p = 1; p <= n; p++) {
        page = reader.getPageN(p);
        rotate = page.getAsNumber(PdfName.ROTATE);

        if (rotate == null) {
            page.put(PdfName.ROTATE, new PdfNumber(90));
        } else {
            page.put(PdfName.ROTATE, new PdfNumber((rotate.intValue() + 90) % 360));
        }

    } 
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(DEST));
    stamper.close();

    PdfImportedPage ipage = writer.getImportedPage(stamper.getReader(), 1);
    cb.addTemplate(ipage, 0, 0);

    document.close();
}

}

1 个答案:

答案 0 :(得分:0)

正如你想要放大PDF一样,我会把放大和旋转放到一个afine转换中。因此:

PdfReader reader = new PdfReader(SOURCE);
Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, RESULT);
document.open();

double sqrt2 = Math.sqrt(2);
Rectangle pageSize = reader.getPageSize(1);
PdfImportedPage importedPage = writer.getImportedPage(reader, 1);
writer.getDirectContent().addTemplate(importedPage, 0, sqrt2, -sqrt2, 0, pageSize.getTop() * sqrt2, -pageSize.getLeft() * sqrt2);

document.close();

EnlargePagePart.java

E.g。对于这个页面

original page

它会生成

zoomed, rotated upper half