Office File API and Office-Inspired Desktop UI Controls – Tips, Tricks, and an Important Breaking Change (June-July 2021)

This post includes a series of interesting support tickets answered throughout June and July along with a breaking change notice and a series of enhancements made to our Office-inspired product line. We hope you find the contents of this post interesting and of business value. Should you have any questions about this post, feel free to comment below.

Upcoming Breaking Change

T1012049 - DataFormatOptions and DXRichEditDataFormatOptions properties are now obsolete

With our v21.1.5 release, the following DataFormatOptions and DXRichEditDataFormatOptions members will become obsolete:

  • AllowHtml
  • AllowRtf
  • AllowPlainText

These properties affected both copy and paste operations with format-specific content (after the T964531 breaking change). The insertion of HTML content was disabled by default, but you could set the AllowHtml property to true to enable it. We received a lot of feedback regarding this breaking change and decided to restore and enhance previous behavior.

We added Html, Rtf, and PlainText properties to DataFormatOptions and DXRichEditDataFormatOptions classes instead of the now obsolete members. Set new properties for the appropriate RichEditClipboardMode enumeration value to enable copy/paste operations or enable/disable all clipboard operations. Insertion of HTML content from the clipboard is now enabled by default.

We plan to remove these obsolete members in our v22.1 major release (we will post a second notice on this blog once this change is made).

Tips & Tricks

Word Processing Document API

  • T998620 - How to replace a document field with a different field

    Iterate through the Document.Fields collection and obtain the field code for each field. Split the code string by words and check whether this string contains the phrase "MERGEFIELD.” Obtain the word after this phrase (which represents the field name). Replace the field name in the code range and save your results. See the code below for more information.

    using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
       Document document = wordProcessor.Document;
       FieldCollection fields = document.Fields; 
       // Fill the list of field replacements as needed:
       var newFieldList;
       // ...
       // Iterate through the field collection:
       for (int i = 0; i < document.Fields.Count; i++)
          Field field = fields[i]; 
          // Get field code:
          string codeRange = document.GetText(field.CodeRange).Trim();
          // Split the code string:
          string[] mergeField = codeRange.Split(' ');    
          // Find field name in the replacement list:
          if (newFieldList.ContainsKey(mergeField[1]))
             DocumentRange range = 
             	document.CreateRange(field.CodeRange.Start, field.CodeRange.Length);
             // Enable field code:
             field.ShowCodes = true;
             // Replace field:
             	string.Format("MERGEFIELD {0}", newFieldList[mergeField[1]]));
             // Update field:
       wordProcessor.SaveDocument("Result.docx", DocumentFormat.OpenXml);
  • T1009825 - How to align text in a table cell

  • T1007313 - How to create a table style with specific formatting for the header row

Excel Export Library

WinForms and WPF Rich Text Editors/Word Processors

  • T1014207 - How to display a tooltip above the caret position

    Call the RichEditControl.GetBoundsFromPosition method within the SelectionChanged event handler to obtain caret screen coordinates. Use the retrieved coordinates to calculate the position for the TooltipController component. The code sample below illustrates use of this API:

    using DevExpress.XtraRichEdit;
    using DevExpress.XtraRichEdit.API.Native;
    using System.Drawing;
    using DevExpress.Office.Utils;
    richEditControl.SelectionChanged += richEditControl_SelectionChanged;       
    private void richEditControl_SelectionChanged(object sender, EventArgs e)
        RichEditControl richEditor = (RichEditControl)sender; 
        // Get caret position: 
        DocumentPosition caretPosition = richEditor.Document.CaretPosition;   
        // Calculate tooltip position:
        Rectangle rectangle =
            	richEditor.DpiX, richEditor.DpiY);
        Point point =
        	richEditor.PointToScreen(new Point(rectangle.Left, rectangle.Top));
        // Specify tooltip text and position:
        toolTipController1.ShowHint("Tooltip text", point);
  • T1007536 - How to right-align Arabic text

  • T1005930 - How to specify a file path to save a document

WinForms Spreadsheet Control

WinForms PDF Viewer

WPF Spreadsheet Control


Spreadsheet Document API and Spreadsheet Controls (for WinForms and WPF)

  • T1012436 - We added a new ColorPalette property to the Workbook and IWorkbook objects.

    This property returns a WorkbookColorPalette object and allows you to access a color palette for a given workbook. This palette includes 56 RGB colors. You can change each color, copy palette colors from another document, or reset a custom palette to default colors.

    The following code example demonstrates how to copy a color palette from one workbook to another:

    using (Workbook sourceWorkbook = new Workbook())
    using (Workbook targetWorkbook = new Workbook())

Spreadsheet Document API

  • T1008783 - We implemented new CrossType and CrossesAt properties for the Axis interface

    Use the CrossType property to define where a specific axis crosses its perpendicular axis. The following options are available:

    • AxisCrossType.Auto – The category axis crosses at the zero point of the value axis - the minimum value (if minimum is greater than zero) - or the maximum value (if maximum is less than zero).
    • AxisCrossType.Min – The intersection point is the minimum value on the perpendicular axis.
    • AxisCrossType.Max – The intersection point is the maximum value on the perpendicular axis.
    • AxisCrossType.Custom – The CrossesAt property defines the axis intersection point.

    The code below creates a simple line chart and specifies that the chart’s category axis crosses the value axis at the minimum y-value.

    // Create line chart and specify its location.
    Chart chart = worksheet.Charts.Add(ChartType.LineMarker, worksheet["B2:C14"]);
    chart.TopLeftCell = worksheet.Cells["E2"];
    chart.BottomRightCell = worksheet.Cells["K14"];
    // Hide chart legend.
    chart.Legend.Visible = false;
    // Specify axis intersection point.
    chart.PrimaryAxes[0].CrossType = AxisCrossType.Min;

PDF Document API

New Example: WPF PDF Viewer

The following example demonstrates how use an Adorner element to add a selection rectangle to the PDF Viewer control and extract selected text:

As always, if you’ve come across an interesting support ticket you’d like to share with the rest of the DevExpress developer community, please comment below and include the appropriate link.

FREE DevExpress Xamarin UI Controls

Deliver high-impact native mobile apps with our feature-rich Data Grid, Scheduler, Chart, TabView, Editors, and utility controls. Reserve your free copy today!
Jaime Alvarez [VOLUNDAT]
Jaime Alvarez

Blazor PDF Viewer!!!

Please consider this component in the next versions 

Thanks a lot

27 July 2021
Graham O'Riley
Graham O'Riley
With a thumbnail viewer like Adobe reader. That would be fantastic. Thanks.
17 August 2021
Do you have intention to implement web pdf viewer?
18 August 2021

Please login or register to post comments.