-1

I want to compare two strings with wingdings characters. Because I don't know how to capture wingdings codes in C# strings, then I can't have a realiable comparaison between two strings...

Context : C#/Word

Problem description

I have a word document with a checkbox. This checkbox is a wingding. My challenge is to say if this checkedbox is checked or not using C#.

I know that the checkbow is checked if the hundled string is equal to one of this wingding character &#120, &#253 and &#254

see the list of wingdings

otherwise the checkbox is unchecked.

After your help

After your help, I have a solution to my problem :), Now, I have another problem :))

As I said in the top, all this is to compare the wingding character (checkbox) to all possible wingdings representing a checked checkbox. Now, When I compare directly the checkbox in the word with the encoded wingding I don t find the character because it's encoded in other format...

BUT when I create a word document and I put in the encoded character and then I compare the character I created with the target word doc I HAVE WHAT I WANT :) But this solution as you see need to create a new word doc :(

Any way, here's my solution :)

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Office.Interop.Word;

namespace Project0
{
    class Program
    {
        static bool isChecked(Range rng)
        {
            var wrdApp = new Application();
            wrdApp.Visible = true;
            Document wrdDoc = wrdApp.Documents.Add();
            Range Rngg = wrdDoc.Paragraphs[1].Range;
            Rngg.Font.Name = "WingDings";   // Why I CREATE A SECOND DOC - I Need this font ;(
            List<string> list = new List<string>{ "\u00fe", "\u00fd", "\u0078" };
            foreach (string k in list)
            {
                if (rng.Text.Contains(Rngg.Text))
                {
                    return true;
                }
            }
            return false;
        }

        static void Main(string[] args)
        {
            List<string> list = new List<string> { "\u00fe", "\u00fd", "\u0078" };

            //            string wPath = @"C:\DV\test.docx";
            //           Document wrdDoc = wrdApp.Documents.Open(FileName: wPath, ReadOnly: false);

            var wrdApp = new Application();
            wrdApp.Visible = true;
            Document wrdDoc = wrdApp.Documents.Add();

            // I SIMULATE MY DOC
            wrdDoc.Paragraphs[1].Range.Font.Name = "WingDings";
            wrdDoc.Paragraphs[1].Range.Text = list[1];  // I put a checked box

            // CAN I DETERMINE IF THERE IS A CHECKBOX
            if (isChecked(wrdDoc.Paragraphs[1].Range))
            {
                Console.WriteLine("Heeah, there's a checked checkbox !!");
            }
        }
    }
}

Thanks in advance for all your help :)

Gauss
  • 21
  • 1
  • 5

2 Answers2

1

When you declare a string as

string x="♓■♑⬧♎♓■♑⬧";  //Wingdings characters

.NET saves that string as UTF-16 encoded unicode code points.

So, it really doesn't matter what font or symbol that you are using to declare the string. Even if you do not have proper font file to display in your machine, you could still save that font value using \u notation. That means directly writing the unicode code points:

Something like:

string test= "\u20AC";
//Also can be declared as: 
string test=     "€" 
//Both are same

Hence the point is, you could still "save" any "characters" to your string and do the comparison. And you have to specify whole lots of other things like culture etc if you want to get the correct collation order. However, this is quite a wide subject and not possible to write everything in this answer.

Look at this msdn page for more details/options on string.compare

EDIT -> After the updated question:

Assuming that you are reading the values from UTF-8 encoded source, you could do something like this:

string yourSourceStringInUTF8="asdfasdýadfasdfasdf";
//These are the wingdings characters in UTF-8 encoded hex format 
List<string> list = new List<string> { "\u00fe", "\u00fd", "\u0078" };
bool found = false;
foreach (string s in list)
{
    found = yourSourceStringInUTF8.Contains(s);
    if (found)
    {
        break;
    }
}
Console.WriteLine(found? "found":"Notfound");

EDIT

There are various ways to get/try find the encoding of a file. Some are:


public static Encoding GetEncoding(string filename)
{
    // Read the BOM
    var bom = new byte[4];
    using (var file = new FileStream(filename, FileMode.Open, FileAccess.Read))
    {
        file.Read(bom, 0, 4);
    }
// Analyze the BOM
    if (bom[0] == 0x2b && bom1 == 0x2f && bom2 == 0x76) return Encoding.UTF7;
    if (bom[0] == 0xef && bom1 == 0xbb && bom2 == 0xbf) return Encoding.UTF8;
    if (bom[0] == 0xff && bom1 == 0xfe) return Encoding.Unicode; //UTF-16LE
    if (bom[0] == 0xfe && bom1 == 0xff) return Encoding.BigEndianUnicode; //UTF-16BE
    if (bom[0] == 0 && bom1 == 0 && bom2 == 0xfe && bom[3] == 0xff) return Encoding.UTF32;
    return Encoding.ASCII;
}

OR


      using (StreamReader reader = new StreamReader(fileName, defaultEncodingIfNoBom, true))
  {
      reader.Peek(); // you need this!
      var encoding = reader.CurrentEncoding;
  }

The code snippet are taken from this stackoverflow link

Community
  • 1
  • 1
ANewGuyInTown
  • 5,957
  • 5
  • 33
  • 45
  • Hello friends, I just posted a detail of my problem :) – Gauss Feb 10 '16 at 15:22
  • Thanks ANewGuyInTown !!! I updated the question :) There's no end for this problem :). (my last code recquire MS Word / Visual Studio) – Gauss Feb 11 '16 at 03:22
  • Nothing you can do , if you do not know the encoding. This has been a major problems of _internet_ itself. If the file doesn't have any encoding meta data, then all you can do is guess. Nevertheless, there are intelligent guesses which gets correct fair amount of time – ANewGuyInTown Feb 11 '16 at 23:00
  • Thank you again ANewGuyInTown !!! To be honest, I don't understand how can I use your answer to fix my problem :) even if your code works :) – Gauss Feb 20 '16 at 23:25
  • Assuming you are not sure about the encoding of your file and that's where the problem is, what I can suggest is: use the method `GetEncoding` to find your file's encoding, after that create a `method`, which takes the `encoding` returned from the previous call as a `parameter` and returns `List` containing encoded `wingdings characters`, (you'll have to find out these characters for each encodings returned from `GetEncoding` method, above example is only for `UTF-8`) then use that returned `List` to compare your file. (as in first example) – ANewGuyInTown Feb 22 '16 at 01:43
0

Well when you compare strings it will be compared by their values.

So you can just take your string with windings ,in a string variable and just use string.comapare() method to compare it. But, I think you should elaborate more on your problem and there will be ans.