PDF Java: Graphics get lost when adding stamps

Sample_488.pdf (76.1 KB)
Sample_492.pdf (113.0 KB)

Aspose Team,

We use the Aspose PDF java package to add stamps to each page of PDF files and found out that some graphics are lost with some PDF files.

Following is the sample code that reproduces the problem and attached are two sample files. In Sample_488.pdf the yellow rectangle at top gets lost and in Sample_492.pdf the black rectangle at top gets lost.

The operating system is Ubuntu 18.04. Java version is 1.8. Aspose PDF java package is 20.9

import com.aspose.pdf.*;
import com.aspose.pdf.facades.FormattedText;
import com.aspose.pdf.facades.PdfFileEditor;
import java.util.ArrayList;
import java.util.List;

public class TestStamp {
public static void main(String[] args) {
try {

        String path = "/home/ubuntu/testdir_redact_stamp/Sample_488.pdf";
        String ucStr = "Test";
        String llStr = "1";
        String lrStr = "YAX.PC.00000198";
        String fontFamily = "DejaVu Sans";
        Font font = FontRepository.findFont(fontFamily);;
        int fontSize = 10;

        // Stamps
        List<TextStamp> stampsToAdd = new ArrayList<>();
        // Uper Center
        stampsToAdd.add(convertToStamp(ucStr, HorizontalAlignment.Center, VerticalAlignment.Top, font, fontSize));
        // Lower Left
        stampsToAdd.add(convertToStamp(llStr, HorizontalAlignment.Left, VerticalAlignment.Bottom, font, fontSize));
        // Lower Right
        stampsToAdd.add(convertToStamp(lrStr, HorizontalAlignment.Right, VerticalAlignment.Bottom, font, fontSize));

        // Resize the page before adding the stamps
        int leftMargin = 5;
        int rightMargin = 5;
        int topMargin = 11;
        int bottomMargin = 11;
        PdfFileEditor.ContentsResizeParameters parameters = new PdfFileEditor.ContentsResizeParameters(

        PdfFileEditor editor = new PdfFileEditor();
        Document document = new Document(path);
        editor.resizeContents(document, parameters);

        for (Page page : document.getPages()) {
            for(TextStamp stamp : stampsToAdd){

    catch(Exception ex) {

private static TextStamp convertToStamp(String text, int horizontalAlignment, int verticalAlignment,
                                        Font font, int fontSize) {
    FormattedText formattedText = new FormattedText();
    TextStamp textStamp = new TextStamp(formattedText);
    return textStamp;



We were able to reproduce the issue in our environment and have logged it as PDFJAVA-39864 in our issue management system. We will surely investigate reasons behind it and will inform you as soon as it is resolved. Please be patient and give us some time.

We are sorry for the inconvenience.

In addition, when I ran the following Ghostscript command to convert the pdf file with stamps into tiff

sudo gs -dBATCH -dNOPAUSE -sFONTPATH=/usr/share/fonts/
-o /home/ubuntu/testdir_redact_stamp/Sample_488.tif
-f /home/ubuntu/testdir_redact_stamp/Sample_488.pdf

I got the following error (the output tiff file looks good though).

GPL Ghostscript 9.53.1 (2020-09-14)
Copyright © 2020 Artifex Software, Inc. All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
Processing pages 1 through 1.
Page 1
Loading NimbusSans-Regular font from %rom%Resource/Font/NimbusSans-Regular… 4373184 3050436 2815392 1406716 4 done.
**** Error: Form stream has unbalanced q/Q operators (too many q’s)
Output may be incorrect.
**** Error: Form stream left garbage on the stack, attempting to recover.
Output may be incorrect.

Note that there is no error with the original pdf file.

Ghostscript version is: 9.53
Operation system is: Ubuntu 18.04.4 LTS

Ghostscript developer got the following details for the “Error: Form stream left garbage on the stack, attempting to recover.” error:

Hello, Form ‘Fm0’ object 20 in the file has a stream that contains an invalid
operator “Qq” at the end in the sequence:

Qq 1 w 0 0 0 RG 0 0 0 rg 48.29763 431.83766 514.71477 123.50395 re B Q q
/RubArial_352_296 8 Tf 0 Tr 1 1 1 rg 9.98784 TL 262.97501 490.63997 Td
(Attorney Client Privilege) Tj
ET Q q 0 0 0 RG 0 0 0 rg 48.29763 431.83766 514.71477 123.50395 re n Q

Probably the creator missed the need for whitespace between the Q and the q


Thanks for providing further details.

We have updated the information against logged ticket and will let you know as soon as it is resolved.

The issues you have found earlier (filed as PDFJAVA-39864) have been fixed in Aspose.PDF for Java 21.3.

NullPointer_Sample.pdf (133.6 KB)

We tested some sample files with PDF release 21.3, and verified that the graphics loss problem is resolved. But the new method (resizeContentsWithNormalization) fails with some other sample files with the following error. I uploaded the sample file (NullPointer_Sample.pdf).
at com.aspose.pdf.Operator.toString(Unknown Source)
at com.aspose.pdf.Operator.toString(Unknown Source)
at com.aspose.pdf.OperatorCollection.toString(Unknown Source)
at com.aspose.pdf.OperatorCollection.updateNormalizedData(Unknown Source)
at com.aspose.pdf.facades.APdfFileEditor.resizeContents(Unknown Source)
at com.aspose.pdf.facades.PdfFileEditor.resizeContents(Unknown Source)
at com.aspose.pdf.facades.APdfFileEditor.resizeContentsWithNormalization(Unknown Source)
at com.aspose.pdf.facades.PdfFileEditor.resizeContentsWithNormalization(Unknown Source)
at TestStamp.main(TestStamp.java:49)

Note that the old method (resizeContents) also has the graphics loss problem fixed and it does not have the NullPointerException problem.


We were able to replicate the issue in our environment with Aspose.PDF for Java 21.3. Therefore, a ticket as PDFJAVA-40415 has been logged in our issue tracking system for the sake of correction. We will further look into its details and keep you posted with the status of its rectification. Please be patient and spare us some time.

We are sorry for the inconvenience.

The issues you have found earlier (filed as PDFJAVA-40415) have been fixed in Aspose.PDF for Java 21.6.