This project has moved and is read-only. For the latest updates, please go here.

Changing language of the document

Sep 29, 2009 at 11:24 PM

Hello,

Is there a way to change language of the document. By default it's English (Ireland) which makes it highlight red on everything in my polish documents. How would i change it?

With regards,

MadBoy

Aug 19, 2011 at 9:13 AM

I have the same problem, if you found how to fix it, please contact.

Aug 19, 2011 at 9:18 AM

Unfortunetly I didn't :/

Aug 19, 2011 at 12:15 PM

I really like this library, but this bug is very annoying. So I spend some time to fix it.
Maybe it's not the best way to solve this problem, but it works very well for me.

First of all, you should change class Formatting:

Add this code:

 

. . .
using System.Globalization;

namespace Novacode
{
    public class Formatting
    {
        . . .
        private CultureInfo language;

        public Formatting()
        {
            . . .

            // Use current culture by default
            language = CultureInfo.CurrentCulture;

            . . .
        }

        internal XElement Xml
        {
            get
            {
                . . .

                if (language != null)
                {
                    rPr.Add(new XElement(XName.Get("lang", DocX.w.NamespaceName), new XAttribute(XName.Get("val", DocX.w.NamespaceName), language.Name)));
                }

                . . .
            }
        }

        . . .

        /// <summary>
        /// Text language
        /// </summary>
        public CultureInfo Language { get { return language; } set { language = value; } }

        . . .
    }
}

 

Then add this code to your Paragraph class:

 

        /// <summary>
        /// For use with Append() and AppendLine()
        /// </summary>
        /// <returns>This Paragraph in curent culture</returns>
        /// <example>
        /// Add a new Paragraph with russian text to this document and then set language of text to local culture.
        /// <code>
        /// // Load a document.
        /// using (DocX document = DocX.Create(@"Test.docx"))
        /// {
        ///     // Insert a new Paragraph with russian text and set curent local culture to it.
        ///     Paragraph p = document.InsertParagraph("Привет мир!").CurentCulture();
        ///       
        ///     // Save this document.
        ///     document.Save();
        /// }
        /// </code>
        /// </example>
        public Paragraph CurentCulture()
        {
            ApplyTextFormattingProperty(XName.Get("lang", DocX.w.NamespaceName),
                string.Empty,
                new XAttribute(XName.Get("val", DocX.w.NamespaceName), CultureInfo.CurrentCulture.Name));
            return this;
        }

        /// <summary>
        /// For use with Append() and AppendLine()
        /// </summary>
        /// <param name="culture">The CultureInfo for text</param>
        /// <returns>This Paragraph in curent culture</returns>
        /// <example>
        /// Add a new Paragraph with russian text to this document and then set language of text to local culture.
        /// <code>
        /// // Load a document.
        /// using (DocX document = DocX.Create(@"Test.docx"))
        /// {
        ///     // Insert a new Paragraph with russian text and set specific culture to it.
        ///     Paragraph p = document.InsertParagraph("Привет мир").Culture(CultureInfo.CreateSpecificCulture("ru-RU"));
        ///       
        ///     // Save this document.
        ///     document.Save();
        /// }
        /// </code>
        /// </example>
        public Paragraph Culture(CultureInfo culture)
        {
            ApplyTextFormattingProperty(XName.Get("lang", DocX.w.NamespaceName), string.Empty,
                new XAttribute(XName.Get("val", DocX.w.NamespaceName), culture.Name));
            return this;
        }

Now it will be work fine with Append() too.

 

That's all. Maybe I missed something, because I did not have enough time to explore whole library.

With regards, ArtFeel (=


 

Aug 19, 2011 at 12:19 PM

Wow! Great! I wish you would join up with Cathal and help him a bit. He could use a team mate :-) I spoke to him many times and he seems to be very busy and looking for someone he could develop with. This library is great and very easy to use comparing to others. And best of all it's free...

Aug 19, 2011 at 1:11 PM

ArtFeel,

This is a great patch. I would be very happy to add you as a dev to docx. This way you can add this and any other patches you want to the source tree.

Let me know if your interested in joining the dev team.

Kind regards,
Cathal

On 19 Aug 2011 12:19, "MadBoy" <notifications@codeplex.com> wrote:
> From: MadBoy
>
> Wow! Great! I wish you would join up with Cathal and help him a bit. He could use a team mate :-) I spoke to him many times and he seems to be very busy and looking for someone he could develop with. This library is great and very easy to use comparing to others. And best of all it's free...
>
>
Dec 30, 2011 at 10:55 AM

This still isn't integrated into source it seems. Can someone please integrate it into main source?

Dec 30, 2011 at 1:18 PM
Madboy,

I have just added ArtFree's patch to the source tree. Can you please download and test this.

Sorry this took so long to integrate, it is a great patch and should have been added long ago.

Kind regards,
Cathal
Dec 30, 2011 at 1:22 PM
Edited Dec 30, 2011 at 1:23 PM

Great. Will test it out and report back. On another note i finally moved myself to 1.0.0.11.. at least it seems so. It was hell since the code i was using was a mess .. it seems there were lost of changes between 1.0.0.10 and 1.0.0.11 since even order of tables, etc in code changed after I started using 1.0.0.11 (had to introduce paragraphs for tables and so on), creating tables also had order from column, rows to rows and columns but with more features coming it's a good thing to finally switch (at least some apps to new versions). And i finally found out the bug we worked on. It's reported in issues section. Hope this helps.

Dec 30, 2011 at 1:56 PM
Madboy,

I have also just fixed and closed the issue you mentioned above. If you have the time can you please verify this also.

Happy coding,
Dec 30, 2011 at 2:04 PM

Thanks for that.

Could this also be a reason why my code from 1.0.0.10 that used to work fine .. in 1.0.0.11 i had to rebuild it to not use document.InsertTable but more like paragraph.InsertTableAfterSelf ? Otherwise the tables and text was so much mixed .. like last table shown up as 1st etc ? It took me several hours to rebuild doc creation but I thought that there was so much change between versions and that's why it's working like it is?

Dec 30, 2011 at 2:21 PM
MadBoy,

I am not entirely sure. I am sometimes amazed at the documents Word will manage to open :-)

Cathal
Dec 30, 2011 at 2:28 PM

It should be simply reproducible.. TEST word will be below TABLE... imagine my surprise .. i wonder why no one noticed this before? Or maybe it's a feature I wasn't aware but this worked fine in 1.0.0.10 :-) Now it makes a big mess .. well haven't tested after you patched it ..

        private static void test1() {
            using (var stream = new MemoryStream()) {
                using (DocX documentWord = DocX.Create(stream)) {

                    Paragraph paragraph = documentWord.InsertParagraph("TEST");

                    Table varUkladTabeli = documentWord.InsertTable(2, 2);
                    varUkladTabeli.Alignment = Alignment.center;
                    varUkladTabeli.Design = TableDesign.TableNormal;
                    varUkladTabeli.AutoFit = AutoFit.Window;
                    varUkladTabeli.Rows[0].Cells[0].Width = 500;
                    varUkladTabeli.Rows[0].Cells[1].Width = 500;
                    documentWord.SaveAs("C:\\test.docx");
                }
            }
        }

Dec 30, 2011 at 2:44 PM
MadBoy,

when I run this example (with the new patch) the Paragraph TEST is above the Table.
This is as expected because when you edit a document you normally insert at the bottom.

Can you verify on your machine?

Kind Regards,
Dec 30, 2011 at 2:46 PM

Yes I will test it. Just before patch it's below :-) So wonder how people lived before :-p or like me they worked around.

Dec 30, 2011 at 3:09 PM
Edited Dec 30, 2011 at 3:10 PM

I've tested newest version and .. tada tada:

1) example 1- passed

 

 private static void test(string rTyp) {
            using (var stream = new MemoryStream()) {
                string varTemplate;
                if (rTyp.ToLower() == "Raport Miesi&#281;czny".ToLower()) {
                    varTemplate = Path.Combine(Path.GetTempPath(), "raport_miesieczny.docx"); //  // @"C:\raport_miesieczny.docx";   
                    SqlMethods.templateDokumentuPobierz("RaportMiesieczny", varTemplate);
                } else {
                    varTemplate = Path.Combine(Path.GetTempPath(), "raport_kwartalny.docx"); // @"C:\raport_kwartalny.docx";
                    SqlMethods.templateDokumentuPobierz("RaportKwartalny", varTemplate);
                }
                using (DocX documentWord = DocX.Load(varTemplate)) {
                    Table varUkladTabeli = documentWord.InsertTable(2, 2);
                    varUkladTabeli.Alignment = Alignment.center;
                    varUkladTabeli.Design = TableDesign.TableNormal;
                    varUkladTabeli.AutoFit = AutoFit.Window;
                    varUkladTabeli.Rows[0].Cells[0].Width = 500;
                    varUkladTabeli.Rows[0].Cells[1].Width = 500;
                    documentWord.SaveAs("C:\\test.docx");
                }
            }
        }






 
2) example 2- passed

      
 private static void test1() {
            using (var stream = new MemoryStream()) {
                using (DocX documentWord = DocX.Create(stream)) {

                    Paragraph paragraph = documentWord.InsertParagraph("TEST");

                    Table varUkladTabeli = documentWord.InsertTable(2, 2);
                    varUkladTabeli.Alignment = Alignment.center;
                    varUkladTabeli.Design = TableDesign.TableNormal;
                    varUkladTabeli.AutoFit = AutoFit.Window;
                    varUkladTabeli.Rows[0].Cells[0].Width = 500;
                    varUkladTabeli.Rows[0].Cells[1].Width = 500;
                    documentWord.SaveAs("C:\\test11.docx");
                }
            }
        }


3) Example 3 ...

My whole old document creation code works without flaws.. this means I wasted several hours fixing things :-)
But at least we have it fixed now and I learnt some new things while trying to fix the problem assuming my code is badly written (it is anyway) :)
Dec 30, 2011 at 3:16 PM

Also the currentCulture seems to work fine .. and language is changed to Polish. Just this little code demonstrates that TEST word is in Polish but if you go down and click next to the table it still says Irish.. however if you put in text into it the text is treated as polish as well. So not necessary a thing to fix but it still jumps to irish on empty tables. And if somone starts to type things in into those tables manually it's treated as Irish ;)

        private static void test1() {
            using (var stream = new MemoryStream()) {
                using (DocX documentWord = DocX.Create(stream)) {

                    Paragraph paragraph = documentWord.InsertParagraph("TEST");

                    Table varUkladTabeli = documentWord.InsertTable(2, 2);
                    varUkladTabeli.Alignment = Alignment.center;
                    varUkladTabeli.Design = TableDesign.TableNormal;
                    varUkladTabeli.AutoFit = AutoFit.Window;
                    varUkladTabeli.Rows[0].Cells[0].Width = 500;
                    varUkladTabeli.Rows[0].Cells[1].Width = 500;
                    documentWord.SaveAs("C:\\test11.docx");
                }
            }
        }

Dec 30, 2011 at 3:54 PM

It seems so more things aren't affected by culture change. Using this code does use Irish

                    Table ukladTabeliPodsumowaniaPortfela = documentWord.InsertTable(1, 2);
                    //Table ukladTabeliPodsumowaniaPortfela = documentWord.InsertTable(2, 1);
                    ukladTabeliPodsumowaniaPortfela.Alignment = Alignment.center;
                    ukladTabeliPodsumowaniaPortfela.Design = TableDesign.TableNormal;
                    ukladTabeliPodsumowaniaPortfela.AutoFit = AutoFit.Window;
                    ukladTabeliPodsumowaniaPortfela.Rows[0].Cells[0].Width = 500;
                    ukladTabeliPodsumowaniaPortfela.Rows[0].Cells[1].Width = 500;
                    documentWord.InsertParagraph().AppendLine("");
                    documentWord.InsertParagraph().AppendLine("");
                    Paragraph paragraphSumaPortfelaLeft = ukladTabeliPodsumowaniaPortfela.Rows[0].Cells[0].Paragraphs[0].Append("Ca&#322;kowita warto&#347;&#263; portfela: ").Bold().FontSize(9).Font(fontCalibri);
                    paragraphSumaPortfelaLeft.Alignment = Alignment.left;
                    Paragraph paragraphSumaPortfelaRight = ukladTabeliPodsumowaniaPortfela.Rows[0].Cells[1].Paragraphs[0].Append(Locale.returnZloty(sumaWycen)).Bold().Italic().UnderlineStyle(UnderlineStyle.doubleLine).FontSize(9).Font(fontCalibri);
                    paragraphSumaPortfelaRight.Alignment = Alignment.right;

 

Dec 30, 2011 at 4:02 PM
Hi MadBoy,

sorry that you wasted so many hours, this was entirely my fault.
It was careless of me to introduce such a bug.

I am very happy to hear all of your tests passed.

About the default culture remaining as Irish for the document. I think this is because by default DocX.Create() uses en-ie. I will search through the code and try and use CurrentCulture wherever I have previously used en-ie.

Kind Regards,
Cathal
Dec 30, 2011 at 4:05 PM

No worries :-) It wasn't your fault. It's just I thought such bug would be "found" by people using 1.0.0.11 quite fast as it made some features hard to use. The good thing about "wasted" hours is that code is a bit cleaner now and uses some charts :-) So really no worries. I found couple of things while fixing it.

Dec 30, 2011 at 4:38 PM
MadBoy,

I have made a new fix for language. I am hoping it fixes the two issue you found below.

>> but it still jumps to irish on empty tables
>> if somone starts to type things in into those tables manually it's treated as Irish ;)

Can you please download and verify?

Kind regards,
Cathal
Dec 30, 2011 at 4:52 PM

Hello,

I verified it and it seems to work just fine. Thanks for this quick Language Fix. I can finally use 1.0.0.11 and drop old 1.0.0.10 and support you in some good words and reports if everything works as should.

However merging in newest version still doesn't work as it should. I have this simple code and I merge 2,4,100 documents that were created with DocX (headers, footers with pictures and tables, and so on) into one big document. After merging file cannot be opened. If you wish I can provide you on mail with 2 documents that don't want to merge.

 

        public static void documentsMerge(object fileName, List<string> arrayList) {
            bool varTest = deleteFile(fileName.ToString());
            if (varTest) {
                using (DocX documentToCreate = DocX.Create(fileName.ToString())) {
                    foreach (var alItem in arrayList) {
                        DocX documentToMergeIn = DocX.Load(alItem);
                        documentToCreate.InsertDocument(documentToMergeIn);
                        documentToCreate.InsertParagraph().InsertPageBreakAfterSelf();
                    }
                    documentToCreate.Save();
                }
            }
        }

Dec 30, 2011 at 5:11 PM
MadBoy,

unfortunately the MergeDocument functions are still young. If you could do, as you did before, provide me with the first two documents that refuse to merge. That would be great.

Regards,
Cathal

Dec 30, 2011 at 5:28 PM

Hello,

Sent you an email with 2 docs.If it will be too much work I guess I can work a bit on "trimming" down documents and trying to find which thing makes the biggest problems (is it header, footer, tables or texts). Maybe some threads will help:

http://docx.codeplex.com/discussions/81282 -> claims custom properties is the problem

http://docx.codeplex.com/discussions/274880 -> images .. seems fixed but maybe it's not :-)

With regards.

MadBoy