Beispiel #1
0
        internal static Paragraph GetPreOrPostParagraphToInsert(string text, SimpleField fieldToMimic)
        {
            Run       runElementForText = DocumentMerge.GetRunElementForText(text, fieldToMimic);
            Paragraph paragraph         = new Paragraph();

            paragraph.Append(new OpenXmlElement[1]
            {
                (OpenXmlElement)runElementForText
            });
            return(paragraph);
        }
Beispiel #2
0
        internal static void FillWordFieldsInElement(WordprocessingDocument docx, Dictionary <string, string> values, OpenXmlElement element)
        {
            Dictionary <SimpleField, string[]> dictionary = new Dictionary <SimpleField, string[]>();

            foreach (SimpleField index in element.Descendants <SimpleField>())
            {
                string[] switches;
                string[] options;
                string   fieldNameWithOptions = DocumentMerge.GetFieldNameWithOptions(index, out switches, out options);
                if (!string.IsNullOrEmpty(fieldNameWithOptions) && values.ContainsKey(fieldNameWithOptions))
                {
                    if (values.ContainsKey(fieldNameWithOptions) && !string.IsNullOrEmpty(values[fieldNameWithOptions]))
                    {
                        string[] strArray = DocumentMerge.ApplyFormatting(options[0], ((object)values[fieldNameWithOptions]).ToString(), options[1], options[2]);
                        if (!string.IsNullOrEmpty(options[1]))
                        {
                            index.Parent.InsertBeforeSelf <Paragraph>(DocumentMerge.GetPreOrPostParagraphToInsert(strArray[1], index));
                        }
                        if (!string.IsNullOrEmpty(options[2]))
                        {
                            index.Parent.InsertAfterSelf <Paragraph>(DocumentMerge.GetPreOrPostParagraphToInsert(strArray[2], index));
                        }
                        index.Parent.ReplaceChild <SimpleField>((OpenXmlElement)DocumentMerge.GetRunElementForText(strArray[0], index), index);
                    }
                    else
                    {
                        dictionary[index] = switches;
                    }
                }
            }
            foreach (KeyValuePair <SimpleField, string[]> keyValuePair in dictionary)
            {
                DocumentMerge.ExecuteSwitches((OpenXmlElement)keyValuePair.Key, keyValuePair.Value);
                keyValuePair.Key.Remove();
            }
        }
Beispiel #3
0
 public static byte[] WordDokumanOlustur(string sablon, DataSet dataset, Dictionary <string, string> degerler)
 {
     byte[]   buffer   = File.ReadAllBytes(sablon);
     string[] switches = (string[])null;
     using (MemoryStream memoryStream = new MemoryStream())
     {
         memoryStream.Write(buffer, 0, buffer.Length);
         using (WordprocessingDocument docx = WordprocessingDocument.Open((Stream)memoryStream, true))
         {
             DocumentMerge.ConvertFieldCodes((OpenXmlElement)docx.MainDocumentPart.Document);
             foreach (SimpleField field in docx.MainDocumentPart.Document.Descendants <SimpleField>())
             {
                 string fieldName = DocumentMerge.GetFieldName(field, out switches);
                 if (!string.IsNullOrEmpty(fieldName) && fieldName.StartsWith("TBL_"))
                 {
                     TableRow firstParent1 = DocumentMerge.GetFirstParent <TableRow>((OpenXmlElement)field);
                     if (firstParent1 != null)
                     {
                         Table firstParent2 = DocumentMerge.GetFirstParent <Table>((OpenXmlElement)firstParent1);
                         if (firstParent2 != null)
                         {
                             string nameFromFieldName = DocumentMerge.GetTableNameFromFieldName(fieldName);
                             if (dataset != null && dataset.Tables.Contains(nameFromFieldName) && dataset.Tables[nameFromFieldName].Rows.Count != 0)
                             {
                                 DataTable dataTable = dataset.Tables[nameFromFieldName];
                                 List <TableCellProperties> list1 = new List <TableCellProperties>();
                                 List <string>      list2         = new List <string>();
                                 List <string>      list3         = new List <string>();
                                 List <SimpleField> list4         = new List <SimpleField>();
                                 foreach (TableCell tableCell in firstParent1.Descendants <TableCell>())
                                 {
                                     list1.Add(tableCell.GetFirstChild <TableCellProperties>());
                                     Paragraph firstChild1 = tableCell.GetFirstChild <Paragraph>();
                                     if (firstChild1 != null)
                                     {
                                         ParagraphProperties firstChild2 = firstChild1.GetFirstChild <ParagraphProperties>();
                                         if (firstChild2 != null)
                                         {
                                             list3.Add(firstChild2.OuterXml);
                                         }
                                         else
                                         {
                                             list3.Add((string)null);
                                         }
                                     }
                                     else
                                     {
                                         list3.Add((string)null);
                                     }
                                     string      str         = string.Empty;
                                     SimpleField simpleField = (SimpleField)null;
                                     using (IEnumerator <SimpleField> enumerator = tableCell.Descendants <SimpleField>().GetEnumerator())
                                     {
                                         if (enumerator.MoveNext())
                                         {
                                             SimpleField current = enumerator.Current;
                                             simpleField = current;
                                             str         = DocumentMerge.GetColumnNameFromFieldName(DocumentMerge.GetFieldName(current, out switches));
                                         }
                                     }
                                     list2.Add(str);
                                     if (str != "")
                                     {
                                         list4.Add(simpleField);
                                     }
                                 }
                                 TableRowProperties firstChild = firstParent1.GetFirstChild <TableRowProperties>();
                                 foreach (DataRow dataRow in (InternalDataCollectionBase)dataTable.Rows)
                                 {
                                     TableRow tableRow = new TableRow();
                                     if (firstChild != null)
                                     {
                                         tableRow.Append(new OpenXmlElement[1]
                                         {
                                             (OpenXmlElement) new TableRowProperties(firstChild.OuterXml)
                                         });
                                     }
                                     for (int index = 0; index < list1.Count; ++index)
                                     {
                                         TableCellProperties tableCellProperties = new TableCellProperties(list1[index].OuterXml);
                                         TableCell           tableCell           = new TableCell();
                                         tableCell.Append(new OpenXmlElement[1]
                                         {
                                             (OpenXmlElement)tableCellProperties
                                         });
                                         Paragraph paragraph = new Paragraph(new OpenXmlElement[1]
                                         {
                                             (OpenXmlElement) new ParagraphProperties(list3[index])
                                         });
                                         tableCell.Append(new OpenXmlElement[1]
                                         {
                                             (OpenXmlElement)paragraph
                                         });
                                         try
                                         {
                                             if (!string.IsNullOrEmpty(list2[index]))
                                             {
                                                 if (!dataTable.Columns.Contains(list2[index]))
                                                 {
                                                     throw new Exception(string.Format("Unable to complete template: column name '{0}' is unknown in parameter tables !", (object)list2[index]));
                                                 }
                                                 if (!dataRow.IsNull(list2[index]))
                                                 {
                                                     string text = dataRow[list2[index]].ToString();
                                                     paragraph.Append(new OpenXmlElement[1]
                                                     {
                                                         (OpenXmlElement)DocumentMerge.GetRunElementForText(text, list4[index])
                                                     });
                                                 }
                                             }
                                         }
                                         catch
                                         {
                                         }
                                         tableRow.Append(new OpenXmlElement[1]
                                         {
                                             (OpenXmlElement)tableCell
                                         });
                                     }
                                     firstParent2.Append(new OpenXmlElement[1]
                                     {
                                         (OpenXmlElement)tableRow
                                     });
                                 }
                                 firstParent1.Remove();
                             }
                         }
                     }
                 }
             }
             foreach (SimpleField field in docx.MainDocumentPart.Document.Descendants <SimpleField>())
             {
                 string fieldName = DocumentMerge.GetFieldName(field, out switches);
                 if (!string.IsNullOrEmpty(fieldName) && fieldName.StartsWith("TBL_"))
                 {
                     TableRow firstParent1 = DocumentMerge.GetFirstParent <TableRow>((OpenXmlElement)field);
                     if (firstParent1 != null)
                     {
                         Table firstParent2 = DocumentMerge.GetFirstParent <Table>((OpenXmlElement)firstParent1);
                         if (firstParent2 != null)
                         {
                             string nameFromFieldName = DocumentMerge.GetTableNameFromFieldName(fieldName);
                             if ((dataset == null || !dataset.Tables.Contains(nameFromFieldName) || dataset.Tables[nameFromFieldName].Rows.Count == 0) && Enumerable.Contains <string>((IEnumerable <string>)switches, "dt"))
                             {
                                 firstParent2.Remove();
                             }
                         }
                     }
                 }
             }
             DocumentMerge.FillWordFieldsInElement(docx, degerler, (OpenXmlElement)docx.MainDocumentPart.Document);
             ((OpenXmlPartRootElement)docx.MainDocumentPart.Document).Save();
             foreach (HeaderPart headerPart in docx.MainDocumentPart.HeaderParts)
             {
                 DocumentMerge.ConvertFieldCodes((OpenXmlElement)headerPart.Header);
                 DocumentMerge.FillWordFieldsInElement(docx, degerler, (OpenXmlElement)headerPart.Header);
                 ((OpenXmlPartRootElement)headerPart.Header).Save();
             }
             foreach (FooterPart footerPart in docx.MainDocumentPart.FooterParts)
             {
                 DocumentMerge.ConvertFieldCodes((OpenXmlElement)footerPart.Footer);
                 DocumentMerge.FillWordFieldsInElement(docx, degerler, (OpenXmlElement)footerPart.Footer);
                 ((OpenXmlPartRootElement)footerPart.Footer).Save();
             }
         }
         memoryStream.Seek(0L, SeekOrigin.Begin);
         return(memoryStream.ToArray());
     }
 }