We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

PDF Java: Characters changed in stamp

Binary_View_of_input_and_output_strings.png (121.3 KB)
input.pdf (85.8 KB)
output.pdf (283.5 KB)

Aspose Team,

We use the Aspose PDF java package to add stamps to each page of PDF files and found out that some characters are changed in the label of the stamp.

Following is the sample code that reproduces the problem and attached are the input and output pdf files.

The input string for the label is as follows. See the attached picture for the binary view.
S3CbwktvZdqTPAj8f紲吹蹮ヨ糖㒟񆔒ᡌ!kohyfXibc

and the output string in the label of the output pdf file is as follows (copied from the output pdf file that is opened with Adobe Acrobat Reader DC). See the attached picture for the binary view.
S3CbwktvZdqTPAj8f紲吹􀀀ヨ􀀀􀀀􀀀􀀀!kohyfXibc

Note that some characters in the output string are different from that in the input string (check with the binary views).

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

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 inPath = "/home/ubuntu/testdirs/testdir_stamp_chars/input.pdf";
        String outPath = "/home/ubuntu/testdirs/testdir_stamp_chars/output.pdf";
        String ucStr = "S3CbwktvZdqTPAj8f紲吹蹮ヨ糖㒟񆔒ᡌ!kohyfXibc";
        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 = 0;
        int rightMargin = 0;
        int topMargin = 10;
        int bottomMargin = 10;
        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(inPath);
        editor.resizeContentsWithNormalization(document, parameters);

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

        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

We have replicated the similar issue in our environment and have logged it as PDFJAVA-40629 in our issue tracking system. 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.