iText Java - 将标题添加到现有pdf [英] iText Java - add header to an existing pdf

查看:1033
本文介绍了iText Java - 将标题添加到现有pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to add an header to an existing pdf using iText.

除了有时我的函数创建带有正确页眉和页脚的pdf之外我没有问题但是旋转现有的pdf页面。

I had no problem except that sometimes my function create a pdf with the correct header and footer but with the existing pdf page rotated.

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();
    }

}

我尝试使用<修复我的问题/ p>

I tried fix my problem using

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

简单的实例

pdfContentByte.add(page);

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

but with this transformation, the imported page is totally missing from my new pdf (maybe cause I rotate the page using a wrong anchor point).

我如何实现目标?

推荐答案

根据mkl,我使用了PdfStamper:

According to mkl I used the 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();
    }

}

这篇关于iText Java - 将标题添加到现有pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆