使用PDFBox替换图像显示乱码图像

时间:2018-06-11 04:03:03

标签: java pdfbox

我使用PDFBox 2.0.8替换pdf文档中的图像。我的过程包括从文档中提取图像并写入文件系统。压缩已提取的图像。压缩图像需要用原始图像替换。但是,替换后PDF显示乱码图像。请参阅链接中的第1页 https://35.200.235.243/compression/download?fileName=/outbox/pdf/test123/test1-compress.pdf。如果我将在Chrome中打开此文档,那么它似乎会更好。但是,在桌面上它是乱码。在chrome中,背景颜色似乎已经改变。但是,此对象中没有遮罩

for (COSName xObjectName : resources.getXObjectNames()) {
                    PDXObject xObject = resources.getXObject(xObjectName);
                    if (xObject instanceof PDImageXObject) {

                        imageCounter++;
                        System.out.println("cosname="+xObjectName.toString());
                        PDImageXObject original_img = ((PDImageXObject) xObject);
                 String suffix = original_img.getSuffix();
                        File imageFile = new File(destPath+"/"+"image"+imageCounter+"."+suffix);
                        int imageWidth = original_img.getWidth();
                        int imageHeight = original_img.getHeight();
                        BufferedImage bImage = new BufferedImage(imageWidth,imageHeight,BufferedImage.TYPE_INT_RGB);
                        bImage = original_img.getOpaqueImage();
                        ImageIO.write(bImage,suffix, imageFile);
                if(suffix.equalsIgnoreCase("jpg") || suffix.equalsIgnoreCase("jpeg")) {

                            System.out.println("found jpg="+imageFile.getAbsolutePath());
                            CompressPDFJPEG compress    =   new CompressPDFJPEG(imageFile.getAbsolutePath(), directoryID);
                            HashMap compressedFilesMap  =   compress.getCompressedFiles();
                            String compressedFiles []   =   (String[]) compressedFilesMap.get("compressedFiles");
                            String replaceImageFile     =   "";
                            for (int i=0;i<compressedFiles.length;i++) {

                                if (compressedFiles[i] != null) {
                                    replaceImageFile = compressedFiles[i];
                                PDImageXObject replacement_img = PDImageXObject.createFromFile(replaceImageFile, doc);
                                resources.put(xObjectName, replacement_img);
                                    doc.save(outFileName);
                                break;
                                }

                            }
                        }

                        if(suffix.equalsIgnoreCase("png")) {

                            CompressPDFPNG compress     =   new CompressPDFPNG(imageFile.getAbsolutePath(), directoryID);
                            HashMap compressedFilesMap  =   compress.compressPNG();
                            String compressedFiles []   =   (String[]) compressedFilesMap.get("compressedFiles");
                            String replaceImageFile     =   "";
                            for (int i=0;i<compressedFiles.length;i++) {

                                if (compressedFiles[i] != null) {
                                    replaceImageFile = compressedFiles[i];
                                    PDImageXObject replacement_img = PDImageXObject.createFromFile(replaceImageFile, doc);
                                        resources.put(xObjectName, replacement_img);
                                    break;
                                }

                            }


                        }


                    }        
                }
            }

     doc.close();

以下是不同的链接 原始PDF: https://35.200.235.243/compression/download?fileName=/inbox/pdf/test1.pdf 提取的图片: https://35.200.235.243/compression/download?fileName=/work/pdf/test123/image2.jpg

压缩图片: https://35.200.235.243/compression/download?fileName=/work/pdf/test123/image2-compress1.jpg 压缩PDF: https://35.200.235.243/compression/download?fileName=/outbox/pdf/test123/test1-compress.pdf

桌面屏幕截图(压缩PDF):

0 个答案:

没有答案