void ListViewSelectionChange(object sender, SelectionChangedEventArgs args) { //WE GET THE SELECTED ITEM LISTVIEW Contracts selected = (Contracts)lvUsers.SelectedItem; string content = System.IO.File.ReadAllText($@"C:\Users\gnpie\OneDrive\Escritorio\proyectos\Portal Ciudadano\{ListViewHeaderSet.ToString()}\{selected.Name}.txt"); //ON THE RELATED .TXT WE SEEK THE VARIABLES CONTAINED ON IT List <int> listOfLegalDocumentVariablesIndices = new List <int>(); bool variableFinded = true; int lastsearching = 0; while (variableFinded == true) { int startIndex = content.IndexOf("${", lastsearching); if (startIndex == -1) { variableFinded = false; } if (startIndex != -1) { listOfLegalDocumentVariablesIndices.Add(startIndex + 2); } int endIndex = content.IndexOf("}", lastsearching); if (endIndex != -1) { listOfLegalDocumentVariablesIndices.Add(endIndex); } lastsearching = endIndex + 1; } int iterator = 0; List <string> listOfLegalDocumentVariablesNames = new List <string>(); //HERE ARE THE VARIABLES FOR THE DOCUMENT BUILDING while (iterator <= listOfLegalDocumentVariablesIndices.Count - 2) { int length = listOfLegalDocumentVariablesIndices[iterator + 1] - listOfLegalDocumentVariablesIndices[iterator]; string piece = content.Substring(listOfLegalDocumentVariablesIndices[iterator], length); listOfLegalDocumentVariablesNames.Add(piece); iterator = iterator + 2; } //HERE GOES THE XALM MANIPULATION if (listOfLegalDocumentVariablesNames.Count > 0) { int i = 0; StackLayoutMap.Children.Clear(); do { IntegerConverter integerConverter = new IntegerConverter(); string textboxName = integerConverter.Convert(i + 1).Trim(); var label = new Label { Content = listOfLegalDocumentVariablesNames[i] }; var textbox = new TextBox(); if (listOfLegalDocumentVariablesNames[i] == "ANTECEDENTES") { textbox = new TextBox { Text = "", Name = textboxName, Height = 180, TextWrapping = TextWrapping.Wrap }; } else { textbox = new TextBox { Text = "", Name = textboxName }; } StackLayoutMap.Children.Add(label); StackLayoutMap.Children.Add(textbox); textboxesList.Add(textboxName); i = i + 1; }while (i < listOfLegalDocumentVariablesNames.Count); } }
private void WriteUp_Click(object sender, RoutedEventArgs e) { Console.WriteLine(ListViewHeaderSet); IntegerConverter integerConverter = new IntegerConverter(); //WE GET THE SELECTED ITEM LISTVIEW Contracts selected = (Contracts)lvUsers.SelectedItem; string content = System.IO.File.ReadAllText($@"C:\Users\gnpie\OneDrive\Escritorio\proyectos\Portal Ciudadano\{ListViewHeaderSet.ToString()}\{selected.Name}.txt"); //ON THE RELATED .TXT WE SEEK THE VARIABLES CONTAINDE ON IT List <int> listOfLegalDocumentVariablesIndices = new List <int>(); bool variableFinded = true; int lastsearching = 0; while (variableFinded == true) { int startIndex = content.IndexOf("${", lastsearching); if (startIndex == -1) { variableFinded = false; } if (startIndex != -1) { listOfLegalDocumentVariablesIndices.Add(startIndex + 2); } int endIndex = content.IndexOf("}", lastsearching); if (endIndex != -1) { listOfLegalDocumentVariablesIndices.Add(endIndex); } lastsearching = endIndex + 1; } int iterator = 0; List <string> listOfLegalDocumentVariablesNames = new List <string>(); while (iterator <= listOfLegalDocumentVariablesIndices.Count - 2) { int length = listOfLegalDocumentVariablesIndices[iterator + 1] - listOfLegalDocumentVariablesIndices[iterator]; string piece = content.Substring(listOfLegalDocumentVariablesIndices[iterator], length); listOfLegalDocumentVariablesNames.Add(piece); iterator = iterator + 2; } // INITIALIZATION OF WORD DOCUMENT Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); word.Documents.Add(); word.Visible = true; word.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone; Microsoft.Office.Interop.Word.Document doc = word.ActiveDocument; //WE FIND THE SELECTED CASE DATA Client selectedClient = new Client(); foreach (Client item in AllClients) { if (item.clients_name == lbUsers.Text) { selectedClient = item; } } //WE STORE THE INPUTS (TEXTBOXES) CONTENT, FOR THE DOCUMENT CONSTRUCTOR int maxLoops = VisualTreeHelper.GetChildrenCount(StackLayoutMap); Dictionary <string, string> labelsAndTextBoxesDict = new Dictionary <string, string>(); for (int i = 0; i < maxLoops; i = i + 2) { string xamlLabelEle = VisualTreeHelper.GetChild(StackLayoutMap, i).ToString(); //ver lector de xml xdoc string xamlTextBoxEle = ""; if (i == maxLoops - 1) { xamlTextBoxEle = VisualTreeHelper.GetChild(StackLayoutMap, i).ToString(); } else { xamlTextBoxEle = VisualTreeHelper.GetChild(StackLayoutMap, i + 1).ToString(); } int startOnTextBox = xamlTextBoxEle.IndexOf("TextBox: ") + 9; int startOnLabel = xamlLabelEle.IndexOf("Label: ") + 7; string textPropertiOfXamlElementTextBox = xamlTextBoxEle.Substring(startOnTextBox); //HERE WE GET THE CONTENT OF TEXTBOX, WITH A TRICK string contentPropertiOfXamlElementLabel = xamlLabelEle.Substring(startOnLabel); labelsAndTextBoxesDict.Add(contentPropertiOfXamlElementLabel, textPropertiOfXamlElementTextBox); } // SECTIONS VARIABLES string rut = selectedClient.clients_rut; string[] rutArr = rut.Split('-'); string verifyingDigit = integerConverter.Convert(Convert.ToInt64(rutArr[1])); string theRestOFTheRut = rutArr[0].Replace(".", ""); long theRestOFTheRutInteger = Convert.ToInt64(theRestOFTheRut); string theRestOFTheRutOnWords = integerConverter.Convert(theRestOFTheRutInteger); string stringifiedRut = theRestOFTheRutOnWords + " guion " + verifyingDigit; string introduction = $"EN SANTIAGO, REPÚBLICA DE CHILE, a {integerConverter.Convert(DateTime.Today.Day)} de {DateTime.Now.ToString("MMMM")} del año {integerConverter.Convert(DateTime.Today.Year)}, ante mí, R. ALFREDO MARTÍN ILLANES, abogado, Notario Púbico Titular de la Décimo Quinta Notaria de Santiago, con oficio en calle Mardoqueo Fernández doscientos uno, oficinas ciento uno y ciento dos, comuna de Providencia, comparece: "; string comparecencia = $"{selectedClient.clients_name}, {selectedClient.clients_nationality}, {selectedClient.clients_civilStatus}, {selectedClient.clients_job}, cédula nacional de identidad número {stringifiedRut}"; string prefacio = @" el compareciente mayor de edad, quien me acredita su identidad con la cédula personal antes citada y expone: "; //SECTION 1.a Microsoft.Office.Interop.Word.Paragraph paragraph1A = doc.Content.Paragraphs.Add(System.Reflection.Missing.Value); paragraph1A.Range.Text = (selected.Name).ToUpper(); paragraph1A.Range.Font.Size = 14; paragraph1A.Range.Font.Bold = 1; paragraph1A.Range.Font.Name = "Helvetica"; paragraph1A.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; paragraph1A.Range.InsertParagraphAfter(); //SECTION 1.b Microsoft.Office.Interop.Word.Paragraph paragraph1B = doc.Content.Paragraphs.Add(System.Reflection.Missing.Value); paragraph1B.Range.Text = selectedClient.clients_name.ToUpper(); paragraph1B.Range.Font.Size = 14; paragraph1B.Range.Font.Bold = 1; paragraph1B.Range.Font.Name = "Helvetica"; paragraph1B.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; paragraph1B.Range.InsertParagraphAfter(); //SECTION 1.c Microsoft.Office.Interop.Word.Paragraph paragraph1C = doc.Content.Paragraphs.Add(System.Reflection.Missing.Value); paragraph1C.Range.Text = "A"; paragraph1C.Range.Font.Size = 14; paragraph1C.Range.Font.Bold = 1; paragraph1C.Range.Font.Name = "Helvetica"; paragraph1C.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; paragraph1C.Range.InsertParagraphAfter(); //SECTION 1.d Microsoft.Office.Interop.Word.Paragraph paragraph1D = doc.Content.Paragraphs.Add(System.Reflection.Missing.Value); paragraph1D.Range.Text = "MEMIN"; paragraph1D.Range.Font.Size = 14; paragraph1D.Range.Font.Bold = 1; paragraph1D.Range.Font.Name = "Helvetica"; paragraph1D.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter; paragraph1D.Range.InsertParagraphAfter(); //SECTION 2 Microsoft.Office.Interop.Word.Paragraph paragraph2 = doc.Content.Paragraphs.Add(System.Reflection.Missing.Value); paragraph2.Range.Text = introduction + comparecencia + prefacio; paragraph2.Range.Font.Size = 12; paragraph2.Range.Font.Bold = 0; paragraph2.Range.Font.Name = "Helvetica"; paragraph2.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify; //paragraph.Range.ParagraphFormat.LineSpacing = (float) Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpace1pt5; paragraph2.Range.InsertParagraphAfter(); //SECTION 3 Microsoft.Office.Interop.Word.Paragraph paragraph3 = doc.Content.Paragraphs.Add(System.Reflection.Missing.Value); //============================================================================================================================ foreach (KeyValuePair <string, string> entry in labelsAndTextBoxesDict) { if (content.Contains(entry.Key)) { int a; if (Int32.TryParse(entry.Value, out a)) { IntegerConverter intConverter = new IntegerConverter(); content = content.Replace("${" + entry.Key + "}", intConverter.Convert(Int64.Parse(entry.Value))); } else { content = content.Replace("${" + entry.Key + "}", entry.Value); } } } /*string modifiedContent = content.Replace("${ANTECEDENTES}", ""); * string modifiedContent2 = modifiedContent.Replace("${FOJAS}", ""); * string modifiedContent3 = modifiedContent2.Replace("${NUMERO}", ""); * string modifiedContent4 = modifiedContent3.Replace("${AÑO}", ""); * string modifiedContent5 = modifiedContent4.Replace("${CIRCUNSCRIPCION}", ""); * string modifiedContent6 = modifiedContent5.Replace("${PRECIO}", "");*/ paragraph3.Range.Text = content; paragraph3.Range.Font.Size = 12; paragraph3.Range.Font.Bold = 0; paragraph3.Range.Font.Name = "Helvetica"; paragraph3.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify; //paragraph.Range.ParagraphFormat.LineSpacing = (float)Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceDouble; paragraph3.Range.InsertParagraphAfter(); //SIGN SECTION Microsoft.Office.Interop.Word.Paragraph sign1 = doc.Content.Paragraphs.Add(System.Reflection.Missing.Value); paragraph1B.Range.Text = selectedClient.clients_name.ToUpper(); paragraph1B.Range.Font.Size = 14; paragraph1B.Range.Font.Bold = 1; paragraph1B.Range.Font.Name = "Helvetica"; paragraph1B.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; paragraph1B.Range.InsertParagraphAfter(); Microsoft.Office.Interop.Word.Paragraph rut1 = doc.Content.Paragraphs.Add(System.Reflection.Missing.Value); paragraph1B.Range.Text = selectedClient.clients_rut; paragraph1B.Range.Font.Size = 14; paragraph1B.Range.Font.Bold = 1; paragraph1B.Range.Font.Name = "Helvetica"; paragraph1B.Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft; paragraph1B.Range.InsertParagraphAfter(); }
public void TestIntegerConverter() { IntegerConverter integerConverter = new IntegerConverter(); Assert.True(integerConverter.CanConvertTo(typeof(byte))); Assert.True(integerConverter.CanConvertTo(typeof(sbyte))); Assert.True(integerConverter.CanConvertTo(typeof(short))); Assert.True(integerConverter.CanConvertTo(typeof(ushort))); Assert.True(integerConverter.CanConvertTo(typeof(int))); Assert.True(integerConverter.CanConvertTo(typeof(uint))); Assert.True(integerConverter.CanConvertTo(typeof(long))); Assert.True(integerConverter.CanConvertTo(typeof(ulong))); Assert.False(integerConverter.CanConvertTo(typeof(string))); Assert.False(integerConverter.CanConvertTo(typeof(float))); Assert.False(integerConverter.CanConvertTo(typeof(double))); Assert.False(integerConverter.CanConvertTo(typeof(decimal))); Assert.False(integerConverter.CanConvertTo(typeof(Enum))); Assert.True(integerConverter.CanConvertFrom(" 10 ")); Assert.True(integerConverter.CanConvertFrom("123")); Assert.True(integerConverter.CanConvertFrom("1,2,3,4 ")); Assert.True(integerConverter.CanConvertFrom(" 1,234,567 ")); Assert.True(integerConverter.CanConvertFrom(" 000,000")); Assert.True(integerConverter.CanConvertFrom(" 3e7 ")); Assert.True(integerConverter.CanConvertFrom("500e-2")); Assert.True(integerConverter.CanConvertFrom(" 6E4 ")); Assert.True(integerConverter.CanConvertFrom(" 2E+5")); Assert.True(integerConverter.CanConvertFrom(" 0xFF")); Assert.True(integerConverter.CanConvertFrom("0X123 ")); Assert.True(integerConverter.CanConvertFrom(" 0x1F2A ")); Assert.True(integerConverter.CanConvertFrom("-0X1")); Assert.False(integerConverter.CanConvertFrom("abc")); Assert.False(integerConverter.CanConvertFrom("")); Assert.False(integerConverter.CanConvertFrom(" ")); Assert.False(integerConverter.CanConvertFrom(".")); Assert.False(integerConverter.CanConvertFrom(" abc123")); Assert.False(integerConverter.CanConvertFrom("0x ")); Assert.False(integerConverter.CanConvertFrom(" e4 ")); Assert.False(integerConverter.CanConvertFrom("23e ")); Assert.False(integerConverter.CanConvertFrom(" 1 2 3")); Assert.False(integerConverter.CanConvertFrom(" 1E-4 ")); Assert.False(integerConverter.CanConvertFrom(" --4")); Assert.False(integerConverter.CanConvertFrom("ABCDEF")); Assert.False(integerConverter.CanConvertFrom("0x0X1")); Assert.Equal((byte)1, integerConverter.Convert(typeof(byte), "1")); Assert.Equal((sbyte)123, integerConverter.Convert(typeof(sbyte), " 123")); Assert.Equal((short)-23, integerConverter.Convert(typeof(short), "-23 ")); Assert.Equal((ushort)5000, integerConverter.Convert(typeof(ushort), " 5,0,0,0 ")); Assert.Equal(-20000, integerConverter.Convert(typeof(int), " -2e4 ")); Assert.Equal(2300000u, integerConverter.Convert(typeof(uint), "23e5")); Assert.Equal(1234567890L, integerConverter.Convert(typeof(long), "1,234,567,890 ")); Assert.Equal(123456789UL, integerConverter.Convert(typeof(ulong), " 1,2,3,4,5,6,7,8,9")); Assert.Equal(123400L, integerConverter.Convert(typeof(object), " 1,234E2 ")); Assert.Equal((byte)3, integerConverter.Convert(typeof(byte), "+0x3")); Assert.Equal((sbyte)123, integerConverter.Convert(typeof(sbyte), " 0X7B ")); Assert.Equal((short)-35, integerConverter.Convert(typeof(short), "-0x23 ")); Assert.Equal((ushort)2748, integerConverter.Convert(typeof(ushort), " +0xABC ")); Assert.Equal(-11259375, integerConverter.Convert(typeof(int), " -0xAbCdEf ")); Assert.Equal(65535u, integerConverter.Convert(typeof(uint), "0XFFFF")); Assert.Equal(-78187493520L, integerConverter.Convert(typeof(long), "-0x1234567890 ")); Assert.Equal(0UL, integerConverter.Convert(typeof(ulong), " 0x0 ")); Assert.Equal(3735928559L, integerConverter.Convert(typeof(object), " 0XDEADBEEF ")); Assert.Throws <InvalidOperationException>(() => integerConverter.Convert(typeof(byte), ",")); Assert.Throws <InvalidOperationException>(() => integerConverter.Convert(typeof(sbyte), "256")); Assert.Throws <InvalidOperationException>(() => integerConverter.Convert(typeof(short), "1234567890")); Assert.Throws <InvalidOperationException>(() => integerConverter.Convert(typeof(object), "123e-2")); Assert.Throws <InvalidOperationException>(() => integerConverter.Convert(typeof(uint), "-0xF")); }