iText Java - 将标题添加到现有pdf

时间:2014-02-17 08:51:02

标签: java pdf itext jcifs

我想使用iText为现有的pdf添加标题。

我没有问题,除了有时我的函数使用正确的页眉和页脚创建一个pdf,但现有的pdf页面已旋转。

private static void print(Sldocuments item, String header, String footer) {
    try {
        String ftpFilename = item.getId()+"_"+item.getDocumentname();
        String newName= String.valueOf(item.getId())+".pdf";
        String path = (Global.SHARED_FOLDER_DEVELOPER);

        String smbUser = "**;"+"**" + ":" + "**";
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(smbUser);
        SmbFile sFile = new SmbFile(path+ftpFilename, auth);

        InputStream in = sFile.getInputStream();

        PdfReader reader = new PdfReader(in);

        // Create output PDF
        Document document = new Document(PageSize.A4);
        SmbFile sFileOut = new SmbFile(path+newName, auth);
        SmbFileOutputStream sfos = new SmbFileOutputStream(sFileOut);

        PdfWriter writer = PdfWriter.getInstance(document, sfos);
        document.open();
        PdfContentByte pdfContentByte = writer.getDirectContent();

        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            PdfImportedPage page = writer.getImportedPage(reader, i);
            document.newPage();
            pdfContentByte.add(page);
            // Write header
            writeText(headerPositionX, headerPositionY, header);

            // Write footer
            writeText(footerPositionX, footerPositionY, footer);

            // Write page number
            String pageNumber = "pagina "+ i +" di " + reader.getNumberOfPages();
            writeText(pageNumberPositionX, pageNumberPositionY, pageNumber);
        }
        document.close();
        reader.close();
    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

我尝试使用

解决问题
AffineTransform af = new AffineTransform();
af.setToRotation(Math.toRadians(page.getRotation()));
pdfContentByte.addTemplate(page, af);

简单的

的insthead
pdfContentByte.add(page);

但是通过这种转换,导入的页面完全从我的新pdf中丢失(可能是因为我使用错误的锚点旋转页面)。

我如何实现目标?

1 个答案:

答案 0 :(得分:0)

根据mkl,我使用了PdfStamper:

private static void print(Sldocuments item, String header, String footer) {
try {
String ftpFilename = item.getId()+"_"+item.getDocumentname();
    String newName= String.valueOf(item.getId())+".pdf";
    String path = (Global.SHARED_FOLDER_DEVELOPER);

    String smbUser = "**;"+"**" + ":" + "**";
    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(smbUser);
    SmbFile sFile = new SmbFile(path+ftpFilename, auth);

    InputStream in = sFile.getInputStream();

    PdfReader reader = new PdfReader(in);

    // Create output PDF
        SmbFile sFileOut = new SmbFile(path+newName, auth);
        SmbFileOutputStream sfos = new SmbFileOutputStream(sFileOut);

        PdfStamper stamper  = new PdfStamper(reader, sfos);
        // Loop over the pages and add a header to each page
        int n = reader.getNumberOfPages();
        for (int i = 1; i <= n; i++) {
            //add header
            PdfPTable table = new PdfPTable(1);
            table.setTotalWidth(PDF_PAGE_SIZE.getWidth()-(headerPositionX*2));
            table.setLockedWidth(true);
            table.getDefaultCell().setFixedHeight(20);
            table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            table.addCell(header);
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
            table.addCell("");
            table.writeSelectedRows(0, -1, headerPositionX, headerPositionY, stamper.getOverContent(i));

            //add footer
            PdfPTable tableFooter = new PdfPTable(2);
            tableFooter.setTotalWidth(PDF_PAGE_SIZE.getWidth()-(footerPositionX*2));
            tableFooter.setLockedWidth(true);
            tableFooter.getDefaultCell().setFixedHeight(20);
            tableFooter.getDefaultCell().setBorder(Rectangle.NO_BORDER);
            tableFooter.addCell(footer);
            tableFooter.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT);
            tableFooter.addCell(String.format("pagina %d of %d", i, n));
            tableFooter.writeSelectedRows(0, -1, footerPositionX, footerPositionY*4, stamper.getOverContent(i));
        }

        // Close the stamper
        stamper.close();
        reader.close();

    } catch (DocumentException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}