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.
Save as Image − Using PDFBox, you can save PDFs as image files, such as PNG or JPEG. Create PDFs − Using PDFBox, you can create a new PDF file by creating Java programs and, you can also include images and fonts. Signing − Using PDFBox, you can add digital signatures to the PDF files.
Apache PDFBox is an open-source Java library that supports the development and conversion of PDF documents. Using this library, you can develop Java programs that create, convert and manipulate PDF documents. In addition to this, PDFBox also includes a command line utility for performing various operations over PDF using the available Jar file.
PDFBox − This is the main part of the PDFBox. This contains the classes and interfaces related to content extraction and manipulation. FontBox − This contains the classes and interfaces related to font, and using these classes we can modify the font of the text of the PDF document.
If you verify the specified path, you can find that the required page was deleted and only two pages remained in the document as shown below. Like other files, a PDF document also has document properties. These properties are key-value pairs. Each property gives particular information about the document. This property holds the name of the file.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With