| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2007 by Dominik Seichter * |
| 3 |
* [email protected] * |
| 4 |
* * |
| 5 |
* This program is free software; you can redistribute it and/or modify * |
| 6 |
* it under the terms of the GNU General Public License as published by * |
| 7 |
* the Free Software Foundation; either version 2 of the License, or * |
| 8 |
* (at your option) any later version. * |
| 9 |
* * |
| 10 |
* This program is distributed in the hope that it will be useful, * |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 |
* GNU General Public License for more details. * |
| 14 |
* * |
| 15 |
* You should have received a copy of the GNU General Public License * |
| 16 |
* along with this program; if not, write to the * |
| 17 |
* Free Software Foundation, Inc., * |
| 18 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 19 |
***************************************************************************/ |
| 20 |
|
| 21 |
#include <podofo.h> |
| 22 |
|
| 23 |
#include "../PdfTest.h" |
| 24 |
|
| 25 |
#include <iostream> |
| 26 |
#include <cstdio> |
| 27 |
|
| 28 |
using namespace PoDoFo; |
| 29 |
|
| 30 |
#define CONVERSION_CONSTANT 0.002834645669291339 |
| 31 |
|
| 32 |
void CreateComplexForm( PdfPage* pPage, PdfStreamedDocument* pDoc ) |
| 33 |
{ |
| 34 |
PdfRect rect = pPage->GetPageSize(); |
| 35 |
|
| 36 |
PdfPainter painter; |
| 37 |
PdfFont* pFont = pDoc->CreateFont( "Courier" ); |
| 38 |
|
| 39 |
painter.SetPage( pPage ); |
| 40 |
painter.SetFont( pFont ); |
| 41 |
|
| 42 |
const char* pszTitle = "PoDoFo Sample Feedback Form"; |
| 43 |
pFont->SetFontSize( 18.0 ); |
| 44 |
|
| 45 |
double x = (rect.GetWidth() - pFont->GetFontMetrics()->StringWidth( pszTitle )) / 2.0; |
| 46 |
double y = rect.GetHeight() - (20000.0 * CONVERSION_CONSTANT); |
| 47 |
|
| 48 |
painter.DrawText( x, y, pszTitle ); |
| 49 |
pFont->SetFontSize( 10.0 ); |
| 50 |
|
| 51 |
y -= 10000.0 * CONVERSION_CONSTANT; |
| 52 |
x = 10000.0 * CONVERSION_CONSTANT; |
| 53 |
|
| 54 |
double h = 10000.0 * CONVERSION_CONSTANT; |
| 55 |
// Name |
| 56 |
y -= 10000.0 * CONVERSION_CONSTANT; |
| 57 |
painter.DrawText( x, y, "Your Name:" ); |
| 58 |
PdfTextField textName( pPage, PdfRect( 80000.0 * CONVERSION_CONSTANT, y - 2500.0 * CONVERSION_CONSTANT, |
| 59 |
80000.0 * CONVERSION_CONSTANT, h ), pDoc ); |
| 60 |
textName.SetFieldName("field_name"); |
| 61 |
textName.SetBorderColor( 1.0 ); |
| 62 |
|
| 63 |
// E-Mail |
| 64 |
y -= 10000.0 * CONVERSION_CONSTANT; |
| 65 |
painter.DrawText( x, y, "E-Mail Address:" ); |
| 66 |
PdfTextField textMail( pPage, PdfRect( 80000.0 * CONVERSION_CONSTANT, y - 2500.0 * CONVERSION_CONSTANT, |
| 67 |
80000.0 * CONVERSION_CONSTANT, h ), pDoc ); |
| 68 |
textMail.SetFieldName("field_mail"); |
| 69 |
textMail.SetBorderColor( 1.0 ); |
| 70 |
|
| 71 |
// Interest |
| 72 |
y -= 10000.0 * CONVERSION_CONSTANT; |
| 73 |
painter.DrawText( x, y, "Job:" ); |
| 74 |
|
| 75 |
PdfComboBox comboJob( pPage, PdfRect( 80000.0 * CONVERSION_CONSTANT, y - 2500.0 * CONVERSION_CONSTANT, |
| 76 |
80000.0 * CONVERSION_CONSTANT, h ), pDoc ); |
| 77 |
comboJob.SetFieldName("field_combo"); |
| 78 |
comboJob.SetBorderColor( 1.0 ); |
| 79 |
|
| 80 |
comboJob.InsertItem( "Software Engineer" ); |
| 81 |
comboJob.InsertItem( "Student" ); |
| 82 |
comboJob.InsertItem( "Publisher" ); |
| 83 |
comboJob.InsertItem( "Other" ); |
| 84 |
|
| 85 |
// Open Source |
| 86 |
y -= 11000.0 * CONVERSION_CONSTANT; |
| 87 |
painter.DrawText( x, y, "I wan't to use PoDoFo in an Open Source application" ); |
| 88 |
PdfCheckBox checkOpenSource( pPage, PdfRect( 120000.0 * CONVERSION_CONSTANT, y - 2500.0 * CONVERSION_CONSTANT, |
| 89 |
h, h ), pDoc ); |
| 90 |
checkOpenSource.SetFieldName("field_check_oss"); |
| 91 |
|
| 92 |
// Commercial |
| 93 |
y -= 11000.0 * CONVERSION_CONSTANT; |
| 94 |
painter.DrawText( x, y, "I wan't to use PoDoFo in a commercial application" ); |
| 95 |
PdfCheckBox checkCom( pPage, PdfRect( 120000.0 * CONVERSION_CONSTANT, y - 2500.0 * CONVERSION_CONSTANT, |
| 96 |
h, h ), pDoc ); |
| 97 |
checkCom.SetFieldName("field_check_com"); |
| 98 |
|
| 99 |
y -= 10000.0 * CONVERSION_CONSTANT; |
| 100 |
painter.DrawText( x, y, "Some comments you want to send to the PoDoFo developers:" ); |
| 101 |
PdfTextField textComment( pPage, PdfRect( 20000.0 * CONVERSION_CONSTANT, y - 120000.0 * CONVERSION_CONSTANT, |
| 102 |
160000.0 * CONVERSION_CONSTANT, 100000.0 * CONVERSION_CONSTANT ), pDoc ); |
| 103 |
textComment.SetFieldName("field_comment"); |
| 104 |
textComment.SetMultiLine( true ); |
| 105 |
textComment.SetRichText( true ); |
| 106 |
textComment.SetText( "<?xml version=\"1.0\"?><body xmlns=\"http://www.w3.org/1999/xtml\" xmlns:xfa=\"http://www.xfa.org/schema/xfa-data/1.0/\" xfa:contentType=\"text/html\" xfa:APIVersion=\"Acrobat:8.0.0\" xfa:spec=\"2.4\"><p style=\"text-align:left\"><b><i>Here is some bold italic text</i></b></p><p style=\"font-size:16pt\">This text uses default text state parameters but changes the font size to 16.</p></body>"); |
| 107 |
|
| 108 |
PdfPushButton buttonSend( pPage, PdfRect( 10000 * CONVERSION_CONSTANT, 10000 * CONVERSION_CONSTANT, |
| 109 |
25000 * CONVERSION_CONSTANT, 25000 * CONVERSION_CONSTANT ), pDoc ); |
| 110 |
buttonSend.SetFieldName("field_send"); |
| 111 |
buttonSend.SetCaption("Send"); |
| 112 |
buttonSend.SetBackgroundColor( 0.5 ); |
| 113 |
|
| 114 |
PdfPushButton buttonClear( pPage, PdfRect( 40000 * CONVERSION_CONSTANT, 10000 * CONVERSION_CONSTANT, |
| 115 |
25000 * CONVERSION_CONSTANT, 25000 * CONVERSION_CONSTANT ), pDoc ); |
| 116 |
buttonClear.SetFieldName("field_clear"); |
| 117 |
buttonClear.SetCaption("Clear"); |
| 118 |
buttonClear.SetBackgroundColor( 0.5 ); |
| 119 |
|
| 120 |
PdfAction actionClear( ePdfAction_JavaScript, pDoc ); |
| 121 |
actionClear.SetScript( |
| 122 |
PdfString("this.getField(\"field_name\").value = \"\";" \ |
| 123 |
"this.getField(\"field_mail\").value = \"\";" \ |
| 124 |
"this.getField(\"field_combo\").value = \"\";" |
| 125 |
"this.getField(\"field_check_oss.\").checkThisBox( 0, false );" \ |
| 126 |
"this.getField(\"field_check_com.\").checkThisBox( 0, false );" \ |
| 127 |
"this.getField(\"field_comment\").value = \"\";" ) ); |
| 128 |
|
| 129 |
|
| 130 |
buttonClear.SetMouseDownAction( actionClear ); |
| 131 |
|
| 132 |
PdfAction actionSubmit( ePdfAction_SubmitForm, pDoc ); |
| 133 |
|
| 134 |
buttonSend.SetMouseDownAction( actionSubmit ); |
| 135 |
|
| 136 |
painter.FinishPage(); |
| 137 |
} |
| 138 |
|
| 139 |
void CreateSimpleForm( PdfPage* pPage, PdfStreamedDocument* pDoc ) |
| 140 |
{ |
| 141 |
PdfPainter painter; |
| 142 |
PdfFont* pFont = pDoc->CreateFont( "Courier" ); |
| 143 |
|
| 144 |
painter.SetPage( pPage ); |
| 145 |
painter.SetFont( pFont ); |
| 146 |
painter.DrawText( 10000 * CONVERSION_CONSTANT, 280000 * CONVERSION_CONSTANT, "PoDoFo Interactive Form Fields Test" ); |
| 147 |
painter.FinishPage(); |
| 148 |
|
| 149 |
PdfPushButton button( pPage, PdfRect( 10000 * CONVERSION_CONSTANT, 10000 * CONVERSION_CONSTANT, |
| 150 |
50000 * CONVERSION_CONSTANT, 50000 * CONVERSION_CONSTANT ), pDoc ); |
| 151 |
|
| 152 |
button.SetFieldName("ButtonFieldName"); |
| 153 |
button.SetAlternateName("ButtonAlternateName"); |
| 154 |
button.SetMappingName("ButtonMappingName"); |
| 155 |
button.SetCaption("Hallo Welt"); |
| 156 |
|
| 157 |
|
| 158 |
PdfAction action( ePdfAction_JavaScript, pDoc ); |
| 159 |
action.SetScript( |
| 160 |
PdfString("var str = this.getField(\"TextFieldName\").value;" \ |
| 161 |
"var j = 4*5;" \ |
| 162 |
"app.alert(\"Hello World! 4 * 5 = \" + j + \" Text Field: \" + str );") ); |
| 163 |
|
| 164 |
button.SetMouseDownAction( action ); |
| 165 |
|
| 166 |
PdfTextField text( pPage, PdfRect( 70000 * CONVERSION_CONSTANT, 10000 * CONVERSION_CONSTANT, |
| 167 |
50000 * CONVERSION_CONSTANT, 50000 * CONVERSION_CONSTANT ), pDoc ); |
| 168 |
|
| 169 |
text.SetFieldName("TextFieldName"); |
| 170 |
text.SetMultiLine( true ); |
| 171 |
text.SetMultiLine( false ); |
| 172 |
|
| 173 |
|
| 174 |
text.SetFileField( true ); |
| 175 |
printf("Text IsMultiLine: %i\n", text.IsMultiLine() ); |
| 176 |
|
| 177 |
PdfComboBox combo( pPage, PdfRect( 10000 * CONVERSION_CONSTANT, 250000 * CONVERSION_CONSTANT, |
| 178 |
50000 * CONVERSION_CONSTANT, 10000 * CONVERSION_CONSTANT ), pDoc ); |
| 179 |
|
| 180 |
combo.SetFieldName("ComboFieldName"); |
| 181 |
combo.InsertItem( "Value1" ); |
| 182 |
combo.InsertItem( "Value2" ); |
| 183 |
combo.InsertItem( "Value3" ); |
| 184 |
combo.InsertItem( "XXXX", "Displayed Text" ); |
| 185 |
combo.SetEditable( true ); |
| 186 |
combo.SetSelectedItem( 1 ); |
| 187 |
|
| 188 |
printf("IsComboBox: %i\n", combo.IsComboBox() ); |
| 189 |
printf("Count : %i\n", static_cast<int>(combo.GetItemCount()) ); |
| 190 |
printf("Selected : %i\n", combo.GetSelectedItem() ); |
| 191 |
|
| 192 |
PdfListBox listBox( pPage, PdfRect( 70000 * CONVERSION_CONSTANT, 200000 * CONVERSION_CONSTANT, |
| 193 |
50000 * CONVERSION_CONSTANT, 50000 * CONVERSION_CONSTANT ), pDoc ); |
| 194 |
|
| 195 |
listBox.SetFieldName("ListBoxFieldName"); |
| 196 |
listBox.InsertItem( "Value1", "Display 1" ); |
| 197 |
listBox.InsertItem( "Value2", "Display 2" ); |
| 198 |
listBox.InsertItem( "Value3", "Display 3" ); |
| 199 |
//listBox.InsertItem( "XXXX", "Displayed Text" ); |
| 200 |
listBox.SetMultiSelect( true ); |
| 201 |
listBox.SetSelectedItem( 2 ); |
| 202 |
} |
| 203 |
|
| 204 |
void FillTextField( PdfTextField & rField ) |
| 205 |
{ |
| 206 |
const char* pszCur = rField.GetText().GetString(); |
| 207 |
std::cout << " Current value:" << (pszCur ? pszCur : "") << std::endl; |
| 208 |
|
| 209 |
std::string value; |
| 210 |
std::cout << " Enter new value (if empty value is unchanged):" << std::endl; |
| 211 |
#if defined(_MSC_VER) && _MSC_VER <= 1200 // Visual Studio 6 |
| 212 |
{ |
| 213 |
char buff[10240]; |
| 214 |
if (gets(buff)) |
| 215 |
value = buff; |
| 216 |
} |
| 217 |
#else |
| 218 |
getline( std::cin, value ); |
| 219 |
#endif |
| 220 |
|
| 221 |
if( value.length() ) |
| 222 |
{ |
| 223 |
rField.SetText( value ); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
void FillListField( PdfListField & rField ) |
| 228 |
{ |
| 229 |
const char* pszCur = ( rField.GetSelectedItem() == -1 ? NULL : |
| 230 |
rField.GetItemDisplayText( rField.GetSelectedItem() ).GetString() ); |
| 231 |
std::cout << " Current value:" << (pszCur ? pszCur : "") << std::endl; |
| 232 |
std::cout << " Values:" << std::endl; |
| 233 |
|
| 234 |
for( int i=0;i<static_cast<int>(rField.GetItemCount());i++ ) |
| 235 |
{ |
| 236 |
pszCur = rField.GetItemDisplayText( i ).GetString(); |
| 237 |
std::cout << " " << i << " " << (pszCur ? pszCur : "") << std::endl; |
| 238 |
} |
| 239 |
|
| 240 |
int nValue; |
| 241 |
std::cout << " Enter index of new value:" << std::endl; |
| 242 |
std::cin >> nValue; |
| 243 |
|
| 244 |
if( nValue >= 0 && nValue < static_cast<int>(rField.GetItemCount()) ) |
| 245 |
rField.SetSelectedItem( nValue ); |
| 246 |
} |
| 247 |
|
| 248 |
void FillForm( const char* pszFilename, const char* pszOutput ) |
| 249 |
{ |
| 250 |
PdfMemDocument doc( pszFilename ); |
| 251 |
PdfPage* pPage; |
| 252 |
int nPageCount = doc.GetPageCount(); |
| 253 |
int nFieldCount; |
| 254 |
|
| 255 |
for( int i=0;i<nPageCount;i++ ) |
| 256 |
{ |
| 257 |
pPage = doc.GetPage( i ); |
| 258 |
nFieldCount = pPage->GetNumFields(); |
| 259 |
|
| 260 |
std::cout << "Page " << i + 1 << " contains " << nFieldCount << " fields." << std::endl; |
| 261 |
|
| 262 |
for( int n=0;n<nFieldCount;n++ ) |
| 263 |
{ |
| 264 |
PdfField field = pPage->GetField( n ); |
| 265 |
EPdfField eType = field.GetType(); |
| 266 |
|
| 267 |
std::cout << " Field: " << field.GetFieldName().GetString() << std::endl; |
| 268 |
std::cout << " Type : "; |
| 269 |
|
| 270 |
switch( eType ) |
| 271 |
{ |
| 272 |
case ePdfField_PushButton: |
| 273 |
std::cout << "PushButton" << std::endl; |
| 274 |
break; |
| 275 |
case ePdfField_CheckBox: |
| 276 |
std::cout << "CheckBox" << std::endl; |
| 277 |
break; |
| 278 |
case ePdfField_RadioButton: |
| 279 |
std::cout << "RadioButton" << std::endl; |
| 280 |
break; |
| 281 |
case ePdfField_TextField: |
| 282 |
{ |
| 283 |
std::cout << "TextField" << std::endl; |
| 284 |
PdfTextField text( field ); |
| 285 |
FillTextField( text ); |
| 286 |
break; |
| 287 |
} |
| 288 |
case ePdfField_ComboBox: |
| 289 |
{ |
| 290 |
std::cout << "ComboBox" << std::endl; |
| 291 |
PdfListField lst( field ); |
| 292 |
FillListField( lst ); |
| 293 |
break; |
| 294 |
} |
| 295 |
case ePdfField_ListBox: |
| 296 |
{ |
| 297 |
std::cout << "ListBox" << std::endl; |
| 298 |
PdfListField lst( field ); |
| 299 |
FillListField( lst ); |
| 300 |
break; |
| 301 |
} |
| 302 |
case ePdfField_Signature: |
| 303 |
std::cout << "Signature" << std::endl; |
| 304 |
break; |
| 305 |
case ePdfField_Unknown: |
| 306 |
default: |
| 307 |
std::cout << "Unknown" << std::endl; |
| 308 |
break; |
| 309 |
} |
| 310 |
|
| 311 |
std::cout << std::endl; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
doc.Write( pszOutput ); |
| 316 |
} |
| 317 |
|
| 318 |
int main( int argc, char* argv[] ) |
| 319 |
{ |
| 320 |
PdfPage* pPage; |
| 321 |
|
| 322 |
if( argc != 2 && argc != 3 ) |
| 323 |
{ |
| 324 |
printf("Usage: FormTest [output_filename]\n"); |
| 325 |
printf(" - Create a new example PDF form\n"); |
| 326 |
printf(" Formtest [input_filename] [output_filename]\n"); |
| 327 |
printf(" - Fill out an existing form and save it to a PDF file\n"); |
| 328 |
return 0; |
| 329 |
} |
| 330 |
|
| 331 |
try { |
| 332 |
if( argc == 3 ) |
| 333 |
{ |
| 334 |
TEST_SAFE_OP( FillForm( argv[1], argv[2] ) ); |
| 335 |
} |
| 336 |
else |
| 337 |
{ |
| 338 |
PdfStreamedDocument writer( argv[1] ); |
| 339 |
|
| 340 |
pPage = writer.CreatePage( PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ) ); |
| 341 |
TEST_SAFE_OP( CreateComplexForm( pPage, &writer ) ); |
| 342 |
|
| 343 |
pPage = writer.CreatePage( PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ) ); |
| 344 |
TEST_SAFE_OP( CreateSimpleForm( pPage, &writer ) ); |
| 345 |
|
| 346 |
TEST_SAFE_OP( writer.Close() ); |
| 347 |
} |
| 348 |
} catch( PdfError & e ) { |
| 349 |
std::cerr << "Error: An error " << e.GetError() << " ocurred." << std::endl; |
| 350 |
e.PrintErrorMsg(); |
| 351 |
return e.GetError(); |
| 352 |
} |
| 353 |
|
| 354 |
return 0; |
| 355 |
} |