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;
}
}