| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2011 by Dominik Seichter * |
| 3 | * [email protected] * |
| 4 | * by Petr Pytelka * |
| 5 | * * |
| 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU Library General Public License as * |
| 8 | * published by the Free Software Foundation; either version 2 of the * |
| 9 | * License, or (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU Library General Public * |
| 17 | * License along with this program; if not, write to the * |
| 18 | * Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | #include "../../src/podofo.h" |
| 23 | |
| 24 | #include "../PdfTest.h" |
| 25 | |
| 26 | #include <iostream> |
| 27 | #include <cstdio> |
| 28 | #include <fstream> |
| 29 | |
| 30 | using namespace PoDoFo; |
| 31 | |
| 32 | |
| 33 | |
| 34 | #define CONVERSION_CONSTANT 0.002834645669291339 |
| 35 | |
| 36 | void CreateSimpleForm( PdfPage* pPage, PdfStreamedDocument* pDoc, const PdfData &signatureData ) |
| 37 | { |
| 38 | PdfPainter painter; |
| 39 | PdfFont* pFont = pDoc->CreateFont( "Courier" ); |
| 40 | |
| 41 | painter.SetPage( pPage ); |
| 42 | painter.SetFont( pFont ); |
| 43 | painter.DrawText( 10000 * CONVERSION_CONSTANT, 280000 * CONVERSION_CONSTANT, "PoDoFo Sign Test" ); |
| 44 | painter.FinishPage(); |
| 45 | |
| 46 | PdfSignatureField signField( pPage, PdfRect( 70000 * CONVERSION_CONSTANT, 10000 * CONVERSION_CONSTANT, |
| 47 | 50000 * CONVERSION_CONSTANT, 50000 * CONVERSION_CONSTANT ), pDoc ); |
| 48 | signField.SetFieldName("SignatureFieldName"); |
| 49 | signField.SetSignature(signatureData); |
| 50 | signField.SetSignatureReason("I agree"); |
| 51 | } |
| 52 | |
| 53 | |
| 54 | int main( int argc, char* argv[] ) |
| 55 | { |
| 56 | PdfPage* pPage; |
| 57 | |
| 58 | if( argc != 2 ) |
| 59 | { |
| 60 | printf("Usage: SignTest [output_filename]\n"); |
| 61 | printf(" - Create a PDF ready to be signed\n"); |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | try { |
| 66 | PdfSignOutputDevice signer(argv[1]); |
| 67 | // Reserve space for signature |
| 68 | signer.SetSignatureSize(1024); |
| 69 | |
| 70 | PdfStreamedDocument writer( &signer, PoDoFo::ePdfVersion_1_5 ); |
| 71 | // Disable default appearance |
| 72 | writer.GetAcroForm(ePdfCreateObject, PdfAcroForm::ePdfAcroFormDefaultAppearance_None); |
| 73 | |
| 74 | pPage = writer.CreatePage( PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ) ); |
| 75 | TEST_SAFE_OP( CreateSimpleForm( pPage, &writer, *signer.GetSignatureBeacon() ) ); |
| 76 | |
| 77 | TEST_SAFE_OP( writer.Close() ); |
| 78 | |
| 79 | // Adjust ByteRange for signature |
| 80 | if(signer.HasSignaturePosition()) { |
| 81 | signer.AdjustByteRange(); |
| 82 | |
| 83 | // read data for signature and count it |
| 84 | signer.Seek(0); |
| 85 | |
| 86 | // generate digest and count signature |
| 87 | // use NSS, MS Crypto API or OpenSSL |
| 88 | // to generate signature in DER format |
| 89 | char buff[65536]; |
| 90 | size_t len; |
| 91 | while( (len = signer.ReadForSignature(buff, 65536))>0 ) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | // Paste signature to the file |
| 96 | PdfData sigData("my-real-signature"); |
| 97 | signer.SetSignature(sigData); |
| 98 | } |
| 99 | |
| 100 | signer.Flush(); |
| 101 | } catch( PdfError & e ) { |
| 102 | std::cerr << "Error: An error " << e.GetError() << " ocurred." << std::endl; |
| 103 | e.PrintErrorMsg(); |
| 104 | return e.GetError(); |
| 105 | } |
| 106 | |
| 107 | return 0; |
| 108 | } |