Exemplo n.º 1
0
void encrypt( const char* pszInput, const char* pszOutput, 
              const std::string & userPass, const std::string & ownerPass,
              const PdfEncrypt::EPdfEncryptAlgorithm eAlgorithm, const int nPermissions ) 
{
    PdfVecObjects objects;
    PdfParser     parser( &objects );
    
    objects.SetAutoDelete( true );
    parser.ParseFile( pszInput );
    
    PdfEncrypt::EPdfKeyLength eKeyLength;
    EPdfVersion   eVersion;
    switch( eAlgorithm ) 
    {
        case PdfEncrypt::ePdfEncryptAlgorithm_RC4V1:
            eKeyLength = PdfEncrypt::ePdfKeyLength_40;
            eVersion   = ePdfVersion_1_3;
            break;
#ifdef PODOFO_HAVE_CRYPTO_LIBS
        case PdfEncrypt::ePdfEncryptAlgorithm_AESV3:;
            eKeyLength = PdfEncrypt::ePdfKeyLength_256;
            eVersion   = ePdfVersion_1_7;
            break;
#endif // PODOFO_HAVE_CRYPTO_LIBS
        case PdfEncrypt::ePdfEncryptAlgorithm_RC4V2:
        case PdfEncrypt::ePdfEncryptAlgorithm_AESV2:
        default:
            eKeyLength = PdfEncrypt::ePdfKeyLength_128;
            eVersion   = ePdfVersion_1_5;
            break;
    }

    PdfWriter writer( &parser );
	PdfEncrypt *encrypt = PdfEncrypt::CreatePdfEncrypt( userPass, ownerPass, nPermissions,
                        eAlgorithm, eKeyLength  );
    
    writer.SetPdfVersion( eVersion );
    writer.SetEncrypted( *encrypt );
    writer.Write( pszOutput );

	delete encrypt;
}
Exemplo n.º 2
0
int main (int argc, char *argv[])
{
    PdfError::EnableLogging(true);
    PdfError::EnableDebug(true);

    PdfVecObjects objects;
    PdfParser     parser( &objects );
    objects.SetAutoDelete( true );

    if( argc != 3 )
    {
        cerr << "Usage: podofogc <input_filename> <output_filename>\n"
             << "    Performs garbage collection on a PDF file.\n"
             << "    All objects that are not reachable from within\n"
             << "    the trailer are deleted.\n"
             << flush;
        return 0;
    }
    

    try {
        cerr << "Parsing  " << argv[1] << " ... (this might take a while)"
             << flush;

        bool bIncorrectPw = false;
        std::string pw;
        do {
            try {
                if( !bIncorrectPw ) 
                    parser.ParseFile( argv[1], false );
                else 
                    parser.SetPassword( pw );
                
                bIncorrectPw = false;
            } catch( PdfError & e ) {
                if( e.GetError() == ePdfError_InvalidPassword ) 
                {
                    cout << endl << "Password :"******" done" << endl;

        cerr << "Writing..." << flush;
        PdfWriter writer( &parser );
        writer.SetPdfVersion( parser.GetPdfVersion() );
        if( parser.GetEncrypted() )
        {
            writer.SetEncrypted( *(parser.GetEncrypt()) );
        }
        writer.Write( argv[2] );
        cerr << " done" << endl;
    } catch( PdfError & e ) {
        e.PrintErrorMsg();
        return e.GetError();
    }

    cerr << "Parsed and wrote successfully" << endl;
	return EXIT_SUCCESS;
}