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

Office File API & Office-Inspired Desktop UI Controls – Tips, Tricks, and a Breaking Change (October 2021)

$
0
0

This post includes a series of interesting support tickets answered throughout August and September along with a breaking change notice. It also includes a list 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.

Breaking Change

T1032641 - The PdfChoiceFormFieldFacade<T> class and its descendants were changed
https://supportcenter.devexpress.com/ticket/details/t1032641

With our most recent update (v21.1.6), the MultiSelect and Value properties were moved from the PdfChoiceFormFieldFacade<T> class to its descendants: PdfComboBoxFormFieldFacade and PdfListBoxFormFieldFacade. The latter class now includes a Values property that allows you to specify multiple values for a list box form field.

If you need to specify a single value for a list box form field, use the PdfListBoxFormFieldFacade.Values property in the following manner:

using (PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor())
{   
   pdfDocumentProcessor.LoadDocument("Documents//FormDemo.pdf");
   PdfDocumentFacade documentFacade = pdfDocumentProcessor.DocumentFacade;
   PdfAcroFormFacade acroForm = documentFacade.AcroForm;
   PdfListBoxFormFieldFacade listField = acroForm.GetListBoxFormField("List");
   listField.Values = new List<string>() { listField.Items[2].Value };
   pdfDocumentProcessor.SaveDocument("Documents//Result.pdf");
}

Tips & Tricks

Word Processing Document API

  • T1019089 - How to use the Word Processing Document API to insert an image into the MVC Rich Text Editor
    https://supportcenter.devexpress.com/ticket/details/t1019089

  • T1026292 - How to obtain custom document properties
    https://supportcenter.devexpress.com/ticket/details/t1026292

    The Document.CustomProperties property returns custom properties. To access an individual property, obtain the property’s name, and pass this name to the indexer. The following code sample retrieves all custom properties:

    using DevExpress.XtraRichEdit;
    using DevExpress.XtraRichEdit.API.Native;
    using System.Linq;
    using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
    {
      // Load document
      wordProcessor.LoadDocument("Document.docx");               
    
      // Access custom property collection
      DocumentCustomProperties customProperties =
      		wordProcessor.Document.CustomProperties;
    
      for (int i = 0; i <= customProperties.Count-1; i++)
      {
         // Get property name
         string propertyName = customProperties.Names.ElementAt(i);
                      
         // Retrieve property by its name
         var property = customProperties[propertyName];
    
         // Your code here
       }
    }
  • T1025985 - How to display a Data Table below a chart
    https://supportcenter.devexpress.com/ticket/details/t1025985

WinForms and WPF Rich Text Editors

Spreadsheet Document API

  • T1024059 - How to merge data from multiple worksheets into a single worksheet
    https://supportcenter.devexpress.com/ticket/details/t1024059

    Call the CellRange.CopyFrom method to copy data (e.g., text, numbers, and formula calculated values) from one worksheet to another. Use the GetDataRange method to retrieve a data range from a source worksheet.

    using DevExpress.Spreadsheet;
    
    using (var targetWorkbook = new Workbook())
    {
    
      // Specify copy options if needed
      targetWorkbook.Options.Copy.KeepSourceThemeColors = true;
      targetWorkbook.Options.Copy.KeepSourceThemeFonts = true;
    
      var targetWorksheet = targetWorkbook.Worksheets[0];
      using (var sourceWorkbook = new Workbook())
      {
         // Load source worksheet
         sourceWorkbook.LoadDocument("merge.xlsx");
         int topRowIndex = 0;
         foreach (var sourceWorksheet in sourceWorkbook.Worksheets)
         {
           if (!sourceWorksheet.HasData)
           continue;
    
           // Retrieve data from source worksheet
           var sourceRange = sourceWorksheet.GetDataRange();
                          
           // Define target worksheet area to paste data
           int rightColumnIndex = sourceRange.ColumnCount - 1;
           int bottomRowIndex = topRowIndex + sourceRange.RowCount - 1;
           var targetRange = targetWorksheet.Range.FromLTRB(0, topRowIndex, 
           		rightColumnIndex, bottomRowIndex );                      
    
           // Copy data from one worksheet to another
           targetRange.CopyFrom(sourceRange, PasteSpecial.Values | PasteSpecial.Formats);
           topRowIndex += sourceRange.RowCount;
         }
      }
      targetWorkbook.SaveDocument("result.xlsx");
    }

WinForms and WPF Spreadsheet Controls

PDF Document API

  • T1025879 - How to add a stamp to a PDF file
    https://supportcenter.devexpress.com/ticket/details/t1025879

    You can use the PDF Graphics API to draw an image (stamp) on a page. The code sample below draws an image in the corner of the page:

    using (PdfDocumentProcessor documentProcessor = new PdfDocumentProcessor())
    {
       // Load document
       documentProcessor.LoadDocument("Document.pdf");               
    
       // Create PDF graphics
       using (PdfGraphics graph = documentProcessor.CreateGraphics())
       {
         // Draw image
         graph.DrawImage(File.ReadAllBytes("Documents//stamp.png"),
         	new System.Drawing.PointF(500, 50));                   
    
         // Add image to the page foreground
         graph.AddToPageForeground(documentProcessor.Document.Pages[0]);
       }
       
       // Save the result
       documentProcessor.SaveDocument("result.pdf");
    }

    Note: We are going to enhance annotation support in our upcoming major release (v21.2). Our new API will allow you to add interactive rubber stamp annotations.

Enhancements

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

  • T1024708 - We implemented new ColumnCollection.Insert method overloads that allow you to format inserted columns
    https://supportcenter.devexpress.com/ticket/details/t1024708

    ColumnCollection.Insert methods now accept the formatMode parameter and allow you to specify how to format inserted columns. The following options are supported:

    • ColumnFormatMode.FormatAsNext - New columns have the same formatting as the column to the right of inserted columns.
    • ColumnFormatMode.FormatAsPrevious - New columns have the same formatting as the column to the left of inserted columns.
    • ColumnFormatMode.None - Inserted columns have no formatting applied.

    The following example inserts two columns ("B" and "C") with the same format settings as column "A":

    worksheet.Columns.Insert("B", 2, ColumnFormatMode.FormatAsPrevious);
  • T1017380 – We added a SpreadsheetCompatibilityOptions.TruncateLongStringsInFormulas option to load documents containing formulas with text values longer than 255 characters
    https://supportcenter.devexpress.com/ticket/details/t1017380

    Disable this property to allow users to load documents that contain formulas with string values longer than 255 characters.

    spreadsheetControl1.Options.Compatibility.TruncateLongStringsInFormulas = false;

    Consider the following restrictions:

    • The following message appears when a user edits a formula with string values longer than 255 characters:

    • Text values in formulas are truncated to 255 characters when you export a document to Excel formats (for example, XLSX, XLSB, XLS, or XML Spreadsheet 2003).

    We recommend that you use the CONCATENATE function or the concatenation operator (&) to create formulas with text values longer than 255 characters. The following example iterates through all formulas in a workbook and splits long strings with the CONCATENATE function:
    Spreadsheet - How to use the CONCATENATE function to update formulas with text values longer than 255 characters

PDF Document API

New Example: Word Processing Document API

This new example demonstrates how to use the Layout API to execute the following page actions:

  • Get page text
  • Get page number and page count
  • Search for text on a specific page

Use the RichEditDocumentServer.DocumentLayout property as an entry point to the Layout API.

Layout API - How to perform page-based operations

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.


Viewing all articles
Browse latest Browse all 2370

Trending Articles