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

📄 pdfreader.java

📁 iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                
                recipients = (PdfArray)dic.get(PdfName.RECIPIENTS);                                    
            } else {
                cryptoMode = PdfWriter.STANDARD_ENCRYPTION_40;
                lengthValue = 40;                
                recipients = (PdfArray)enc.get(PdfName.RECIPIENTS);                
            }

            for (int i = 0; i<recipients.size(); i++)
            {
                PdfObject recipient = (PdfObject)recipients.getArrayList().get(i);
                strings.remove(recipient);
                
                CMSEnvelopedData data = null;
                try {
                    data = new CMSEnvelopedData(recipient.getBytes());
                
                    Iterator recipientCertificatesIt = 
                        data.getRecipientInfos().getRecipients().iterator();
            
                    while (recipientCertificatesIt.hasNext()) {
                        RecipientInformation recipientInfo = 
                            (RecipientInformation)recipientCertificatesIt.next();

                        if (recipientInfo.getRID().match(certificate) && !foundRecipient) {
    
                         envelopedData = recipientInfo.getContent(certificateKey, certificateKeyProvider);
                         foundRecipient = true;                         
                        }
                    }                        
                } catch (Exception f) {
                    throw new ExceptionConverter(f);
                }            
            }
            
            if(!foundRecipient || envelopedData == null)
            {
                throw new IOException("Bad certificate and key.");
            }            

            MessageDigest md = null;

            try {
                md = MessageDigest.getInstance("SHA-1");
                md.update(envelopedData, 0, 20);
                for (int i=0; i<recipients.size(); i++)
                {
                  byte[] encodedRecipient = ((PdfObject)recipients.getArrayList().get(i)).getBytes();  
                  md.update(encodedRecipient);
                }
                if ((cryptoMode & PdfWriter.DO_NOT_ENCRYPT_METADATA) != 0)
                    md.update(new byte[]{(byte)255, (byte)255, (byte)255, (byte)255});
                encryptionKey = md.digest();
                
            } catch (Exception f) {
                throw new ExceptionConverter(f);
            }
        }


        decrypt = new PdfEncryption();
        decrypt.setCryptoMode(cryptoMode, lengthValue);

        if (filter.equals(PdfName.STANDARD))
        {
            //check by owner password
            decrypt.setupByOwnerPassword(documentID, password, uValue, oValue, pValue);
            if (!equalsArray(uValue, decrypt.userKey, (rValue == 3 || rValue == 4) ? 16 : 32)) {
                //check by user password
                decrypt.setupByUserPassword(documentID, password, oValue, pValue);
                if (!equalsArray(uValue, decrypt.userKey, (rValue == 3 || rValue == 4) ? 16 : 32)) {
                    throw new IOException("Bad user password");
                }
            }
            else
                ownerPasswordUsed = true;
        } else if (filter.equals(PdfName.PUBSEC)) {   
            decrypt.setupByEncryptionKey(encryptionKey, lengthValue);
            ownerPasswordUsed = true;
        }
                 
        for (int k = 0; k < strings.size(); ++k) {
            PdfString str = (PdfString)strings.get(k);
            str.decrypt(this);
        }
        
        if (encDic.isIndirect()) {
            cryptoRef = (PRIndirectReference)encDic;
            xrefObj.set(cryptoRef.getNumber(), null);
        }
    }

    /**
     * @param obj
     * @return a PdfObject
     */
    public static PdfObject getPdfObjectRelease(PdfObject obj) {
        PdfObject obj2 = getPdfObject(obj);
        releaseLastXrefPartial(obj);
        return obj2;
    }


    /**
     * Reads a <CODE>PdfObject</CODE> resolving an indirect reference
     * if needed.
     * @param obj the <CODE>PdfObject</CODE> to read
     * @return the resolved <CODE>PdfObject</CODE>
     */
    public static PdfObject getPdfObject(PdfObject obj) {
        if (obj == null)
            return null;
        if (!obj.isIndirect())
            return obj;
        try {
            PRIndirectReference ref = (PRIndirectReference)obj;
            int idx = ref.getNumber();
            boolean appendable = ref.getReader().appendable;
            obj = ref.getReader().getPdfObject(idx);
            if (obj == null) {
                return null;
            }
            else {
                if (appendable) {
                    switch (obj.type()) {
                        case PdfObject.NULL:
                            obj = new PdfNull();
                            break;
                        case PdfObject.BOOLEAN:
                            obj = new PdfBoolean(((PdfBoolean)obj).booleanValue());
                            break;
                        case PdfObject.NAME:
                            obj = new PdfName(obj.getBytes());
                            break;
                    }
                    obj.setIndRef(ref);
                }
                return obj;
            }
        }
        catch (Exception e) {
            throw new ExceptionConverter(e);
        }
    }

    /**
     * Reads a <CODE>PdfObject</CODE> resolving an indirect reference
     * if needed. If the reader was opened in partial mode the object will be released
     * to save memory.
     * @param obj the <CODE>PdfObject</CODE> to read
     * @param parent
     * @return a PdfObject
     */
    public static PdfObject getPdfObjectRelease(PdfObject obj, PdfObject parent) {
        PdfObject obj2 = getPdfObject(obj, parent);
        releaseLastXrefPartial(obj);
        return obj2;
    }

    /**
     * @param obj
     * @param parent
     * @return a PdfObject
     */
    public static PdfObject getPdfObject(PdfObject obj, PdfObject parent) {
        if (obj == null)
            return null;
        if (!obj.isIndirect()) {
            PRIndirectReference ref = null;
            if (parent != null && (ref = parent.getIndRef()) != null && ref.getReader().isAppendable()) {
                switch (obj.type()) {
                    case PdfObject.NULL:
                        obj = new PdfNull();
                        break;
                    case PdfObject.BOOLEAN:
                        obj = new PdfBoolean(((PdfBoolean)obj).booleanValue());
                        break;
                    case PdfObject.NAME:
                        obj = new PdfName(obj.getBytes());
                        break;
                }
                obj.setIndRef(ref);
            }
            return obj;
        }
        return getPdfObject(obj);
    }

    /**
     * @param idx
     * @return a PdfObject
     */
    public PdfObject getPdfObjectRelease(int idx) {
        PdfObject obj = getPdfObject(idx);
        releaseLastXrefPartial();
        return obj;
    }

    /**
     * @param idx
     * @return aPdfObject
     */
    public PdfObject getPdfObject(int idx) {
        try {
            lastXrefPartial = -1;
            if (idx < 0 || idx >= xrefObj.size())
                return null;
            PdfObject obj = (PdfObject)xrefObj.get(idx);
            if (!partial || obj != null)
                return obj;
            if (idx * 2 >= xref.length)
                return null;
            obj = readSingleObject(idx);
            lastXrefPartial = -1;
            if (obj != null)
                lastXrefPartial = idx;
            return obj;
        }
        catch (Exception e) {
            throw new ExceptionConverter(e);
        }
    }

    /**
     *
     */
    public void resetLastXrefPartial() {
        lastXrefPartial = -1;
    }

    /**
     *
     */
    public void releaseLastXrefPartial() {
        if (partial && lastXrefPartial != -1) {
            xrefObj.set(lastXrefPartial, null);
            lastXrefPartial = -1;
        }
    }

    /**
     * @param obj
     */
    public static void releaseLastXrefPartial(PdfObject obj) {
        if (obj == null)
            return;
        if (!obj.isIndirect())
            return;
        PRIndirectReference ref = (PRIndirectReference)obj;
        PdfReader reader = ref.getReader();
        if (reader.partial && reader.lastXrefPartial != -1 && reader.lastXrefPartial == ref.getNumber()) {
            reader.xrefObj.set(reader.lastXrefPartial, null);
        }
        reader.lastXrefPartial = -1;
    }

    private void setXrefPartialObject(int idx, PdfObject obj) {
        if (!partial || idx < 0)
            return;
        xrefObj.set(idx, obj);
    }

    /**
     * @param obj
     * @return an indirect reference
     */
    public PRIndirectReference addPdfObject(PdfObject obj) {
        xrefObj.add(obj);
        return new PRIndirectReference(this, xrefObj.size() - 1);
    }

    protected void readPages() throws IOException {
        catalog = (PdfDictionary)getPdfObject(trailer.get(PdfName.ROOT));
        rootPages = (PdfDictionary)getPdfObject(catalog.get(PdfName.PAGES));
        pageRefs = new PageRefs(this);
    }

    protected void readDocObjPartial() throws IOException {
        xrefObj = new ArrayList(xref.length / 2);
        xrefObj.addAll(Collections.nCopies(xref.length / 2, null));
        readDecryptedDocObj();
        if (objStmToOffset != null) {
            int keys[] = objStmToOffset.getKeys();
            for (int k = 0; k < keys.length; ++k) {
                int n = keys[k];
                objStmToOffset.put(n, xref[n * 2]);
                xref[n * 2] = -1;
            }
        }
    }

    protected PdfObject readSingleObject(int k) throws IOException {
        strings.clear();
        int k2 = k * 2;
        int pos = xref[k2];
        if (pos < 0)
            return null;
        if (xref[k2 + 1] > 0)
            pos = objStmToOffset.get(xref[k2 + 1]);
        if (pos == 0)
            return null;
        tokens.seek(pos);
        tokens.nextValidToken();
        if (tokens.getTokenType() != PRTokeniser.TK_NUMBER)
            tokens.throwError("Invalid object number.");
        objNum = tokens.intValue();
        tokens.nextValidToken();
        if (tokens.getTokenType() != PRTokeniser.TK_NUMBER)
            tokens.throwError("Invalid generation number.");
        objGen = tokens.intValue();
        tokens.nextValidToken();
        if (!tokens.getStringValue().equals("obj"))
            tokens.throwError("Token 'obj' expected.");
        PdfObject obj;
        try {
            obj = readPRObject();
            for (int j = 0; j < strings.size(); ++j) {
                PdfString str = (PdfString)strings.get(j);
                str.decrypt(this);
            }
            if (obj.isStream()) {
                checkPRStreamLength((PRStream)obj);
            }
        }
        catch (Exception e) {
            obj = null;
        }
        if (xref[k2 + 1] > 0) {
            obj = readOneObjStm((PRStream)obj, xref[k2]);
        }
        xrefObj.set(k, obj);
        return obj;
    }

    protected PdfObject readOneObjStm(PRStream stream, int idx) throws IOException {
        int first = ((PdfNumber)getPdfObject(stream.get(PdfName.FIRST))).intValue();
        byte b[] = getStreamBytes(stream, tokens.getFile());
        PRTokeniser saveTokens = tokens;
        tokens = new PRTokeniser(b);
        try {
            int address = 0;
            boolean ok = true;

⌨️ 快捷键说明

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