Support

Knowledge Base - Add watermark (Server)

/**
  * creates a copy of the input file into the output file, and apply the given watermark at the center of all pages.
  *
  * @param inputFile the path of the input document.
  * @param password the password of the input document.
  * @param outputFile the path of the output document.
  * @param watermarkPath the page to remove (0-based from 0 to GetPageCount() - 1)
  * @return true or false
  */
 public static boolean addWatermark(String inputFile, String password, String outputFile, String watermarkPath) {
  try {
   //init the library and activate the license
    Global.Init(company, email, key);
    
    //open the input file
    Document mInputFile = new Document();
    if(mInputFile.Open(inputFile, password) == 0) {
     //create the output file
     Document mOutputDoc = new Document();
     if(mOutputDoc.Create(outputFile) == 0) {
      ImportContext mContext = mOutputDoc.ImportStart(mInputFile);
      if(mContext != null) { //import all pages
       int pagesCount = mInputFile.GetPageCount();
       for(int i = 0; i < pagesCount; i++)
        mOutputDoc.ImportPage(mContext, i, i);
       mContext.Destroy();
      }
      mOutputDoc.Save();
      mOutputDoc.Close();
     
      mOutputDoc.Open(outputFile, null);
      addWatermark(mOutputDoc, watermarkPath);
     }
    }
    mInputFile.Close();
    return true;
   } catch (Exception e) {
    e.printStackTrace();
   }
   return false;
 }
 
 /**
  * Add a watermark to all the pages of the given document in the given position.
  *
  * @param srcDoc the document to apply the watermark to.
  * @param watermarkPath the path of the watermark image.
  */ 
 public static void addWatermark(Document srcDoc, String watermarkPath) {
  try {
    BufferedImage bimg = ImageIO.read(new File(watermarkPath));
    int width = bimg.getWidth();
    int height = bimg.getHeight();
   
    DocImage dimg;  
    if(watermarkPath.toLowerCase().endsWith("png"))
     dimg = srcDoc.NewImage(bimg, true);
    else
     dimg = srcDoc.NewImageJPEG(watermarkPath);
    if(dimg != null) {
     for(int i = 0 ; i < srcDoc.GetPageCount() ; i++) {
      Page page = srcDoc.GetPage(i);    
      ResImage rimg = page.AddResImage(dimg);
      if(rimg != null) {      
       int watermarkX = (int) ((srcDoc.GetPageWidth(i) - width) / 2);
       int watermarkY = (int) ((srcDoc.GetPageHeight(i) - height) / 2);
      
       PageContent content = new PageContent();
       content.Create();
       content.GSSave();
       Matrix mat = new Matrix( width, height, watermarkX, watermarkY);
       content.GSSetMatrix(mat);
       mat.Destroy();
       content.DrawImage(rimg);
       content.GSRestore();
       //add content to page
       page.AddContent(content, true);
       content.Destroy();
       page.Close();
      }
     }
    }
   } catch (Exception e) {
    e.printStackTrace();
   }
   srcDoc.Save();
   srcDoc.Close();
 }
Applies To

RadaeePDF SDK for Server

Details

Created : 2017-05-05 14:44:04, Last Modified : 2017-05-08 15:48:06

Order history

Login to handle your order history.