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