Telerik Forums
Telerik Document Processing Forum
0 answers
163 views
I had a problem with loading & decoding UTF-7 data from a CSV file into Silverlight's RADSpreadProcessing. Special characters like 'é' were being flagged as invalid by the Microsoft UTF-8 decoder in Silverlight. WPF on the other hand supports UTF-7 decoding.

With the help of Nikolay Demirev (Telerik support) he pointed me in the right direction with this Microsoft information: Understanding Encodings for Silverlight

Below is the solution that I came up with to auto-identify the encoding and handle UTF-7 encoded CSV files. I hope that it helps others.

    Sub ProcessData()
 
        Dim FormatProvider = New CsvFormatProvider
 
        With FormatProvider
            With .Settings
                .Delimiter = ","
                .Quote = """"
                .HasHeaderRow = True
                .Encoding = New System.Text.UnicodeEncoding
            End With
        End With
 
        Dim Filename As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Telerik Test.csv")
        Dim WB As Workbook = LoadWB(Filename, FormatProvider)
 
        '-- do something with the loaded data...
 
    End Sub
 
    Function LoadWB(FileName As String, FormatProvider As CsvFormatProvider) As Workbook
 
        If Not File.Exists(FileName) Then
            Throw New FileNotFoundException(String.Format("{0} Not Found.", FileName))
        End If
 
        Dim WB As Workbook
 
        If GetFileEncoding(FileName) Is Encoding.UTF8 Then
            WB = FormatProvider.Import(GetUTF8(FileName))
        Else
            Using Data As FileStream = New FileStream(FileName, FileMode.Open)
                WB = FormatProvider.Import(Data)
            End Using
        End If
 
        Return WB
 
    End Function
 
    Function GetUTF8(Filename As String) As String
 
        Dim encodedBytes() As Byte = File.ReadAllBytes(Filename)
        Dim enc8 As Encoding = New UTF8Encoding(False, True)
        Dim decodedString As String = String.Empty
 
        Try
            decodedString += enc8.GetString(encodedBytes, 0, encodedBytes.Length)
        Catch e As DecoderFallbackException
 
            Dim sb As New StringBuilder
 
            For Each b As Byte In encodedBytes
                sb.Append(Microsoft.VisualBasic.ChrW(Convert.ToInt32(b)))
            Next
 
            decodedString = sb.ToString()
 
        End Try
 
        Return decodedString
 
    End Function
 
    Public Shared Function GetFileEncoding(srcFile As String) As Encoding
 
        ' Use Default of Encoding.Default (Ansi CodePage)
        Dim enc As Encoding = Encoding.UTF8
 
        ' Detect byte order mark if any - otherwise assume default
        Dim buffer As Byte() = New Byte(4) {}
        Dim file As New FileStream(srcFile, FileMode.Open)
        file.Read(buffer, 0, 5)
        file.Close()
 
        If buffer(0) = &HEF AndAlso buffer(1) = &HBB AndAlso buffer(2) = &HBF Then
            enc = Encoding.UTF8
        ElseIf buffer(0) = &HFF AndAlso buffer(1) = &HFE Then
            enc = Encoding.Unicode
        ElseIf (buffer(0) = &HFE AndAlso buffer(1) = &HFF) Then
            enc = Encoding.BigEndianUnicode
        ElseIf buffer(0) = 0 AndAlso buffer(1) = 0 AndAlso buffer(2) = &HFE AndAlso buffer(3) = &HFF Then
            enc = Encoding.Unicode
        ElseIf buffer(0) = &H2B AndAlso buffer(1) = &H2F AndAlso buffer(2) = &H76 Then
            enc = Encoding.UTF8
        End If
 
        Return enc
 
    End Function

Graeme
Top achievements
Rank 2
 asked on 18 Nov 2014
2 answers
1.0K+ views
Hello, I wonder if you can help with my issue.

In short, I am getting the exception (as in the post's title) when I get to the following line of code:


document = new RadFixedDocument();

RadFixedPage page = document.Pages.AddPage();
                
editor = new FixedContentEditor(page); // <<-- exception here



The things to note are:

1. No exception is thrown when the project is run from Visual Studio.

2. The exception is thrown when the merged assembly is run, that is after it has been merged with ILMerge.

3. The merging worked fine UNTIL I have added the Telerik.WinControls.PdfViewer.dll (and the related Telerik.Windows.Documents.Core.dll and  Telerik.Windows.Documents.Fixed) assemblies to ILMerge script and then tried to instantiate the FixedContentEditor when executing the merged assembly. 

That is, I have been using ILMerge for months with over 20 merged Telerik assemblies--no problem! It's only now that I started instantiating the FixedContentEditor so I assume it is the merged Telerik.Windows.Documents.Fixed.dll assembly that's throwing a wobbly somewhere.

By the way, the ILMerge throws no errors or warnings and the merging completes just fine. When I then run the merged assembly, it runs fine until I access the function that executes the code snipped above (that is, all other Telerik controls render correctly, etc.).

This seems to indicates it is not an issue with the ILMerge process per se. I wonder if there are some weird WPF dependencies somewhere. Even though it is a WinForms project and according to the Telerik documentation the FixedContentEditor can be used in WinForm applications, I noticed I had to refer to WindowsBase.dll in the project references because of two silly lines of code which refer to the System.Windows.Size struct.

    maxWidth = page.Size.Width - defaultLeftIndent * 2;   <<-- does not compile without WindowsBase.dll reference add
     maxHeight = page.Size.Height - currentTopOffset * 2; <<-- does not compile without WindowsBase.dll reference add



I have attached the merge script below and also screenshots my project references and successfully completed ILMerge process.

Thank you!

-----------------
merge.bat below


ilmerge /target:winexe /out:F:\OFFLINE\GULMAY\VMP1_Pro\Setup\VMP2.exe /targetplatform:"v4, C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0" F:\OFFLINE\GULMAY\VMP1_Pro\VMP1Pro\VMP1Pro\bin\Release\VMP2.exe TelerikCommon.dll Telerik.WinControls.UI.dll Telerik.WinControls.dll Telerik.WinControls.Themes.Windows7.dll Telerik.WinControls.Themes.VisualStudio2012Light.dll Telerik.WinControls.Themes.VisualStudio2012Dark.dll  Telerik.WinControls.Themes.TelerikMetroBlue.dll Telerik.WinControls.Themes.TelerikMetro.dll Telerik.WinControls.Themes.Office2013Light.dll Telerik.WinControls.Themes.Office2013Dark.dll Telerik.WinControls.Themes.Office2010Silver.dll Telerik.WinControls.Themes.Office2010Blue.dll  Telerik.WinControls.Themes.HighContrastBlack.dll Telerik.WinControls.Themes.Desert.dll Telerik.WinControls.Themes.Breeze.dll Telerik.WinControls.Themes.Aqua.dll Telerik.WinControls.GridView.dll Telerik.Windows.Documents.Core.dll Telerik.Windows.Documents.Fixed.dll Telerik.Windows.Zip.dll Telerik.WinControls.PdfViewer.dll 











gulmay
Top achievements
Rank 1
 answered on 07 Nov 2014
1 answer
292 views
hi,

I'm trying to import an existing word template into a RadFlowDocument, edit it by filling the merge fields and display it on the screen (read-only).
Can anyone please assist me with this. The word templates also contains a header, which contains images and text boxes.
The File.OpenRead command returns an error, as it is unable to read the document into a stream (object reference not set to an instance of an object).
Petya
Telerik team
 answered on 21 Oct 2014
1 answer
332 views
Hi,

When I generate a Worksheet with foreground colors for every row this is slow for small Excel files (say 1000 rows and 25 columns) and runs out memory for a 4000 row by 25 column Excel (the .xls file without the colors is about 400k). When I comment out the color code below it is significantly faster (about 5 times) and doesnt run out of memory.

Regards,

Bayram


private static Workbook CreateWorkbook(RadGridView grid, IEnumerable<string> skipColumns)
{
    Workbook book = new Workbook();
    book.Sheets.Add(SheetType.Worksheet);
    Worksheet sheet = book.ActiveWorksheet;
 
    Color accentBorder = Office2013Palette.Palette.AccentColor;
    Color transparent = Colors.Transparent;
    Color black = Colors.Black;
    Color white = Colors.White;
 
    ThemableColor themeBlack = new ThemableColor(Color.FromArgb(0,0,0,0));
    ThemableColor themeWhite = new ThemableColor(Color.FromArgb(0, 255, 255, 255));
 
    CellBorder border = new CellBorder(CellBorderStyle.Thin, themeBlack);
    CellBorder noBorder = new CellBorder(CellBorderStyle.None, themeBlack);
    CellBorders borders = new CellBorders(border, border, border, border, border, border, noBorder, noBorder);
 
    int rowNumber = 0;
    int colNumber = 0;
    foreach (var column in grid.Columns)
    {
        if (column.Header is string)
        {
            string columnHeader = (string)column.Header;
            if (skipColumns == null || !skipColumns.Contains(columnHeader))
            {
                sheet.Cells[rowNumber, colNumber].SetValue(columnHeader);
                colNumber++;
            }
        }
    }
    CellSelection selection = sheet.Cells[0,0,0,colNumber-1];
    selection.SetIsBold(true);
    PatternFill solidPatternFill = new PatternFill(PatternType.Solid, accentBorder, transparent);
    selection.SetFill(solidPatternFill);
    selection.SetForeColor(themeWhite);
 
    HashSet<int> textColumns = new HashSet<int>();
    rowNumber++;
    foreach (var item in grid.Items)
    {
        if (item is DynRecord)
        {
            DynRecord record = (DynRecord)item;
            colNumber = 0;
 
            foreach (var column in grid.Columns)
            {
                if (column.Header is string)
                {
                    string columnHeader = (string)column.Header;
                    if (skipColumns == null || !skipColumns.Contains(columnHeader))
                    {
                        if (column is GridViewDataColumn)
                        {
                            GridViewDataColumn dataColumn = (GridViewDataColumn)column;
                            string propName = dataColumn.DataMemberBinding.Path.Path;
 
                            object value = record.Value(propName);
                            if (value != null)
                            {
                                if (value is string)
                                {
                                    textColumns.Add(colNumber);
                                    sheet.Cells[rowNumber, colNumber].SetValue((string)value);
                                }
                                else if (value is DateTime)
                                    sheet.Cells[rowNumber, colNumber].SetValue((DateTime)value);
                                else if (value is Decimal)
                                    sheet.Cells[rowNumber, colNumber].SetValue(Convert.ToDouble((decimal)value));
                                else
                                {
                                    textColumns.Add(colNumber);
                                    sheet.Cells[rowNumber, colNumber].SetValue(value.ToString());
                                }
                            }
                        }
                        colNumber++;
                    }
                }
            }
            // This part is slow and runs out of memory
            //Color color = record.GetRecordColor().Color;
            //if (color != Colors.Black)
            //{
            //    selection = sheet.Cells[rowNumber, 0, rowNumber, colNumber - 1];
            //    selection.SetForeColor(new ThemableColor(color));
            //}
 
            rowNumber++;
        }
    }
    selection = sheet.Cells[0, 0, rowNumber-1, colNumber - 1];
    selection.SetBorders(borders);
 
 
    // Set Text columns formatting to @ to prevent Excel autoformatting
    foreach (int i in textColumns)
    {
        selection = sheet.Cells[1, i, rowNumber - 1, i];
        selection.SetFormat(new CellValueFormat("@"));
    }
 
    // AutoFitWidth
    for (int i = 0; i < colNumber - 1; i++)
    {
        ColumnSelection columnSelection = sheet.Columns[i];
        columnSelection.AutoFitWidth();
        double width = sheet.Columns[i].GetWidth().Value.Value * 1.1;
        columnSelection.SetWidth(new ColumnWidth(width, false));
    }
 
    return book;
}
Anna
Telerik team
 answered on 17 Oct 2014
1 answer
129 views
In excel you can open a document and go to File->Properties where in custom tab you can enter a custom property (with value) to the document.

Is it possible to do this in an excel workbook created with SpreadProcessing?
Nikolay Demirev
Telerik team
 answered on 10 Oct 2014
5 answers
212 views
Hi,

Setting:

TextProperties.Font = FontsRepository.Helvetica

Editor.DrawText("æøåÆØÅ")

Gives an error

Any setting for nordic letters I am missing?

Regards,
Brian
Kammen
Telerik team
 answered on 30 Sep 2014
1 answer
101 views
Hi,

When trying to overwrite an already created pdf file using the following code:

      Dim OutStream As New FileStream(Me.FileName, FileMode.Create)
      PdfFormatProvider.Export(Me.PDFDocument, OutStream)
      OutStream.Close()

I get an error "File is being used by an other process"

What do I miss in ending the first operation?

Regards,
Brian
Kammen
Telerik team
 answered on 29 Sep 2014
1 answer
79 views
Hi,

Using Clipping and having defined a clipping rectangle: I am searching for a "IsVisible" function like the one in System.Drawing.Graphics.

The purpose is to reduce file size by excluding eg. text, which will not be visible because of the clipping.

Regards,
Brian
Kammen
Telerik team
 answered on 29 Sep 2014
1 answer
201 views
Hi,

I would like to apply multiple transformation to FixedContentEditor.Position

However, the MatrixPosition class seems not to apply to the FixedContentEditor

And eg. the Editor.Position.Matrix.TranslatePrepend function does not seem to work, because Editor.Position.Matrix is readonly.

Regards,
Brian
Kammen
Telerik team
 answered on 29 Sep 2014
1 answer
618 views
Hi,

Your example shows how to insert jpg image from file:

Using stream As Stream = Me.GetResourceStream("Telerik_logo.jpg")
editor.DrawImage(stream, ImageFormat.Jpeg, New Size(118, 28))
End Using

I would like to insert image from .NET Image class variable. The images are stored in Base64 format in a database.

Could you provide a code snippet for that?

Regards,
Brian
Kammen
Telerik team
 answered on 29 Sep 2014
Narrow your results
Selected tags
Tags
+? more
Top users last month
Boardy
Top achievements
Rank 2
Veteran
Iron
Benjamin
Top achievements
Rank 3
Bronze
Iron
Veteran
ivory
Top achievements
Rank 1
Iron
Iron
Rob
Top achievements
Rank 3
Bronze
Bronze
Iron
ClausDC
Top achievements
Rank 2
Iron
Iron
Iron
Want to show your ninja superpower to fellow developers?
Top users last month
Boardy
Top achievements
Rank 2
Veteran
Iron
Benjamin
Top achievements
Rank 3
Bronze
Iron
Veteran
ivory
Top achievements
Rank 1
Iron
Iron
Rob
Top achievements
Rank 3
Bronze
Bronze
Iron
ClausDC
Top achievements
Rank 2
Iron
Iron
Iron
Want to show your ninja superpower to fellow developers?
Want to show your ninja superpower to fellow developers?