iText 7 将 HTML 中的页眉和页脚添加到 PDF

问题描述 投票:0回答:2

我想向我的 PDF 添加重复的页眉和页脚,该 PDF 由 iText7 通过转换 HTML 创建。

但是,到目前为止我在互联网上找到的所有示例都描述了如何通过代码创建带有页眉和页脚的空白 PDF。

有人知道我怎样才能实现这个目标吗?我已经尝试使用 CSS 打印媒体查询来指定某些区域,但 iText7 似乎忽略了这些区域。

转换非常简单:

string input = "Bestellung.html";
string output = "Bestellung.pdf";
HtmlConverter.ConvertToPdf(new FileInfo(input), new FileInfo(output));

bestellung.html 只是一个带有一些演示内容的纯 HTML 文件。

c# itext7
2个回答
0
投票

请参阅 ConverterProperties 下的 mediaDeviceDescription

如果您的输入文件使用此功能,那么您可以简单地告诉 pdfHTML 解释相关规则:

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

然后调用带有此签名的方法:

static void convertToPdf(InputStream htmlStream, PdfDocument pdfDocument, ConverterProperties converterProperties)

0
投票

你应该把它放在你的控制器中:

pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, new HeaderHandler("static/logo.png"));

pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, new FooterHandler("页脚内容"));

您的 HeaderHandler 应该是:

public class HeaderHandler implements IEventHandler {
private ImageData headerImageData;

public HeaderHandler(String imagePath) {
    try {
        ClassPathResource imgPath = new ClassPathResource(imagePath);
        this.headerImageData = ImageDataFactory.create(imgPath.getURL());
    } catch (IOException e) {
        throw new RuntimeException("Failed to load image for header.", e);
    }
}

@Override
public void handleEvent(Event event) {

    PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
    PdfDocument pdf = docEvent.getDocument();
    PdfCanvas canvas = new PdfCanvas(pdf.getLastPage().newContentStreamBefore(), pdf.getLastPage().getResources(), pdf);
    float scale = 0.3f; // Adjust scale to make the logo smaller if needed
    float imageWidth = headerImageData.getWidth() * scale;
    float imageHeight = headerImageData.getHeight() * scale;

    // Position the logo at the top left
    float xPosition = 36; // Margin from the left
    float topMargin = pdf.getDefaultPageSize().getTop() - imageHeight - 20; // Adjust top margin

    // Add the image
    canvas.addImageAt(headerImageData, xPosition, topMargin, false);
}

}

和你的页脚处理程序:

公共类 FooterHandler 实现 IEventHandler { 私有字符串页脚内容;

public FooterHandler(String footerContent) {
    this.footerContent = footerContent;
}

@Override
public void handleEvent(Event event) {
    PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
    PdfDocument pdf = docEvent.getDocument();
    PdfPage page = docEvent.getPage();
    PdfCanvas canvas = new PdfCanvas(pdf.getLastPage().newContentStreamBefore(), pdf.getLastPage().getResources(), pdf);
    float xPosition = 36; // Left margin
    float yPosition = 40; // Bottom margin
    String[] lines = footerContent.split("\n"); // Split text into lines
    float lineHeight = 10; // Height of each line
    try {
        PdfFont boldFont = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD); // Specify the bold font here
        for (String line : lines) {
            canvas.beginText().setFontAndSize(boldFont, 8).moveText(xPosition, yPosition).showText(line).endText();
            yPosition -= lineHeight; // Move to the next line
        }
    } catch (IOException e) {
        throw new RuntimeException("Failed to create bold font.", e);
    }

}

}

© www.soinside.com 2019 - 2024. All rights reserved.