Load Template, Update and Save |
[Top] [Chapter] [Previous] [Next] |
Here we work with a template which does not only contain data field placeholder but also tables.
We can export this template using a few lines of code:
rtF2PDF1.SetLicense("","",0); // Your data here .... rtF2PDF1.FileName = "c:\\newpdf.pdf"; if(rtF2PDF1.LoadRTF( "c:\Template.rtf" )) { rtF2PDF1.Export(); }
To also update the fields we first convert the tags into merge fields and then step through the document and modify it accordingly.
// requires: "using wPDF; using WPDynamic;" IWPEditor Memo = rtF2PDF1.Memo; IWPTextCursor Cursor = Memo.TextCursor;
// Convert all <<*>> int merge fields since such fields can be updated a lot easier // then deletion and inserting text. Also text attributes are preserved better. Cursor.FieldsFromTokens("<<", ">>", "");
// Now we go through the text and update it
// Go to start Cursor.CPPosition = 0;
// And move from mail merge field to mail merge field while(Cursor.CPMoveNextObject(WPDynamic.TextObjTypes.wpobjMergeField, false)) { Memo.CurrObj.EmbeddedText = "data of " + Memo.CurrObj.Name; }
Since this code now works to update the fields we update the code to modify each table to have as many data rows as records in a query. In this example however we just set the rowcount value to a random value. This code should be executed after FieldsFromTokens.
// Now we locate all tables and update their row count to match our query. // In this example we just use a random value Random rnd = new Random(); Cursor.CPPosition = 0; while(Cursor.CPMoveNextTable()) { int rowcount = (int)rnd.Next(20); if (rowcount==0) Cursor.TableDelete(); else { Cursor.CPTableRowNr = 1; // goto row # 1 Memo.CurrPar.SetPtr( Cursor.CPRowPtr ); while(rowcount-->0) Memo.CurrPar.Duplicate(); } }
|