1

I am not able to see footer in resultant PDF for below HTML and CSS. This code works fine with IText version 2.

Does the way to show header and footer is changed in IText version 7? or am I missing any configuration required for newer version?

Thanks.

Java code

public void createPdf(String src, String dest, String resources) throws IOException {
try {
        FileOutputStream outputStream = new FileOutputStream(dest);

        WriterProperties writerProperties = new WriterProperties();
        //Add metadata
        writerProperties.addXmpMetadata();

        PdfWriter pdfWriter = new PdfWriter(outputStream, writerProperties);

        PdfDocument pdfDoc = new PdfDocument(pdfWriter);
        pdfDoc.getCatalog().setLang(new PdfString("en-US"));

        // pdf conversion
        ConverterProperties props = new ConverterProperties();

        MediaDeviceDescription mediaDeviceDescription =
            new MediaDeviceDescription(MediaType.PRINT);
        props.setMediaDeviceDescription(mediaDeviceDescription);

        HtmlConverter.convertToPdf(new FileInputStream(src), pdfDoc, props);
        pdfDoc.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Html content

<html>
<head>
    <meta charset="UTF-8"/>

    <style type="text/css" media="print">

        #footer {
            display: block;
            position: running(footer);
            color:gray;
            font-size: 50%
        }

        @page {
            @bottom-center {
                content: element(footer);
            }
            margin-bottom : 1in;
            size: 6in 6in;
        }


    </style>
</head>
<body>
<div id="footer">
This is the text that goes at the bottom of every page.
</div>

....
..
.
</body>
</html>
Pirate_Jack
  • 138
  • 1
  • 3
  • 11

1 Answers1

2

You're doing everything right. It wasn't working for you because running elements (position: running(..) and content: element(..) properties) support is added only in the latest version, iText 7.1.2. It was released in the end of April.

  • 2
    I am using 7.1.7 and @page doesnot work in order to make it work documentation says to setMediaDeviceDescription to MediaType.PRINT and wrap it @meda{@page{}} still doesnt work – Juke Sep 20 '19 at 17:36