iText Java - add header to an existing pdf

為{幸葍}努か 提交于 2019-12-08 06:02:20

问题


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

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

}

I tried fix my problem using

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

insthead of the simple

pdfContentByte.add(page);

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).

How can I achieve my goal?


回答1:


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

}


来源:https://stackoverflow.com/questions/21824468/itext-java-add-header-to-an-existing-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!