Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PDFBox Support Spot Colors and Separations?

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.

like image 858
Joe Bane Avatar asked May 04 '12 21:05

Joe Bane


People also ask

How do I use PDFBox?

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.

What is Apache PDFBox?

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.

What is the difference between PDFBox and fontbox?

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.

What are the document properties of a PDF file?

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.


1 Answers

Please see as below

enter image description here

  1. 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;
        }
    
  2. 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 :)

like image 171
sam.zeng Avatar answered Sep 20 '22 22:09

sam.zeng