Package org.pdfbox.cos

Examples of org.pdfbox.cos.COSDocument


   * The method gets the version of the COSDocument of the PDDocument.
   *
   * @return The version of the tested pdf file.
   */
  public float getVersion() {
    final COSDocument cosDoc = pdDocument.getDocument();
    return cosDoc.getVersion();
  }
View Full Code Here


        }
      }
 
      PDDocumentInformation info = doc.getDocumentInformation();
      PDDocumentCatalog catlog = doc.getDocumentCatalog();
      COSDocument cosDoc = doc.getDocument();   
     
      ctx.fireStartParseEvent("pdf-meta");
 
      COSArray array = cosDoc.getDocumentID();
      if(array != null && array.size() == 2) {
        ctx.fireParseEvent("doc-id", ((COSString) array.get(0)).getHexString());
        ctx.fireParseEvent("iteration-id", ((COSString) array.get(1)).getHexString());
      }
     
      if(array == null || array.size() != 2) {
        ctx.fireParseEvent("original", "unknown");
      }
      else {
        boolean orig = ((COSString) array.get(0)).getHexString().equals(((COSString) array.get(1)).getHexString());
        ctx.fireParseEvent("original", orig);
      }
     
      fireSpecialNull(ctx, "title", info.getTitle());
      fireSpecialNull(ctx, "language", catlog.getLanguage());
      fireSpecialNull(ctx, "author", info.getAuthor());
      fireSpecialNull(ctx, "creator", info.getCreator());
      fireSpecialNull(ctx, "subject", info.getSubject());
      fireSpecialNull(ctx, "producer", info.getProducer());
      fireSpecialNull(ctx, "keywords", info.getKeywords());   
     
      ctx.fireStartParseEvent("creation-date");
      fireDate(ctx, info.getCreationDate());
      ctx.fireEndParseEvent("creation-date");
     
      ctx.fireStartParseEvent("modified-date");
      fireDate(ctx, info.getModificationDate());
      ctx.fireEndParseEvent("modified-date");
     
      ctx.fireParseEvent("has-forms", catlog.getAcroForm() != null);
      ctx.fireParseEvent("has-metadata-stream", catlog.getMetadata() != null);
      ctx.fireParseEvent("has-outline", catlog.getDocumentOutline() != null);
      ctx.fireParseEvent("has-threads", catlog.getThreads().size() > 0);
      ctx.fireParseEvent("tagged", catlog.getMarkInfo() != null);
      fireSpecialNull(ctx, "page-layout", catlog.getPageLayout());
      fireSpecialNull(ctx, "page-mode", catlog.getPageMode());
      fireSpecialNull(ctx, "trapped", info.getTrapped());
     
      fireSpecialNull(ctx, "version", Float.toString(cosDoc.getVersion()));
     
      ctx.fireStartParseEvent("security");
      ctx.fireParseEvent("encrypted", encrypted);
      if (encrypted) {
        PDEncryptionDictionary encDict = doc.getEncryptionDictionary();
View Full Code Here

     * @throws COSVisitorException If there is an error generating the PDF document.
     */
    public void doIt( String file, String message) throws IOException, COSVisitorException
    {
        // the document
        COSDocument doc = null;
        OutputStream os = null;
        COSWriter writer = null;
        try
        {
            doc = new COSDocument();

            // the pages dict
            COSDictionary pages = new COSDictionary();
            pages.setItem(COSName.TYPE, COSName.PAGES);

            // the pagesarray
            COSArray pagesArray = new COSArray();

            // a page
            COSDictionary page = new COSDictionary();
            page.setItem(COSName.TYPE, COSName.PAGE);
            page.setItem(COSName.PARENT, doc.createObject(pages));
            COSArray mediaBox = new COSArray();
            mediaBox.add( new COSInteger(0));
            mediaBox.add( new COSInteger(0));
            mediaBox.add( new COSInteger(612));
            mediaBox.add( new COSInteger(792));
            page.setItem(COSName.getPDFName("MediaBox"), mediaBox);
            byte[] bytes = ("BT /F1 24 Tf 100 100 Td (" + message + ") Tj ET").getBytes();
            COSDictionary streamDict = new COSDictionary();
            streamDict.setItem(COSName.LENGTH, new COSInteger(bytes.length));
            COSStream contents = new COSStream(streamDict,doc.getScratchFile());
            OutputStream output = contents.createUnfilteredStream();
            output.write(bytes);
            output.close();
            page.setItem(COSName.CONTENTS, doc.createObject(contents));
            COSDictionary resources = new COSDictionary();
            // the procset
            COSArray procSet = new COSArray();
            procSet.add(COSName.getPDFName("PDF"));
            procSet.add(COSName.getPDFName("Text"));
            resources.setItem(COSName.getPDFName("ProcSet"), doc.createObject(procSet));
            // the font
            COSDictionary font = new COSDictionary();
            font.setItem(COSName.TYPE, COSName.FONT);
            font.setItem(COSName.SUBTYPE, COSName.getPDFName("Type1"));
            font.setItem(COSName.getPDFName("Name"), COSName.getPDFName("F1"));
            font.setItem(COSName.getPDFName("BaseFont"), COSName.getPDFName("Helvetica"));
            COSDictionary fontDict = new COSDictionary();
            fontDict.setItem(COSName.getPDFName("F1"), doc.createObject(font));
            resources.setItem(COSName.getPDFName("Font"), fontDict);
            page.setItem(COSName.RESOURCES, resources);

            // now add the page
            pagesArray.add(doc.createObject(page));
            pages.setItem(COSName.KIDS, pagesArray);
            pages.setItem(COSName.COUNT, new COSInteger(pagesArray.size()));

            // the catalog dict
            COSDictionary catalog = new COSDictionary();
            catalog.setItem(COSName.TYPE, COSName.CATALOG);
            catalog.setItem(COSName.PAGES, doc.createObject(pages));
            doc.createObject(catalog);

            //The document trailer
            COSDictionary trailer = new COSDictionary();
            trailer.setItem( COSName.ROOT, catalog );

            doc.setTrailer( trailer );

            os = new FileOutputStream( file );
            writer = new COSWriter(os);

            writer.write(doc);
        }
        finally
        {
            doc.close();
            os.close();
            writer.close();
        }
    }
View Full Code Here

     * @throws COSVisitorException If there is an error generating the PDF document.
     */
    public void doIt(String in, String out, String name, String value) throws IOException, COSVisitorException
    {
        java.io.InputStream is = null;
        COSDocument doc = null;
        OutputStream os = null;
        COSWriter writer = null;
        try
        {
            is = new java.io.FileInputStream(in);
            PDFParser parser = new PDFParser(is);
            parser.parse();

            doc = parser.getDocument();

            setField(doc, new COSString(name), new COSString(value));

            os = new FileOutputStream(out);
            writer = new COSWriter(os);

            writer.write(doc);

        }
        finally
        {
            is.close();
            doc.close();
            os.close();
            writer.close();
        }
    }
View Full Code Here

        {
            is = new java.io.FileInputStream(in);
            PDFParser parser = new PDFParser(is);
            parser.parse();

            COSDocument doc = parser.getDocument();

            os = new java.io.FileOutputStream(out);
            writer = new COSWriter(os);

            writer.write(doc);
View Full Code Here

     * @throws COSVisitorException If there is an error while copying the document.
     */
    public void doIt(String in, String out) throws IOException, COSVisitorException
    {
        java.io.InputStream is = null;
        COSDocument doc = null;
        COSWriter writer = null;
        java.io.OutputStream os = null;
        try
        {
            is = new java.io.FileInputStream(in);
            PDFParser parser = new PDFParser(is);
            parser.parse();

            doc = parser.getDocument();
            if( doc.isEncrypted() )
            {
                try
                {
                    DecryptDocument decryptor = new DecryptDocument( doc );
                    decryptor.decryptDocument( "" );
                }
                catch( InvalidPasswordException e )
                {
                    System.err.println( "Error: The document is encrypted." );
                }
                catch( org.pdfbox.exceptions.CryptographyException e )
                {
                    e.printStackTrace();
                }
            }

            for (Iterator i = doc.getObjects().iterator(); i.hasNext();)
            {
                COSBase base = ((COSObject) i.next()).getObject();
                if (base instanceof COSStream)
                {
                    // just kill the filters
                    COSStream cosStream = (COSStream)base;
                    cosStream.getUnfilteredStream();
                    cosStream.setFilters(null);
                }
            }

            os = new java.io.FileOutputStream(out);
            writer = new COSWriter(os);

            writer.write(doc);
        }
        finally
        {
            if( is != null )
            {
                is.close();
            }
            if( doc != null )
            {
                doc.close();
            }
            if( writer != null )
            {
                writer.close();
            }
View Full Code Here

        else
        {
            String password = args[0];
            String infile = args[1];
            InputStream is = null;
            COSDocument document = null;;
            try
            {
                is = new FileInputStream( infile );
                PDFParser parser = new PDFParser( is );
                parser.parse();
                document = parser.getDocument();

                if( document.isEncrypted() )
                {
                    DecryptDocument decryptor = new DecryptDocument( document );
                    decryptor.decryptDocument( password );
                }
                else
                {
                    System.err.println( "Warning: Document is not encrypted." );
                }

                COSDictionary trailer = document.getTrailer();
                COSDictionary root = (COSDictionary)trailer.getDictionaryObject( COSName.ROOT );
                COSDictionary acroForm = (COSDictionary)root.getDictionaryObject( COSName.getPDFName( "AcroForm" ) );
                COSArray fields = (COSArray)acroForm.getDictionaryObject( COSName.getPDFName( "Fields" ) );
                for( int i=0; i<fields.size(); i++ )
                {
                    COSDictionary field = (COSDictionary)fields.getObject( i );
                    COSName type = (COSName)field.getDictionaryObject( COSName.getPDFName( "FT" ) );
                    if( type != null && type.getName().equals( "Sig" ) )
                    {
                        COSDictionary cert = (COSDictionary)field.getDictionaryObject( COSName.getPDFName( "V" ) );
                        System.out.println( "Certificate found" );
                        System.out.println( "Name=" + cert.getDictionaryObject( COSName.getPDFName( "Name" ) ) );
                        System.out.println( "Modified=" + cert.getDictionaryObject( COSName.getPDFName( "M" ) ) );
                        COSName subFilter = (COSName)cert.getDictionaryObject( COSName.getPDFName( "SubFilter" ) );
                        if( subFilter != null )
                        {
                            if( subFilter.getName().equals( "adbe.x509.rsa_sha1" ) )
                            {
                                COSString certString = (COSString)cert.getDictionaryObject( COSName.getPDFName( "Cert" ) );
                                byte[] certData = certString.getBytes();
                                CertificateFactory factory = CertificateFactory.getInstance( "X.509" );
                                Collection certs = factory.generateCertificates( new ByteArrayInputStream( certData ) );
                                System.out.println( "certs=" + certs );
                            }
                            else
                            {
                            }
                        }
                        else
                        {
                            throw new IOException( "Missing subfilter for cert dictionary" );
                        }
                    }
                }
            }
            finally
            {
                is.close();
                document.close();
            }
        }
    }
View Full Code Here

     */
    public void parse() throws IOException
    {
        try
        {
            document = new COSDocument( tempDirectory );

            String header = readLine();
            if( log.isDebugEnabled() )
            {
                log.debug( "Header=" + header );
View Full Code Here

     * @throws COSVisitorException If there is an error generating the PDF document.
     */
    public void doIt(String in1, String in2, String out) throws IOException, COSVisitorException
    {
        InputStream is1 = null;
        COSDocument doc1 = null;

        InputStream is2 = null;
        COSDocument doc2 = null;
        OutputStream os = null;
        COSWriter writer = null;
        try
        {
            is1 = new FileInputStream(in1);
View Full Code Here

                      String name1,
                      String value1,
                      String name2,
                      String value2) throws IOException, COSVisitorException
    {
        COSDocument doc1 = null;
        COSDocument doc2 = null;
        InputStream is1 = null;
        InputStream is2 = null;
        PDFParser parser1 = null;
        PDFParser parser2 = null;

        OutputStream os = null;
        COSWriter writer = null;
        try
        {
            is1 = new FileInputStream(in1);
            parser1 = new PDFParser(is1);
            parser1.parse();
            doc1 = parser1.getDocument();

            is2 = new FileInputStream(in2);
            parser2 = new PDFParser(is2);
            parser2.parse();
            doc2 = parser2.getDocument();

            setField(doc1, "doc1", new COSString(name1), new COSString(value1));
            setField(doc2, "doc2", new COSString(name2), new COSString(value2));

            appendDocument(doc1, doc2);

            os = new FileOutputStream(out);
            writer = new COSWriter(os);
            writer.write(doc1);
        }
        finally
        {
            is1.close();
            doc1.close();

            is2.close();
            doc2.close();

            os.close();
            writer.close();
        }
    }
View Full Code Here

TOP

Related Classes of org.pdfbox.cos.COSDocument

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.