You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Weite Feng <wa...@gmail.com> on 2024/02/18 03:24:14 UTC

When adding a watermark to a PDF file, there may be a native memory leak issue.

When using the following code to add a watermark to a PDF file, the memory usage of the Java process will gradually increase, even exceeding the limit of the maximum heap memory usage. When the process uses memory exceeding the maximum memory of the machine, the Java process will be killed by the operating system.

When analyzing the dumped memory, I found that when the Java process occupies a large amount of memory (viewed through the top command), the heap memory of the process actually does not occupy too much space, so I inferred that there may be a native memory leak in this code, due to I don’t have a deep understanding of Linux memory analysis, so I can’t find the problem in this code.

I wonder if you have any suggestion.

The following is the code I use to add watermarks to PDF: 

public ByteBuffer AddWaterMark(AddWaterMarkRequest request) throws Exception {

   byte[] b = TBaseHelper.copyBinary(request.PDFContent).array();
   PDDocument pdf = PDDocument.load(b);
   ByteArrayOutputStream output = new ByteArrayOutputStream();

   if (Objects.equals(request.WaterMark.Font, "") || !this.isFontExist(request.WaterMark.Font)) {
       request.WaterMark.Font = pdf_rpcConstants.WenQuanDengKuanZhengHei;
   }
   PDFont font = getFontFromByte(pdf, request.WaterMark.Font);

   addWaterMark(pdf, request.WaterMark, font);
   pdf.save(output);
   pdf.close();
   return ByteBuffer.wrap(output.toByteArray());
}

private void addWaterMark(PDDocument pdf, WaterMark waterMark, PDFont font) throws Exception {

   List<String> texts = waterMark.getWaterMarkTexts();
   for (PDPage page : pdf.getPages()) {

       PDPageContentStream cs = new PDPageContentStream(pdf, page, PDPageContentStream.AppendMode.APPEND, true, true);
       PDExtendedGraphicsState r0 = new PDExtendedGraphicsState();

       r0.setNonStrokingAlphaConstant((float) waterMark.AlphaConstant);
       r0.setAlphaSourceFlag(true);
       cs.setGraphicsStateParameters(r0);

       cs.setNonStrokingColor(new Color(waterMark.Color.Red, waterMark.Color.Green, waterMark.Color.Blue));

       float horizontalSpacing = waterMark.HorizontalSpacing;
       float verticalSpacing = waterMark.VerticalSpacing;

       int horizontalNumber = (int) (page.getMediaBox().getWidth() / horizontalSpacing) + 2;
       int verticalNumber = (int) (page.getMediaBox().getHeight() / verticalSpacing) + 2;

       cs.beginText();
       cs.setFont(font, waterMark.getFontSize());
       for (int i = 0; i <= horizontalNumber; i++) {
           for (int j = 0; j < verticalNumber; j++) {
               for (int k = 0; k < texts.size(); k++) {
                   float tx = waterMark.StartIndexX + (i - 1) * horizontalSpacing + k * waterMark.getLineSpace();
                   float ty = waterMark.StartIndexY + j * verticalSpacing;
                   if (i % 2 == 0) {
                       cs.setTextMatrix(Matrix.getRotateInstance(waterMark.RotateTheta, tx, ty));
                   } else {
                       cs.setTextMatrix(Matrix.getRotateInstance(waterMark.RotateTheta, tx, ty + waterMark.Offset));
                   }
                   cs.showText(texts.get(k));
               }
           }
       }
       cs.endText();
       cs.restoreGraphicsState();
       cs.close();
   }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org