Set watermark to be output only when printing with iText

Is there output only when printing?

Well, there is such a thing as PDF. I understand, let's make it. Speaking of PDF, iText. There's nothing you can't do with iText. e? Is it possible to create a linearized PDF? If you know that kind of thing, don't read this article.

Honestly, it looks like this on the Acrobat settings screen. I see. Acrobatでの設定画面

Okay, I'll write it right away

Now, if you use Japanese, don't forget ** iTextAsian.jar ** and ** iTextAsianCmaps.jar **.

Watermark.java


import java.awt.Color;
import java.io.FileOutputStream;
import java.util.stream.IntStream;

import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfLayer;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

import lombok.val;

public class Watermark {

	public static void draw(String src, String dest) {
		try {
			val reader = new PdfReader(src);

			val stamp = new PdfStamper(reader, new FileOutputStream(dest));

			val layer = new PdfLayer("watermark", stamp.getWriter());

			layer.setOnPanel(true);

			layer.setOn(false);
			layer.setPrint("Watermark", true);
			layer.setView(false);

			val transparent = new PdfGState();
			
			//Transparency
			transparent.setStrokeOpacity(0.3f);
			transparent.setFillOpacity(0.3f);

			val bf_courier = BaseFont.createFont("HeiseiKakuGo-W5", "UniJIS-UCS2-H", BaseFont.NOT_EMBEDDED);

			IntStream.rangeClosed(1, stamp.getReader().getNumberOfPages()).forEach(page-> {

				val cb = stamp.getOverContent(page);

				cb.setColorFill(Color.RED);

				cb.beginLayer(layer);
				cb.setGState(transparent);

				val watermarkText = "Do not take out";

				val cropBox = reader.getCropBox(page);
				
				cb.beginText();
				cb.setFontAndSize(bf_courier, 20);
				
				//Try to write diagonally at appropriate intervals
				for(int x = 0; x < cropBox.getWidth(); x += 100) {
					for(int y = 0; y < cropBox.getHeight(); y += 100) {
						cb.showTextAligned(PdfContentByte.ALIGN_CENTER, watermarkText, x, y, 45);
					}
				}
				
				cb.endText();

				cb.endLayer();
			});

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

	public static void main(String[] args) {

		if (args.length < 2) {
			System.out.println("Usage: Watermark src.pdf dest.pdf");
			return;
		}

		Watermark.draw(args[0], args[1]);
	}
}

It looks like this

It's a little small and hard to see, but the watermark was drawn only in the print preview. Well, it looks like this. wmpdf.png

Recommended Posts

Set watermark to be output only when printing with iText
How to set environment variables when using Payjp with Rails
What to do when CentOS cannot be started with VirtualBox on Catalina
Notes on what to do when EC2 is set up with t2.micro
Output system time to MANIFEST.MF with Maven
Only up to 100 javac warnings are output.
How to set JAVA_HOME with Maven appassembler-maven-plugin
Be careful when using rails_semantic_logger with unicorn