pdfbox错误 - 此时无法确定色彩空间,即将从未处理的分支

时间:2015-05-05 05:58:44

标签: pdfbox

我正在使用以下代码获取附加到我的一个对象的jpeg列表" PrintDocument"它引用了一个" Document"它引用了一个" Page"宾语。路径由Document和Page组合而成。但这不是问题。当我使用这段代码并在jvm上的简单java中运行时,pdf正在构建而没有任何错误......

    public File createPDFtoPrint(PrintDocument pdocument)
        throws IncompleteDocumentException, IOException, FSException {
    File tmp = null;
    if(validatePrintDocument(pdocument)){

        PDDocument document = new PDDocument();
        Iterator<Page> iterator = pdocument.getDocument().getPageList().iterator();

        while(iterator.hasNext()){
            Page page = iterator.next();
            PDPage pdpage = new PDPage(PDPage.PAGE_SIZE_A4);
            document.addPage( pdpage );

            PDPageContentStream contentStream = null;
            try {
                contentStream = new PDPageContentStream(document, pdpage);
            } catch (IOException e) {
                String message = "Error while creating contentStream for Page" + page.getPageNumber();
                logger.error(message);
                logger.debug(e.toString());
            }
            PDJpeg ximage;
            try {
                File imgFile = new File(docDir+"/"+pdocument.getDocument().getName()+"/"+page.getPageNumber()+".jpg");
                System.out.println("*************** FILEPATH IS "+imgFile.getPath());
                ximage = new PDJpeg(document,new FileInputStream(imgFile));
            } catch (FileNotFoundException e) {
                String message = "File not found for Document " + pdocument.getDocument().getName() + "! Try uploading the PDF for this product!";
                logger.error(message);
                throw new FileNotFoundException(message);
            } catch (IOException e) {
                String message = "Error while reading input file for document " + pdocument.getDocument().getName();
                logger.error(message);
                throw new IOException(message);             }

            if(ximage.getWidth()>ximage.getHeight()){
                AffineTransform at = new AffineTransform(pdpage.getMediaBox().getWidth(), 0, 0, pdpage.getMediaBox().getHeight(), pdpage.getMediaBox().getWidth(), 0);
                at.rotate(Math.toRadians(90));
                contentStream.drawXObject(ximage,at);
            }
            else{
                AffineTransform at = new AffineTransform(pdpage.getMediaBox().getWidth(), 0, 0, pdpage.getMediaBox().getHeight(), 0, 0);
                contentStream.drawXObject(ximage,at);
            }

            PDFont font = PDType1Font.HELVETICA_BOLD;

            //Pixel per Point
            float ppp = page.getPpp();

            Iterator<Field> cbIterator = pdocument.getCheckBoxMap().keySet().iterator();

            while(cbIterator.hasNext()){
                Field field = cbIterator.next();
                if(pdocument.getCheckBoxMap().get(field)){
                    contentStream.beginText();
                    contentStream.setFont( font, 14 );
                    contentStream.moveTextPositionByAmount(field.getPosx()/ppp, field.getPosy()/ppp );
                    contentStream.drawString("x");
                    contentStream.endText();
                }
            }

但是,当我在Glassfish容器中的EJB中使用它时,我得到以下错误,页面是空白的:

[2015-05-05T01:57:59.739+0200] [glassfish 4.0] [INFO] [] [] [tid: _ThreadID=22 _ThreadName=Thread-3] [timeMillis: 1430783879739] [levelValue: 800] [[2015-05-05 01:57:59 DEBUG PDXObjectImage:398 - Colorspace can't be determined at this time, about to return NULL from unhandled branch. filter = COSName{DCTDecode}]]

[2015-05-05T01:57:59.740+0200] [glassfish 4.0] [INFO] [] [] [tid: _ThreadID=22 _ThreadName=Thread-3] [timeMillis: 1430783879740] [levelValue: 800] [[2015-05-05 01:57:59 DEBUG PDXObjectImage:400 - Can happen e.g. when constructing PDJpeg from ImageStream]]

有没有人知道为什么会发生这种情况以及如何解决这个问题?

此致

1 个答案:

答案 0 :(得分:0)

我的问题是,我生成的PDF似乎是空的。全白。但是主题中提到的错误与此无关。看来这是一种没有人的错误,那并不是真正关心色彩空间应该关心的问题。错误,为什么我的文档似乎是空的是我在完成页面后没有关闭ContentStream。所以ContentStream #close解决了我的问题。