2021年4月11日日曜日

PDFBoxで書き込みパスワードを設定する

 kingFisherでは、Javaで生成したPDFをメール送付又は、ダウンロードできることを考えていますが、受け取り者が編集できてしまうのは具合が悪く、読み取りはフリーですが、更新は不可とするためパスワードを付与してみます。

実現するには、org.apache.pdfbox.pdmodel.encryption.AccessPermissionと、org.apache.pdfbox.pdmodel.encryption.StandardProtectionPolicyクラスを使用します。

指定方法は以下


//制限を設定
AccessPermission objAp = new AccessPermission();
objAp.setCanAssembleDocument(false);
objAp.setCanExtractContent(false);
objAp.setCanExtractForAccessibility(false);
objAp.setCanFillInForm(false);
objAp.setCanModify(false);
objAp.setCanModifyAnnotations(false);
objAp.setCanPrint(false);
objAp.setCanPrintDegraded(false);
// ap.setReadOnly();
StandardProtectionPolicy spp = new StandardProtectionPolicy("12345", "", objAp);
spp.setEncryptionKeyLength(128);
document.protect(spp);


具体例を示しますと、まず引数にファイル名を渡し、PDF生成部分を呼び出す部分

File pdfOut = new File("C:\\Work\\testpdf001secure.pdf");
createPdf(pdfOut);

呼び出されるPDF作成部分

	protected void createPdf(File pdfOut) throws IOException{
		// 空のドキュメントオブジェクトを作成します
		try(PDDocument document = new PDDocument()) {
			//制限を設定
			AccessPermission objAp = new AccessPermission();
			objAp.setCanAssembleDocument(false);
			objAp.setCanExtractContent(false);
			objAp.setCanExtractForAccessibility(false);
			objAp.setCanFillInForm(false);
			objAp.setCanModify(false);
			objAp.setCanModifyAnnotations(false);
			objAp.setCanPrint(false);
			objAp.setCanPrintDegraded(false);
            // ap.setReadOnly();
            StandardProtectionPolicy spp = new StandardProtectionPolicy("12345", "", objAp);
            spp.setEncryptionKeyLength(128);
            document.protect(spp);

			// 新しいページのオブジェクトを作成します
			PDRectangle rectangle = PDRectangle.A4;
			PDPage page = new PDPage(rectangle);
			document.addPage(page);
			try (TrueTypeCollection ttcG = new TrueTypeCollection(new File("C:/Windows/Fonts/msgothic.ttc"));
				 TrueTypeCollection ttcM = new TrueTypeCollection(new File("C:/Windows/Fonts/msmincho.ttc"))) {

	            TrueTypeFont ttfG = ttcG.getFontByName("MS-Gothic");
	            PDFont fontG = PDType0Font.load(document, ttfG, true);
	            TrueTypeFont ttfM = ttcM.getFontByName("MS-Mincho");
	            PDFont fontM = PDType0Font.load(document, ttfM, true);

	            try(PDPageContentStream contentStream = new PDPageContentStream(document, page)){
	            	//文字出力
	            	contentStream.beginText();
					contentStream.setFont(fontG, 14);
					contentStream.newLineAtOffset(10, rectangle.getHeight() - 40);
					contentStream.showText( "この文字はフォント MSゴシックです" );
					contentStream.endText();
	            	//文字出力
					contentStream.beginText();
					contentStream.setFont(fontM, 14);
					contentStream.newLineAtOffset(10, rectangle.getHeight() - 70);
					contentStream.showText( "この文字はフォント MS明朝です" );
					contentStream.endText();
	            }
				// ドキュメントを保存します
				document.save(pdfOut);
			}
		}
	}


作成されたPDFを開きプロパティを見てみると、パスワードが設定されているのが分かります



0 件のコメント:

コメントを投稿

適格請求書等保存方式(インボイス制度)と消費税の端数処理

消費税の税額計算は 売上税額-仕入れ税額=納税額 2023年10月以降、この納税額の計算の元になる請求書は適格請求書(インボイス)の保存が必要となる。 2019年10月から消費税が10%に引き上げられる際に、日用品等は8%に据え置かれ複数税率を扱う事業者が発生する。 この軽減税率...