Merging Table data into RTF template

  • Hello,
    We have been talking about dynamic "on-fly" creation of table inside a RTF, after you inform me that the RTF2PDF.Report capability is still not supported.
    My concept is to use a RTF template in order to generate a lot of output PDF documents. I have already manage merging the "single value" field from the templates by typing <<FieldName>> wherever is needed on the RTF template and I did a method which performs TextCursor.ReplateText(...) for every FieldName from a list. So far, it seems OK althougth I'm not sure is this the best possible approach. I notice that the rext-replacement does not want to apply on <<FieldName>> locates inside the header or foter. I assume I have to use the MergeText in order to get accees to this fields ? However, just point me am I doing right when I using TextCursor.ReplaceText(...).

    The other more important for me is , related with my concept about the merging (populating) a table of variables. I actually would like to do something like:

    <<TableStart>> <<Column1>> <<Column2>> <<TableEnd>>

    So I would like to create a code which will parse the TableStart and TableEnd tags and automatically map the data from the data source table for each row selecting only those columns specified between the Table Start/End tags.. Do you understand?

    I hope you do.

    And also I will be very thankful if you would provide some piece of code for this.

    Thank You in Advance,

    Ivanov

    • Offizieller Beitrag

    Hello,

    Zitat

    My concept is to use a RTF template in order to generate a lot of output PDF documents. I have already manage merging the "single value" field from the templates by typing <<FieldName>> wherever is needed on the RTF template and I did a method which performs TextCursor.ReplateText(...) for every FieldName from a list.

    Please note that this, although it works, is not the ideal
    way. In general you get a better solution if you convert the
    <<..>> tags in true merge fields using the method
    FieldsFromTokens.

    The other more important for me is , related with my concept about the merging (populating) a table of variables. I actually would like to do something like:
    <<TableStart>> <<Column1>> <<Column2>> <<TableEnd>>

    I would recommend to create a table in the RTF. It may have
    a header (the titles), a footer and ONE data row. This one
    data row you can duplicate for each data record you have in
    your query.

    You can export your template using a few lines of code:

    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 first convert the tags into merge fields

    Code
    // 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("<<", ">>", "");

    This code modifes 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.

    Code
    // Now we locate all tables and update their row count to match our query.// In this example we just use a random valueRandom 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();   }}

    ... then step through the document and modify the fields:

    Code
    // 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;
    }

    Kind Regards,
    Julian

    • Offizieller Beitrag

    This is the template I used for the example above:

    Code
    {\rtf1\ansi\deff0\uc1\ansicpg1252\deftab720{\fonttbl{\f0\fnil\fcharset1 Arial;}{\f1\fnil\fcharset1 Wingdings;}}{\colortbl\red0\green0\blue0;\red255\green0\blue0;\red0\green128\blue0;\red0\green0\blue255;\red255\green255\blue0;\red255\green0\blue255;\red128\green0\blue128;\red128\green0\blue0;\red0\green255\blue0;\red0\green255\blue255;\red0\green128\blue128;\red0\green0\blue128;\red255\green255\blue255;\red192\green192\blue192;\red128\green128\blue128;\red0\green0\blue0;}\wpprheadfoot1\paperw11906\paperh16838\margl600\margr698\margt828\margb496\headery720\footery720\ftnbj\sftnbj\sftnrstcont\nocolbal\sftnnar\saftnnar\fet0\endnhere\sectdefaultcl{\*\generator WPTools_5.210-PRM;}{{\header{\pard\plain\par
    }}
    \pard\plain\plain\f0\fs22 This is a test template\par
    \pard\plain\plain\f0\fs22\par
    \plain\f0\fs22 A name: <<NAME>>\par
    \pard\plain\plain\f0\fs22\par
    \plain\f0\fs22 And a table\par
    \pard\plain\plain\f0\fs22\par
    \tblstart1{\trowd\trleft0\trftsWidth2\trwWidth5000\clbrdrr\brdrs\brdrw10\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clftsWidth2\clwWidth1250\clvertalt\cellx2645\clbrdrr\brdrs\brdrw10\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clftsWidth2\clwWidth1250\clvertalt\cellx5290\clbrdrr\brdrs\brdrw10\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clftsWidth2\clwWidth1250\clvertalt\cellx7935\clbrdrr\brdrs\brdrw10\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clftsWidth2\clwWidth1250\clvertalt\cellx10575\pard\intbl\itap1\li0\fi0\ri0\sb0\sa0\ql\plain\f0\fs22 A\cell\pard\intbl\itap1\li0\fi0\ri0\sb0\sa0\ql\plain\f0\fs22 B\cell\pard\intbl\itap1\li0\fi0\ri0\sb0\sa0\ql\plain\f0\fs22 C\cell\pard\intbl\itap1\li0\fi0\ri0\sb0\sa0\ql\plain\f0\fs22 D\cell\row}{\trowd\trleft0\trftsWidth2\trwWidth5000\clbrdrr\brdrs\brdrw10\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clftsWidth2\clwWidth1250\clvertalt\cellx2645\clbrdrr\brdrs\brdrw10\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clftsWidth2\clwWidth1250\clvertalt\cellx5290\clbrdrr\brdrs\brdrw10\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clftsWidth2\clwWidth1250\clvertalt\cellx7935\clbrdrr\brdrs\brdrw10\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clftsWidth2\clwWidth1250\clvertalt\cellx10575\pard\intbl\itap1\li0\fi0\ri0\sb0\sa0\ql\plain\f0\fs22 <<A>>\cell\pard\intbl\itap1\li0\fi0\ri0\sb0\sa0\ql\plain\f0\fs22 <<B>>\cell\pard\intbl\itap1\li0\fi0\ri0\sb0\sa0\ql\plain\f0\fs22 <<C>>\cell\pard\intbl\itap1\li0\fi0\ri0\sb0\sa0\ql\plain\f0\fs22 <<D>>\cell\row}{\trowd\trleft0\trftsWidth2\trwWidth5000\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clftsWidth2\clwWidth1250\clvertalt\cellx2645\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clftsWidth2\clwWidth1250\clvertalt\cellx5290\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clftsWidth2\clwWidth1250\clvertalt\cellx7935\clbrdrb\brdrs\brdrw10\clbrdrr\brdrs\brdrw10\clbrdrt\brdrs\brdrw10\clbrdrl\brdrs\brdrw10\clftsWidth2\clwWidth1250\clvertalt\cellx10575\pard\intbl\itap1\li0\fi0\ri0\sb0\sa0\ql\plain\f0\fs22\cell\pard\intbl\itap1\li0\fi0\ri0\sb0\sa0\ql\plain\f0\fs22\cell\pard\intbl\itap1\li0\fi0\ri0\sb0\sa0\ql\plain\f0\fs22\cell\pard\intbl\itap1\li0\fi0\ri0\sb0\sa0\ql\plain\f0\fs22 <<SUMA>>\cell\row}\tblend1\pard\plain\plain\f0\fs22 End\par
    }}