JAVA 는 itextpdf 를 사용 하여 pdf 에 워 터 마크 를 추가 합 니 다.
package test;
import java.io.FileOutputStream;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
public class pdfUtils {
	 /**
     * @param inputFile   PDF    
     * @param outputFile        PDF     
     * @param waterMarkName     
     * @return
     */
      public static boolean waterMark(String inputFile,
                                    String outputFile, String waterMarkName) {
        try {
        PdfReader reader = new PdfReader(inputFile);
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
                    outputFile));
            //           ,            
            BaseFont base = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);//       
            int total = reader.getNumberOfPages() + 1;
            PdfContentByte under;
            Rectangle pageRect = null;
            for (int i = 1; i < total; i++) {
                pageRect = stamper.getReader().
                        getPageSizeWithRotation(i);
                //     X,Y  
//                float x = pageRect.getWidth()/10;
//                float y = pageRect.getHeight()/10-10;
                //   PDF   
                under = stamper.getOverContent(i);
                under.saveState();
                // set Transparency
                PdfGState gs = new PdfGState();
                //       0.2
                gs.setFillOpacity(0.8f);
                under.setGState(gs);
                under.restoreState();
                under.beginText();
                under.setFontAndSize(base, 25);
                under.setTextMatrix(30, 30);
                under.setColorFill(BaseColor.RED);
            	for (int y = 0; y < 10; y++) {
					for (int x = 0; x < 8; x++) {
						//      45    
		                under.showTextAligned(Element.ALIGN_LEFT
		                        , waterMarkName, 100 + 300 * x, 300 * y, 45);					}
				}
                
                //       
                under.endText();
                under.setLineWidth(1f);
                under.stroke();
            }
            stamper.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
      
      public static void main(String[] args) { 
      	System.out.println(pdfUtils.waterMark("D:\\222\\  &  .pdf", "D:\\222\\  .pdf", "    "));
      }
      
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.