PDFに透かしを追加する
Contents
[
Hide
]
ExcelファイルをPDFに変換する際、PDFファイルに透かしを追加する要件がある場合があります。次の例では、PDFに変換する際にテキストや画像の透かしを追加する方法が示されています。
PDFにテキスト透かしを追加する
テキストと対応するフォントを指定することで、簡単にPDFにテキスト透かしを追加できます。また、RenderingWatermarkでページへの配置、オフセット、回転、不透明度、前景色/背景色、ページへのスケーリングを設定することができます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//prepare a workbook with 3 pages. | |
Workbook wb = new Workbook(); | |
wb.getWorksheets().get(0).getCells().get("A1").putValue("Page1"); | |
int index = wb.getWorksheets().add(); | |
wb.getWorksheets().get(index).getCells().get("A1").putValue("Page2"); | |
index = wb.getWorksheets().add(); | |
wb.getWorksheets().get(index).getCells().get("A1").putValue("Page3"); | |
wb.getWorksheets().get(index).getPageSetup().setPaperSize(PaperSizeType.PAPER_A_3); | |
//create a font for watermark, and specify bold, italic, color. | |
RenderingFont font = new RenderingFont("Calibri", 68); | |
font.setItalic(true); | |
font.setBold(true); | |
font.setColor(Color.getBlue()); | |
//create a watermark from text and the specified font. | |
RenderingWatermark watermark = new RenderingWatermark("Watermark", font); | |
//specify horizontal and vertical alignment | |
watermark.setHAlignment(TextAlignmentType.CENTER); | |
watermark.setVAlignment(TextAlignmentType.CENTER); | |
//specify rotation | |
watermark.setRotation(30); | |
//specify opacity | |
watermark.setOpacity(0.6f); | |
//specify the scale to page(e.g. 100, 50) in percent. | |
watermark.setScaleToPagePercent(50); | |
//spcify watermark for rendering to pdf. | |
PdfSaveOptions options = new PdfSaveOptions(); | |
options.setWatermark(watermark); | |
wb.save("output_text_watermark.pdf", options); |
PDFに画像透かしを追加する
画像のバイトを指定するだけで、PDFに画像透かしを簡単に追加できます。また、RenderingWatermarkでページへの配置、オフセット、回転、不透明度、前景色/背景色、ページへのスケーリングを設定することができます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//prepare a workbook with 3 pages. | |
Workbook wb = new Workbook(); | |
wb.getWorksheets().get(0).getCells().get("A1").putValue("Page1"); | |
int index = wb.getWorksheets().add(); | |
wb.getWorksheets().get(index).getCells().get("A1").putValue("Page2"); | |
index = wb.getWorksheets().add(); | |
wb.getWorksheets().get(index).getCells().get("A1").putValue("Page3"); | |
wb.getWorksheets().get(index).getPageSetup().setPaperSize(PaperSizeType.PAPER_A_3); | |
//create a watermark from image(you need to prepare image bytes). | |
RenderingWatermark watermark = new RenderingWatermark(imageBytes); | |
//specify offset to alignment. | |
watermark.setOffsetX(100); | |
watermark.setOffsetY(200); | |
//specify rotation | |
watermark.setRotation(30); | |
//specify watermark to background. | |
watermark.setBackground(true); | |
//specify opacity | |
watermark.setOpacity(0.6f); | |
//specify the scale to page(e.g. 100, 50) in percent. | |
watermark.setScaleToPagePercent(50); | |
//spcify watermark for rendering to pdf. | |
PdfSaveOptions options = new PdfSaveOptions(); | |
options.setWatermark(watermark); | |
wb.save("oputput_image_watermark.pdf", options); |