Sample.pdf (143.4 KB)
Aspose Team,
We use the Aspose PDF java package to add stamps to each page of PDF files, and then use the following Ghostscipt command to conver the PDF file to grayscale.
sudo gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite
-sFONTPATH=/usr/share/fonts/
-dHaveTransparency=false
-dProcessColorModel=/DeviceGray
-dColorConversionStrategy=/Gray
-o Sample_gray.pdf
-f Sample.pdf
We found the following error with some PDF files.
GPL Ghostscript 9.52 (2020-03-19)
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 2.
Page 1
**** Warning: Found circular references in resource dictionaries while checking for transparency.
Page 2
**** This file had errors that were repaired or ignored.
**** The file was produced by:
**** >>>> Aspose.PDF for Java 20.7 <<<<
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe’s published PDF
**** specification.
Following is the sample code that reproduces the problem and attached is the sample file. Note that the problem is caused by the following statement that resizes the page before adding the stamps.
editor.resizeContents(document, parameters);
The operating system is Ubuntu 18.04. Java version is 1.8. Aspose PDF java package is 20.7 and Ghostscript version is 9.52.
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/testdir/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);
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;
}
}