1. PDF Stamp Text with Apache PDFBox 2.0.x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | import java.awt.Color; import java.awt.Rectangle; import java.io.File; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.PDPageContentStream.AppendMode; import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException; import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDType0Font; import org.apache.pdfbox.pdmodel.font.PDType1Font; public class PdfStamp { PDDocument document; public PdfStamp() { } public PdfStamp(String fileName) throws InvalidPasswordException, IOException { load(fileName); } public void load(String fileName) throws InvalidPasswordException, IOException { document = PDDocument.load(new File(fileName)); } public void close() throws IOException { document.close(); } public void save(String fileName) { try { this.document.save(fileName); this.document.close(); } catch (IOException e) { e.printStackTrace(); } } public PDFont loadFont(String fontFile) { PDFont font = PDType1Font.HELVETICA_BOLD; if( new File(fontFile).exists() ) { try { return PDType0Font.load(document, new File(fontFile)); } catch (IOException e) { } } return font; } private int convY(PDPage page, int y) { return (int)page.getMediaBox().getHeight() - y - 1; } public int getTextWidth( String text, PDFont font, int fontSize ) { try { return (int)((font.getStringWidth(text)/1000)*fontSize); } catch (IOException e) { e.printStackTrace(); return 0; } } public void drawText( PDPage page, int x, int y, String text, Color color, Color bgColor, PDFont font, int fontSize ) throws IOException { if( font == null ) { font = PDType1Font.HELVETICA; } y += fontSize * 1.5; if( bgColor != null ) { int width = getTextWidth(text, font, fontSize); drawRect( page, new Rectangle(x,y+3,width,fontSize), bgColor, true ); } PDPageContentStream stream = new PDPageContentStream( this.document, page, AppendMode.APPEND, true ); stream.beginText(); stream.setFont(font, fontSize); stream.setNonStrokingColor( color.getRed(), color.getGreen(), color.getBlue()); stream.newLineAtOffset( x, convY(page,y) ); stream.showText(text); stream.endText(); stream.close(); } public void drawText( PDPage page, int x, int y, String text, Color color ) throws IOException { drawText( page, x, y, text, color, null, PDType1Font.HELVETICA_BOLD, 12); } public void drawLine( PDPage page, int x1, int y1, int x2, int y2, int width, Color color ) { try { PDPageContentStream stream = new PDPageContentStream( this.document, page, AppendMode.APPEND, true ); stream.setLineWidth(width); stream.setStrokingColor( color.getRed(), color.getGreen(), color.getBlue()); stream.moveTo( x1, convY(page, y1) ); stream.lineTo( x2, convY(page, y2) ); stream.stroke(); stream.close(); } catch (IOException e) { e.printStackTrace(); } } public void drawRect( PDPage page, Rectangle rect, Color color, boolean fill) throws IOException { PDPageContentStream stream = new PDPageContentStream( this.document, page, AppendMode.APPEND, true ); stream.addRect(rect.x, convY(page,(int)rect.y), rect.width, rect.height); if (fill) { stream.setNonStrokingColor(color); stream.fill(); } else { stream.setStrokingColor(color); stream.stroke(); } stream.close(); } public void stampTextAllPages(String fileName, int x, int y, String text, Color color, Color bgColor) throws IOException { load(fileName); PDFont font = loadFont("malgunbd.ttf"); int fontSize = 12; for( PDPage page : this.document.getPages() ) { drawText( page, x, y, text, color, bgColor, font, fontSize ); } } public static void main(String[] args) throws IOException { PdfTools pdf = new PdfTools(); pdf.stampTextAllPages("D:/document.pdf",20,20,"[Confidential]",Color.RED,Color.YELLOW); pdf.save("D:/stamp.pdf"); } } |
Input & output: