Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用iText7 pdfHtml向每个页面添加透明水印文本_Java_Itext_Itext7 - Fatal编程技术网

Java 如何使用iText7 pdfHtml向每个页面添加透明水印文本

Java 如何使用iText7 pdfHtml向每个页面添加透明水印文本,java,itext,itext7,Java,Itext,Itext7,目前,我正在尝试使用iText7 pdfHtml在我的pdf的每个页面的背景中添加水印,但我无法找到解决方案。例如,我希望文本“机密”出现在每一页的背景中。我已经尝试用css添加它,就像这样 @page { size: Letter; margin: .5in .5in .5in .5in; @left-middle { content: "Confidential"; /* z-index: 100; */ font-s

目前,我正在尝试使用iText7 pdfHtml在我的pdf的每个页面的背景中添加水印,但我无法找到解决方案。例如,我希望文本“机密”出现在每一页的背景中。我已经尝试用css添加它,就像这样

@page {
    size: Letter;
    margin: .5in .5in .5in .5in;

    @left-middle {
        content: "Confidential";
        /* z-index: 100; */
        font-size: 80pt;
        font-weight: bold;
        opacity: .2;
        text-align: center;
        text-transform: uppercase;
        transform: translateX(350px) rotate(-54.7deg);
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        overflow: auto;
        z-index: 0;
    }
}
这几乎解决了我的问题,但文本不是透明的,它掩盖了后面的文本。它也不会旋转,但这不是必要的要求

欢迎使用涉及Java、CSS或Html组合的解决方案

下面是我的Java代码示例:

        FileInputStream htmlStream = null;
        FileOutputStream pdfStream = null;

        try {
            ConverterProperties converterProperties = new ConverterProperties().setBaseUri(path);
            converterProperties.setMediaDeviceDescription(new MediaDeviceDescription(MediaType.PRINT));
            htmlStream = new FileInputStream(inputPath);
            pdfStream = new FileOutputStream(outputPath);
            HtmlConverter.convertToPdf(htmlStream, pdfStream, converterProperties);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (htmlStream != null) {
                htmlStream.close();
            }
            if (pdfStream != null) {
                pdfStream.close();
            }
        }
编辑

要复制的html示例:

<!DOCTYPE html>
<html>
<link id="watermark_link" href="watermark.css" rel="stylesheet" type="text/css" />
</head>

<body>
    <h1>My First Heading</h1>
    <p>My first paragraph.</p>
</body>

</html>

我希望这能帮助其他尝试开始使用iText pdfHtml的人

这里有一个解决方案,可以在“每页的背景”中添加文本。这将在现有内容后面添加文本,这样就不会掩盖它。请注意,这不会增加透明度。透明度需要与外部图形状态一起添加

try(PdfDocument doc=newpdfdocument(newpdfReader(in.toFile()),newpdfWriter(out.toFile())){
PdfFont helvetica=PdfFontFactory.createFont();

对于(int pageNum=1;pageNum),我尝试用css创建html文件,但失败或“机密”没有出现在html中。您可以附加您试图处理的整个html文件吗?我对html2pdf不是很熟悉,但您可以在html2pdf处理完成后,在页面中循环,并在每个页面中添加一块文本。这将是一个java解决方案。@UladzimirAsipchuk我用一些基本的html编辑了我的初始帖子,以重新编写减少这个问题。我不能提供完整的html文档,因为它实际上包含敏感信息,但是这个html与我之前发布的CSS结合起来就足以复制。另外,你只是在浏览器中打开html吗?我不相信我提供的CSS在大多数浏览器中都能工作,所以你需要通过iText将它发送到vi查看结果。生成的pdf有一个带有“机密”的页面在中间,但它是100%不透明的,并掩盖了它背后的任何东西。@BenIngle感谢您的建议。我将研究通过Java代码向每个页面添加水印。谢谢您的回答!我现在很忙,但我会在有机会尝试时更新。希望是今天或明天。再次感谢!我最终修改了第三种选择是使用我已经拥有的pdfHtml。我用我的最终代码更新了帖子,以备将来参考,以防有人再次使用。在我发布之前,我一直在玩弄类似于最终解决方案的东西,但我认为我缺少的关键部分是PdfExtGState,以增加文本的透明度。我认为非常感谢你的帮助!
    private static void generatePDFFromHTML(String inputPath, String outputPath, String baseUrl) throws IOException {
        FileInputStream htmlStream = null;
        FileOutputStream pdfStream = null;

        try {
            ConverterProperties converterProperties = new ConverterProperties().setBaseUri(baseUrl);
            converterProperties.setMediaDeviceDescription(new MediaDeviceDescription(MediaType.PRINT));
            htmlStream = new FileInputStream(inputPath);
            pdfStream = new FileOutputStream(outputPath);
            PdfWriter writer = new PdfWriter(pdfStream);
            PdfDocument pdfDocument = new PdfDocument(writer);
            Watermark watermark = new Watermark("Confidential");
            pdfDocument.addEventHandler(PdfDocumentEvent.END_PAGE,watermark);
            HtmlConverter.convertToPdf(htmlStream, pdfDocument, converterProperties);
            pdfDocument.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (htmlStream != null) {
                htmlStream.close();
            }
            if (pdfStream != null) {
                pdfStream.close();
            }
        }

    }

    protected static class Watermark implements IEventHandler {

        String watermarkText;

        public Watermark(String watermarkText) {
            this.watermarkText = watermarkText;
        }

        @Override
        public void handleEvent(Event event) {
            //Retrieve document and
            PdfDocumentEvent docEvent = (PdfDocumentEvent) event;
            PdfDocument pdf = docEvent.getDocument();
            PdfPage page = docEvent.getPage();
            Rectangle pageSize = page.getPageSize();
            PdfCanvas pdfCanvas = new PdfCanvas(page.getLastContentStream(), page.getResources(), pdf);
            Canvas canvas = new Canvas(pdfCanvas, pdf, pageSize);
            PdfExtGState gstate = new PdfExtGState();
            gstate.setFillOpacity(.2f);
            pdfCanvas.setExtGState(gstate);

            double rotationDeg = -54.7d;
            double rotationRad = Math.toRadians(rotationDeg);
            Paragraph watermarkParagraph = new Paragraph(watermarkText)
                    .setFontSize(80f)
                    .setTextAlignment(TextAlignment.CENTER)
                    .setVerticalAlignment(VerticalAlignment.MIDDLE)
                    .setRotationAngle(rotationRad)
                    .setFixedPosition(100, page.getPageSize().getHeight(), page.getPageSize().getWidth());
            canvas.add(watermarkParagraph);
            canvas.close();

        }

    }