Reputation: 1646
I am interested in using PDFBox for a project that requires being able to specify spot colors and color separations in PDF output to go to a professional printer and am curious as to whether or not it supports this. If so (and I think so), I am also looking for some example code.
I found an old post from 2009 on their mailing list (here) that leads me to believe PDFBox can support color separations, but have not succeeded in finding any example code. I looked through their JavaDoc and discovered the org.apache.pdfbox.pdmodel.graphics.color
classes, but don't have any idea how to leverage them and don't see any cookbook examples on their website or in their source code.
I specifically would appreciate any examples that help illustrate the DeviceN colorspace.
Upvotes: 12
Views: 1444
Reputation: 21
Please see as below
get the PDColor from a PDF file(spotColor.pdf),and make sure that the spot colors which you well used are in this PDF file.(I made the file by Adobe Illustrator)
public static Map<String, PDColor> getSpotColor() {
Map<String, PDColor> colors = new HashMap<String, PDColor>();
PDDocument spotColorFile = null;
try {
spotColorFile = PDDocument.load(new FileInputStream(new File(
"d:\\spotColor.pdf")));
for (PDPage page : spotColorFile.getPages()) {
for (COSName name : page.getResources().getColorSpaceNames()) {
PDColor color = page.getResources().getColorSpace(name)
.getInitialColor();
PDSeparation cs = (PDSeparation) color.getColorSpace();
colors.put(cs.getColorantName(), color);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (spotColorFile != null)
try {
spotColorFile.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
spotColorFile = null;
}
}
return colors;
}
use your PDColor
public static void main(String[] args) {
PDDocument doc = null;
PDPage page = null;
try {
Map<String, PDColor> colors = getSpotColor();
doc = new PDDocument();
page = new PDPage(new PDRectangle(100, 100));
doc.addPage(page);
PDPageContentStream content = new PDPageContentStream(doc, page);
content.beginText();
content.setNonStrokingColor(colors.get("PANTONE 2607 C"));
content.setFont(PDType1Font.HELVETICA_BOLD, 20);
content.showText("abcdef");
content.endText();
content.setNonStrokingColor(colors.get("PANTONE 108 U"));
content.addRect(50, 50, 50, 50);
content.fill();
content.close();
doc.save("d:\\spotColorTest.pdf");
} catch (Exception e) {
System.out.println(e);
} finally {
if (doc != null)
try {
doc.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
doc = null;
}
}
}
3 if you have some idea more smarter, please let me know :)
Upvotes: 2