Office File API & Office-Inspired UI Controls – Tips & Tricks (November-December 2020)

Office-Inspired Products
04 February 2021

We have compiled a short list of interesting support tickets answered throughout November and December. This post also includes a few enhancements you may wish to leverage in your current project.

We hope you find these support tickets interesting and of business value. Should you have any questions, feel free to comment below or submit a support ticket via the DevExpress Support Center. We’ll be more than happy to follow-up.

Tips & Tricks

PDF Document API

  1. T955113 - How to save a PDF document's attachments as files
    https://supportcenter.devexpress.com/ticket/details/T955113

    The PdfDocument.FileAttachments property allows you to retrieve file attachments as needed. Call the File.WriteAllBytes method for each attachment and use the PdfFileAttachment.FileName and PdfFileAttachment.Data properties as parameters:

    foreach (PdfFileAttachment attachment in pdfProcessor.Document.FileAttachments)
    {
        File.WriteAllBytes(attachment.FileName, attachment.Data);
    }

WinForms and WPF Rich Text Editors

  1. T954928 - How to prevent users from adding shapes to a document
    https://supportcenter.devexpress.com/ticket/details/T954928

  2. T951813 - How to change a hyperlink’s font color
    https://supportcenter.devexpress.com/ticket/details/T951813

  3. T950519 - How to create notes on page margins in code
    https://supportcenter.devexpress.com/ticket/details/T950519/

    You can use text boxes to emulate notes. Create a text box, set its horizontal position relative to the left edge of the page and vertical position relative to a paragraph line:

    Document document = richEditControl.Document;
    document.Unit = DevExpress.Office.DocumentUnit.Inch;
    
    // Create text box and set its size.
    Shape textNote = document.Shapes.InsertTextBox(document.Range.Start);
    textNote.Height = 0.5f;
    textNote.Width = 1.09f;
    
    // Add text box content.
    SubDocument textBoxDocument = textNote.ShapeFormat.TextBox.Document;
    textBoxDocument.AppendText("Text box");
    
    // Fill text box with color.
    textNote.Fill.Color = Color.WhiteSmoke;
    
    // Display borders around text box.
    textNote.Line.Color = Color.Black;
    textNote.Line.Thickness = 1;
    
    // Place text box in page margin.
    textNote.RelativeHorizontalPosition = ShapeRelativeHorizontalPosition.Page;
    textNote.HorizontalAlignment = ShapeHorizontalAlignment.Left;
    textNote.RelativeVerticalPosition = ShapeRelativeVerticalPosition.Line;
    textNote.VerticalAlignment = ShapeVerticalAlignment.Top;
    

WinForms Spreadsheet Control

  1. T958622 - How to connect a shape with a line connector
    https://supportcenter.devexpress.com/ticket/details/T958622

  2. T957161 - How to insert a line shape at the clicked position within a worksheet
    https://supportcenter.devexpress.com/ticket/details/T957161

  3. T947196 - How to remove an item from a sheet tab's context menu
    https://supportcenter.devexpress.com/ticket/details/T947196

Enhancements

Word Processing Document API and Rich Text Editors (for WinForms and WPF)

  1. T949990 – Use the DataFormatOptions class properties to restrict paste operations from the Clipboard for a specific format.

    Note: These options are only used for visual controls.
    https://supportcenter.devexpress.com/ticket/details/T949990

    DataFormatOptions dataFormatOptions = richEditControl.Options.ClipboardFormats;
    dataFormatOptions.AllowImage = false;
    dataFormatOptions.AllowPlainText = false;
    dataFormatOptions.AllowHtml = true;
  2. T959126 – We implemented new LoadDocument and SaveDocument method overloads to import/export a document from/to a byte array

    https://supportcenter.devexpress.com/ticket/details/T959126

    // Save document to byte array to store it in a database.
    byte[] docBytes = wordProcessor.SaveDocument(DocumentFormat.OpenXml);
    
    // ...
           
    // Load document from byte array.
    wordProcessor.LoadDocument(docBytes);
  3. T948328 – Use the HtmlDocumentImporterOptions.ImageScalingDpi property to preserve image resolution in a loaded HTML document and ignore screen DPI settings

    https://supportcenter.devexpress.com/ticket/details/T948328

    HtmlDocumentImporterOptions htmlImportOptions = richEditControl.Options.Import.Html;
    htmlImportOptions.ImageScalingDpi = 96;

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

  1. T956784 – Our Spreadsheet controls now support End + Arrow key keyboard navigation
    https://supportcenter.devexpress.com/ticket/details/T956784

  2. T957871 – You can now define write-protection options for a workbook

    https://supportcenter.devexpress.com/ticket/details/T957871

    Use the DocumentSettings.WriteProtection options to make your workbook read-only and specify a password to prevent modifications from unauthorized users.

    var wpOptions = workbook.DocumentSettings.WriteProtection;
    wpOptions.SetPassword("Password");
    wpOptions.UserName = "John Smith";
    workbook.SaveDocument("WriteProtectedDocument.xlsx");

    If you open the saved workbook in Microsoft Excel, it will prompt you to enter a password to modify the document.

    Note: Write-protection options have no effect when a document is opened in our WinForms or WPF Spreadsheet control. However, you can handle the SpreadsheetControl.DocumentLoaded event to check whether the document is write-protected (or its ReadOnlyRecommended option is enabled) and to make the control read-only when needed.

  3. T956281 – You can now change a shape connector’s form

    https://supportcenter.devexpress.com/ticket/details/T956281

    Use the ConnectorFormat.HandleCount property to specify the number of adjustment handles for an elbow or curved connector. These handles are used to change a connector's size and form. A connector can have two to five handles (including connection points).

    The code example below creates an elbow connector with three line segments and three handles ("bentConnector3").

    IWorkbook workbook = spreadsheetControl.Document; 
    Worksheet worksheet = workbook.Worksheets.ActiveWorksheet; 
    var connector = worksheet.Shapes.AddConnector(ConnectorType.Elbow, 
    	50f, 50f, 450f, 450f); 
    connector.ConnectorFormat.HandleCount = 3;
    
  4. T952107 – Use the Formatting.NumberFormatLocal option to specify or return a number format based on the current (local) culture
    https://supportcenter.devexpress.com/ticket/details/T952107

    using DevExpress.Spreadsheet;
    using System;
    using System.Globalization;
    // …
    
    // Change culture for Spreadsheet control instance.
    spreadsheetControl1.Options.Culture = new CultureInfo("fr-FR");
    
    // Create cell and set its value.
    CellRange cell = worksheet.Range["A1"];
    cell.Value = DateTime.Now;
    
    // Apply date format to cell using the invariant culture.
    cell.NumberFormat = "mm-dd-yy";
    // Obtain applied date format in French culture ("jj/mm/aaaa").
    string localFormat = cell.NumberFormatLocal;
  5. T947451 – You can now retrieve names of selected sheets
    https://supportcenter.devexpress.com/ticket/details/T947451

    Use the SheetView.IsSelected property to select a sheet in a workbook or determine whether the sheet is selected.

    The following code retrieves the names of all selected sheets in the document:

    using System.Collections.Generic;
    using DevExpress.Utils.Extensions;
    using DevExpress.Spreadsheet;
    // ...
    SheetCollection sheets = workbook.Sheets;
    List<string> selectedSheetNames = new List<string>();
    sheets.ForEach((sheet) => {
        if (sheet.ActiveView.IsSelected)
            selectedSheetNames.Add(sheet.Name);
    });

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!
No Comments

Please login or register to post comments.