Participant
May 12, 2025
Question
B/W PDFs | GeneratePDFService optimizePdf Not Working - Sample Code Anyone?
- May 12, 2025
- 1 reply
- 232 views
I have been tasked with converting the rendering of an output pdf document from the default (color) to instead generate pdfs in Black & White / Greyscale. We are seeing a couple issues on our end with some of the Adobe code when using the implementation in an OSGI AEM Forms context on version 6.5.18 and wanted to reach out to the community to see if anyone had been able to get either of these methods functioning to generate black and white output.
//initial document creation existing code example
// (templateDocument is an xdp, inXml is an xml used to fill in the template)
PDFOutputOptions pdfOutputOptions = new PDFOutputOptions();
pdfOutputOptions.setAcrobatVersion(AcrobatVersion.Acrobat_10);
final Document doc = outputService.generatePDFOutput(templateDocument, inXml, pdfOutputOptions);
File outputPdfTemp = new File("C:\\output\\output-pdf-temp.pdf");
doc.copyToFile(outputPdfTemp);
Document outputPdfTempDoc = new Document(Files.newInputStream(outputPdfTemp.toPath()));
//try convert to b/w via optimize pdf using pdfsettings file, schema @ https://help.adobe.com/en_US/livecycle/11.0/generatePDFConversionRef/file2pdfconfig_xsd.html
File pdfSettings = new File("C:\\output\\pdfsettings.xml");
Document pdfSettingsDoc = new Document(Files.newInputStream(pdfSettings.toPath()));
//METHOD 1: code errors out with uninformative error output
try {
OptimizePDFResult optimizedPdf = generatePDFService.optimizePDF(outputPdfTempDoc, null, pdfSettingsDoc);
Document bwPdf = optimizedPdf.getConvertedDocument();
File outFile = new File("C:\\output\\bw-pdf.pdf");
bwPdf.copyToFile(outFile);
}catch(Exception e){
e.printStackTrace();
log.error("NoticeServiceImpl - Invoke - Error: "+e.getMessage());
}
//METHOD 2: expecting this to convert to greyscale but final document still showing in color when printing ps file
ToPSOptionsSpec spec = new ToPSOptionsSpec();
spec.setColor(com.adobe.fd.cpdf.api.enumeration.Color.compositeGray);
Document psNoColorDocument = convertPdfService.toPS(pdfDocument, spec);
File psNoColorFile = new File("C:\\output\\ps-nocolor.ps");
if(psNoColorDocument != null){
psNoColorDocument.copyToFile(psNoColorFile);
}
A couple different pdfsettings.xml configs are attached that have been tried.
