Java iText移动水印位置

Java iText移动水印位置,java,pdf,itext,pdf-generation,itext7,Java,Pdf,Itext,Pdf Generation,Itext7,我有以下代码可以将图像缩放到A4大小: void scale(PdfDocument pdfDocument, Rectangle pageSize, Rectangle pageBodySize) { int n = pdfDocument.getNumberOfPages(); for (int i = 1; i <= n; i++) { PdfPage page = pdfDocument.getPage(i); MarginF

我有以下代码可以将图像缩放到A4大小:

  void scale(PdfDocument pdfDocument, Rectangle pageSize, Rectangle pageBodySize) {
    int n = pdfDocument.getNumberOfPages();

    for (int i = 1; i <= n; i++) {
        PdfPage page = pdfDocument.getPage(i);

        MarginFinder marginFinder = new MarginFinder();
        PdfCanvasProcessor pdfCanvasProcessor = new PdfCanvasProcessor(marginFinder);
        pdfCanvasProcessor.processPageContent(page);
        Rectangle boundingBox = marginFinder.getBoundingBox();
        if (boundingBox == null || boundingBox.getWidth() == 0 || boundingBox.getHeight() == 0) {
            LOG.warn("Cannot scale page %d contents with bounding box %s\n", i, boundingBox);
            continue;
        } else {
            // Scale and move content into A4 with margin
            double scale = 0, xDiff = 0, yDiff = 0;
            double xScale = pageBodySize.getWidth() / boundingBox.getWidth();
            double yScale = pageBodySize.getHeight() / boundingBox.getHeight();
            if (xScale < yScale) {
                yDiff = boundingBox.getHeight() * (yScale / xScale - 1) / 2;
                scale = xScale;
            } else {
                xDiff = boundingBox.getWidth() * (xScale / yScale - 1) / 2;
                scale = yScale;
            }

            AffineTransform transform = AffineTransform.getTranslateInstance(pageBodySize.getLeft() + xDiff, pageBodySize.getBottom() + yDiff);
            transform.scale(scale, scale);
            transform.translate(-boundingBox.getLeft(), -boundingBox.getBottom());

            new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDocument)
                    .concatMatrix(transform);

        }
        page.setMediaBox(pageSize);
        page.setCropBox(pageSize);
    }
}
编辑2:

addWatermarkToDocumentPages执行以下操作:

 private void addWatermarkToDocumentPages(final Document document, final Paragraph watermark, final Integer position) {
    final PdfDocument pdfDocument = document.getPdfDocument();
    final int pages = pdfDocument.getNumberOfPages();

    // transparency
    final PdfExtGState gs1 = new PdfExtGState();
    gs1.setFillOpacity(0.9f);

    for (int i = 1; i <= pages; i++) {
        final PdfCanvas canvas = new PdfCanvas(pdfDocument.getPage(i));
        final PdfPage pdfPage = pdfDocument.getPage(i);
        final Rectangle pageSize = pdfPage.getPageSizeWithRotation();
        pdfPage.setIgnorePageRotationForContent(true);

        pageSize.increaseHeight(WATERMARK_MARGIN_SIZE);
        pageSize.moveDown(WATERMARK_MARGIN_SIZE);
        pdfPage.setMediaBox(pageSize);

        final float x = (pageSize.getLeft() + pageSize.getRight()) / 2;
        canvas.saveState();
        canvas.setExtGState(gs1);
        document.showTextAligned(watermark, x, position, i, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0);
        canvas.restoreState();
    }
}

有不同的方法可以应用水印。例如,有水印注释,但水印也可以是常规内容的一部分。你说的是哪种水印?将水印添加到文档页面的作用是什么
addWatermark
本身不添加水印,但会委托添加水印。确定后,水印将添加到内容中。我担心这会让移动变得有点困难,像内容这样添加的东西通常表现得像内容,很难更改。难道你不能等到合并和调整大小之后再加水印吗?在加水印之前调整大小是微不足道的。但是,您希望水印最终出现在何处?不同文档的页面上的水印是不同还是相同?水印的应用方式可能不同。例如,有水印注释,但水印也可以是常规内容的一部分。你说的是哪种水印?将水印添加到文档页面的作用是什么
addWatermark
本身不添加水印,但会委托添加水印。确定后,水印将添加到内容中。我担心这会让移动变得有点困难,像内容这样添加的东西通常表现得像内容,很难更改。难道你不能等到合并和调整大小之后再加水印吗?在加水印之前调整大小是微不足道的。但是你到底希望水印最终出现在哪里?不同文档的页面上的水印是不同的还是相同的?
 private byte[] addWatermark(final byte[] content, final String watermarkText, final Integer[] RgbColor, final Integer position) {
    final ByteArrayOutputStream pdfByteArrayOutputStream = new ByteArrayOutputStream();
    byte[] newPdfContent = new byte[0];
    Document document = null;

    try {
        document = createPdf(content, pdfByteArrayOutputStream);
        final Paragraph watermark = createWaterMark(watermarkText, RgbColor);
        addWatermarkToDocumentPages(document, watermark, position);
    } catch (final IOException e) {
        LOG.error("Error while adding watermark to Shipping Label, ", e);
    } finally {
        if (Objects.nonNull(document)) {
            document.close();
            newPdfContent = pdfByteArrayOutputStream.toByteArray();
        }
    }

    return newPdfContent;
}
 private void addWatermarkToDocumentPages(final Document document, final Paragraph watermark, final Integer position) {
    final PdfDocument pdfDocument = document.getPdfDocument();
    final int pages = pdfDocument.getNumberOfPages();

    // transparency
    final PdfExtGState gs1 = new PdfExtGState();
    gs1.setFillOpacity(0.9f);

    for (int i = 1; i <= pages; i++) {
        final PdfCanvas canvas = new PdfCanvas(pdfDocument.getPage(i));
        final PdfPage pdfPage = pdfDocument.getPage(i);
        final Rectangle pageSize = pdfPage.getPageSizeWithRotation();
        pdfPage.setIgnorePageRotationForContent(true);

        pageSize.increaseHeight(WATERMARK_MARGIN_SIZE);
        pageSize.moveDown(WATERMARK_MARGIN_SIZE);
        pdfPage.setMediaBox(pageSize);

        final float x = (pageSize.getLeft() + pageSize.getRight()) / 2;
        canvas.saveState();
        canvas.setExtGState(gs1);
        document.showTextAligned(watermark, x, position, i, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0);
        canvas.restoreState();
    }
}
private Document createPdf(final byte[] content, final ByteArrayOutputStream outputStream) throws IOException {
    try {
        final PdfDocument pdfDocument = new PdfDocument(new PdfReader(new ByteArrayInputStream(content)), new PdfWriter(outputStream));
        return new Document(pdfDocument);
    } catch (final IOException e) {
        throw new IOException("Error while creating the PDF Document, ", e);
    }
}