PDF Java: Graphics get changed when adding stamps

Result.pdf (32.5 KB)
Sample.pdf (4.1 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 changed with some PDF files.

Following is the sample code that reproduces the problem and attached are two files (sample file and result file). In the sample file there is a white box in the middle of the page. After the file is resized the white box becomes black and a small rectangle is added to the page in the lower left part.

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

Note that we filed a bug (PDF Java: Graphics get lost when adding stamps (PDF Java: Graphics get lost when adding stamps)) some time ago for graphics get lost. The same sample codes are the same.

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 {
System.out.println(“Start”);

        String path = "/home/ubuntu/testdirs/testdir_redact_stamp_2/Sample.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.ContentsResizeValue.units(leftMargin),
                null,
                PdfFileEditor.ContentsResizeValue.units(rightMargin),
                PdfFileEditor.ContentsResizeValue.units(topMargin),
                null,
                PdfFileEditor.ContentsResizeValue.units(bottomMargin)
        );

        PdfFileEditor editor = new PdfFileEditor();
        Document document = new Document(path);
        editor.resizeContents(document, parameters);
        document.save();  // Error happens here.

        for (Page page : document.getPages()) {
            for(TextStamp stamp : stampsToAdd){
                page.addStamp(stamp);
            }
        }
        document.save();

        System.out.println("Done");
    }
    catch(Exception ex) {
        ex.printStackTrace();
    }
}

private static TextStamp convertToStamp(String text, int horizontalAlignment, int verticalAlignment,
                                        Font font, int fontSize) {
    FormattedText formattedText = new FormattedText();
    formattedText.addNewLineText(text);
    TextStamp textStamp = new TextStamp(formattedText);
    textStamp.setWordWrap(true);
    textStamp.setHorizontalAlignment(horizontalAlignment);
    textStamp.setVerticalAlignment(verticalAlignment);
    textStamp.getTextState().setFont(font);
    textStamp.getTextState().setFontSize(fontSize);
    textStamp.setTopMargin(0);
    return textStamp;
}

}

@xyang

Another issue as PDFJAVA-40153 has been logged in our issue tracking system after testing the scenario with Aspose.PDF for Java 21.1. We will further look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.

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