Quantcast
Channel: Developer Express Inc.
Viewing all articles
Browse latest Browse all 2370

Word Processing Tips and Tricks — Convert Tab-Delimited Text to a Table and Vice Versa

$
0
0

In this post, I’ll describe an interesting usage scenario of our Word Processing product line. We hope you find the contents of this post of business value.

The conversion of a table to a tab-delmited text is automated in Microsoft Word. In this post, I'll show you how to implement the same functionality and the reverse conversion in the Rich Text Editor.

As always, should you have any questions about DevExpress Office products, feel free to submit a support ticket via the DevExpress Support Center.

Convert a Tab-Delimited Text to a Table

To convert a range of tab-delimited text to a table, iterate through paragraphs in the range and split each paragraph to a collection of tab-delimited ranges. Use the SubDocument.FindAll method and pass the "[^\t]+" regular expression as a parameter to complete the step. Combine these collections of tab-delimited ranges into an array.

Create a temporary RichEditDocumentServer instance and build a table. Iterate through the elements in the array and insert contents in the table cells of the temporary RichEditDocumentServer. Once complete, remove source paragraphs and insert the table from the temporary RichEditDocumentServer to the main document.

public void ConvertTabDelimitedTextToTable(DocumentRange range) 
{ 
  richEditControl.BeginUpdate(); 
  SubDocument document = range.BeginUpdateDocument();
    
  // obtain paragraphs for the specified range
  var paragraphs = document.Paragraphs.Get(range); 

  // iterate through paragraphs 
  // and obtain collections of tab-delimited ranges 
  DocumentRange[][] paragraphRanges = new DocumentRange[paragraphs.Count][]; 

  for (int i = 0; i < paragraphs.Count; i++) 
  { 
    Paragraph paragraph = paragraphs[i];
    DocumentRange[] tabDelimitedRanges =
        document.FindAll(new Regex("[^\t]+"), paragraph.Range); 
    paragraphRanges[i] = tabDelimitedRanges; 
  } 

  // calculate the number of rows and columns in a table
  int colCount = paragraphRanges.Select(pRange => pRange.Count()).Max();
  int rowCount = paragraphRanges.Length;
  if (colCount > 0) 
      
  // create a temporary RichEditDocumentServer to build the table 
  using (RichEditDocumentServer temp = new RichEditDocumentServer()) {  
   Document tempDoc = temp.Document;

   // create a table and insert cell content
   // from the collection of the tab-delimited ranges
   Table table = tempDoc.Tables.Create(tempDoc.Range.Start, rowCount, colCount); 

   table.ForEachCell((cell, rowIndex, colIndex) => {
     if (colIndex < paragraphRanges[rowIndex].Count())
     tempDoc.InsertDocumentContent(cell.ContentRange.Start, paragraphRanges[rowIndex][colIndex]); 
     }); 

   // delete source text and insert the table
   // from the temporary RichEditDocumentServer in the main document
   DocumentPosition targetPos = paragraphs[0].Range.Start; 
   document.Delete(document.CreateRange(targetPos, 
       paragraphs.Last().Range.End.ToInt() - targetPos.ToInt()));

   document.InsertDocumentContent(targetPos, 
       tempDoc.Range, InsertOptions.KeepSourceFormatting); 
   } 

   range.EndUpdateDocument(document); 
   richEditControl.EndUpdate(); 
}

Convert a Table to Tab-Delimited Text

Call the Table.ForEachCell method to iterate through table cells and write cell content separated with tabs to a temporary RichEditDocumentServer. Call the InsertDocumentContent method with TableCell.ContentRange value as a parameter to copy cell content. To insert a Tab character, pass the DevExpress.Office.Characters.TabMark.ToString() value as the InsertText method parameter. If necessary, adjust tab stop positions for the inserted paragraphs.

Once you iterate through all cells, call the Document.Delete method to remove the table from the main document and insert the content from the temporary RichEditDocumentServer at the table's position.

public void ConvertTableToTabDelimitedText(DocumentRange range) 
{ 
   richEditControl.BeginUpdate(); 
   SubDocument document = range.BeginUpdateDocument(); 

   // obtain tables from the specified range
   var tables = document.Tables.Get(range); 

   // iterate through tables and convert each table
   // to paragraphs with tab-delimited text 
   foreach (Table table in tables)
   { 
     // create a temporary RichEditDocumentServer to convert table content
     using (RichEditDocumentServer temp = new RichEditDocumentServer()) { 
       Document tempDoc = temp.Document; 
       Section section = tempDoc.Sections[0]; 
       int columnCount = table.FirstRow.Cells.Count; 
       float tabWidth = (section.Page.Width - section.Margins.Left 
           - section.Margins.Right) / columnCount; 
       Paragraph paragraph = tempDoc.Paragraphs.First(); 

       // adjust tab stop positions in the document 
       // based on the number of table columns
       TabInfoCollection tabs = paragraph.BeginUpdateTabs(true);
       for (int colIndex = 1; colIndex <= columnCount; colIndex++)
         tabs.Add(new TabInfo() { Position = tabWidth * colIndex }); 
       paragraph.EndUpdateTabs(tabs); 
                    
       // iterate through cells in a table and insert cell content
       // and the Tab character in the temporary RichEditDocumentServer
       table.ForEachCell((cell, rowIndex, colIndex) => {                         
         tempDoc.AppendDocumentContent(cell.ContentRange, 
             InsertOptions.KeepSourceFormatting); 
         if (colIndex < cell.Row.Cells.Count - 1) { 
           DocumentRange tabRange = tempDoc.AppendText(Characters.TabMark.ToString()); 
           CharacterProperties characterProperties = tempDoc.BeginUpdateCharacters(tabRange); 
           characterProperties.Reset();
           tempDoc.EndUpdateCharacters(characterProperties); 
         } 
         else 
           tempDoc.Paragraphs.Append(); 
       }); 

       // delete the source table and insert tab-delimited content 
       // from RichEditDocumentServer in the mail document
       DocumentPosition targetPos = table.Range.Start; 
       document.Delete(table.Range); 
       document.InsertDocumentContent(targetPos, tempDoc.Range, 
           InsertOptions.KeepSourceFormatting);
     } 
  } 

  range.EndUpdateDocument(document); 
  richEditControl.EndUpdate();

Once complete, the table is converted as follows:

Word Processing Tips and Tricks - Convert Tab-Delimited Text to a Table and Vice Versa

Viewing all articles
Browse latest Browse all 2370

Trending Articles