⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pdfimage.java

📁 一个java操作pdf文件的开发包,很好用的.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                            }                                                        int compressionMethod = is.read();                            int filterMethod = is.read();                                                        int interlaceMethod = is.read();                            if (interlaceMethod != 0) {                                throw new BadPdfFormatException(errorID + " Interlace method " + interlaceMethod + " is not suported.");                            }                                                        PdfDictionary decodeparms = new PdfDictionary();                            decodeparms.put(PdfName.BITSPERCOMPONENT, new PdfNumber(bitDepth));                            decodeparms.put(PdfName.PREDICTOR, new PdfNumber(15));                            decodeparms.put(PdfName.COLUMNS, new PdfNumber(w));                            decodeparms.put(PdfName.COLORS, new PdfNumber((colorType == 2) ? 3: 1));                            put(PdfName.DECODEPARMS, decodeparms);                                                        Png.getInt(is);                        }                        else if (Png.PLTE.equals(marker)) {                            if (colorType == 3) {                                PdfArray colorspace = new PdfArray();                                colorspace.add(PdfName.INDEXED);                                colorspace.add(PdfName.DEVICERGB);                                colorspace.add(new PdfNumber(len / 3 - 1));                                ByteBuffer colortable = new ByteBuffer();                                while ((len--) > 0) {                                    colortable.append_i(is.read());                                }                                colorspace.add(new PdfString(colortable.toByteArray()));                                put(PdfName.COLORSPACE, colorspace);                                Png.getInt(is);                            }                            else {                                Image.skip(is, len + 4);                            }                        }                        else if (Png.IEND.equals(marker)) {                            break;                        }                        else {                            for (int j = -4; j < len; j++) {                                is.read();                            }                        }                                            }                    break;                case Image.JPEG:                    put(PdfName.FILTER, PdfName.DCTDECODE);                    switch(image.colorspace()) {                        case 1:                            put(PdfName.COLORSPACE, PdfName.DEVICEGRAY);                            break;                        case 3:                            put(PdfName.COLORSPACE, PdfName.DEVICERGB);                            break;                        default:                            put(PdfName.COLORSPACE, PdfName.DEVICECMYK);                            if (image.isInvertedJPEG()) {                                put(PdfName.DECODE, new PdfLiteral("[1 0 1 0 1 0 1 0]"));                            }                    }                    put(PdfName.BITSPERCOMPONENT, new PdfNumber(8));                    if (image.rawData() != null){                        bytes = image.rawData();                        streamBytes = null;                        put(PdfName.LENGTH, new PdfNumber(bytes.length));                        return;                    }                    transferBytes(is, streamBytes, -1);                    break;                case Image.GIF:                    // HEADER + INFO + COLORTABLE                                        // Byte 0-2: header                    // checks if the file really is a GIF-file                    if (is.read() != 'G' || is.read() != 'I' || is.read() != 'F') {                        throw new BadPdfFormatException(errorID + " is not a GIF-file (GIF header not found).");                    }                                        put(PdfName.FILTER, PdfName.LZWDECODE);                                        PdfDictionary decodeparms = new PdfDictionary();                    decodeparms.put(PdfName.EARLYCHANGE, new PdfNumber(0));                    put(PdfName.DECODEPARMS, decodeparms);                                        PdfArray colorspace = new PdfArray();                    colorspace.add(PdfName.INDEXED);                    colorspace.add(PdfName.DEVICERGB);                    // Byte 3-5: version                    // Byte 6-7: logical screen width                    // Byte 8-9: logical screen height                    // Byte 10: Packed Fields                    for (int j = 0; j < 8; j++) {                        i = is.read();                    }                    // Byte 10: bit 1: Global Color Table Flag                    if ((i & 0x80) == 0) {                        throw new BadPdfFormatException(errorID + " is not a supported GIF-file (there is no global color table present).");                    }                    // Byte 10: bit 6-8: Size of Global Color Table                    int nColors = 1 << ((i & 7) + 1);                    colorspace.add(new PdfNumber(nColors - 1));                    // Byte 11: Background color index                    is.read();                    // Byte 12: Pixel aspect ratio                    is.read();                    // Byte 13-...: Global color table                    ByteBuffer colortable = new ByteBuffer();                    for (int j = 0; j < nColors; j++) {                        colortable.append_i(is.read());	// red                        colortable.append_i(is.read());	// green                        colortable.append_i(is.read());	// blue                    }                    colorspace.add(new PdfString(colortable.toByteArray()));                    put(PdfName.COLORSPACE, colorspace);                    put(PdfName.BITSPERCOMPONENT, new PdfNumber(8));                                        // IMAGE DESCRIPTOR                                        // Byte 0: Image separator                    // only simple gif files with image immediate following global color table are supported                    // 0x2c is a fixed value for the image separator                    if (is.read() != 0x2c) {                        throw new BadPdfFormatException(errorID + " is not a supported GIF-file (the image separator '0x2c' is not found after reading the color table).");                    }                    // Byte 1-2: Image Left Position                    // Byte 3-4: Image Top Position                    // Byte 5-6: Image Width                    // Byte 7-8: Image Height                    // ignore position and size                    for (int j = 0; j < 8; j++) {                        is.read();                    }                    // Byte 9: Packed Fields                    // Byte 9: bit 1: Local Color Table Flag                    // Byte 9: bit 2: Interlace Flag                    if ((is.read() & 0xc0) > 0) {                        throw new BadPdfFormatException(errorID + " is not a supported GIF-file (interlaced gifs or gifs using local color table can't be inserted).");                    }                                        // Byte 10: LZW initial code                    if (is.read() != 0x08) {                        throw new BadPdfFormatException(errorID + " is not a supported GIF-file (initial LZW code not supported).");                    }                    // Read the Image Data                    int code = 0;                    int codelength = 9;                    int tablelength = 257;                    int bitsread = 0;                    int bitstowrite = 0;                    int bitsdone = 0;                    int bitsleft = 23;                    int bytesdone = 0;                    int bytesread = 0;                    int byteswritten = 0;                    // read the size of the first Data Block                    int size = is.read();                    // Check if there is any data in the GIF                    if (size < 1) {                        throw new BadPdfFormatException(errorID + " is not a supported GIF-file. (no image data found).");                    }                    // if possible, we read the first 24 bits of data                    size--; bytesread++; bitsread = is.read();                    if (size > 0) {                        size--; bytesread++; bitsread += (is.read() << 8);                        if (size > 0) {                            size--; bytesread++; bitsread += (is.read() << 16);                        }                    }                    while (bytesread > byteswritten) {                        tablelength++;                        // we extract a code with length=codelength                        code = (bitsread >> bitsdone) & ((1 << codelength) - 1);                        // we delete the bytesdone in bitsread and append the next byte(s)                        bytesdone = (bitsdone + codelength) / 8;                        bitsdone = (bitsdone + codelength) % 8;                        while (bytesdone > 0) {                            bytesdone--;                            bitsread = (bitsread >> 8);                            if (size > 0) {                                size--; bytesread++; bitsread += (is.read() << 16);                            }                            else {                                size = is.read();                                if (size > 0) {                                    size--; bytesread++; bitsread += (is.read() << 16);                                }                            }                        }                        // we package all the bits that are done into bytes and write them to the stream                        bitstowrite += (code << (bitsleft - codelength + 1));                        bitsleft -= codelength;                        while (bitsleft < 16) {                            streamBytes.write(bitstowrite >> 16);                            byteswritten++;                            bitstowrite = (bitstowrite & 0xFFFF) << 8;                            bitsleft += 8;                        }                        if (code == 256) {                            codelength = 9;                            tablelength = 257;                        }                        if (code == 257) {                            break;                        }                        if (tablelength == (1 << codelength)) {                            codelength++;                        }                    }                    if (bytesread - byteswritten > 2) {                        throw new BadPdfFormatException(errorID + " is not a supported GIF-file (unexpected end of data block).");                    }                    break;                default:                    throw new BadPdfFormatException(errorID + " is an unknown Image format.");            }            put(PdfName.LENGTH, new PdfNumber(streamBytes.size()));        }        catch(IOException ioe) {            throw new BadPdfFormatException(ioe.getMessage());        }        finally {            if (is != null) {                try{                    is.close();                }                catch (Exception ee) {                    // empty on purpose                }            }        }    }        /**     * Returns the <CODE>PdfName</CODE> of the image.     *     * @return		the name     */        public PdfName name() {        return name;    }        static void transferBytes(InputStream in, OutputStream out, int len) throws IOException {        byte buffer[] = new byte[TRANSFERSIZE];        if (len < 0)            len = 0x7ffffff;        int size;        while (len != 0) {            size = in.read(buffer, 0, Math.min(len, TRANSFERSIZE));            if (size < 0)                return;            out.write(buffer, 0, size);            len -= size;        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -