309 lines
14 KiB
C#
309 lines
14 KiB
C#
using Syncfusion.DocIO.DLS;
|
||
using Syncfusion.DocIO;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using Syncfusion.Calculate;
|
||
using Helper;
|
||
using System.Data;
|
||
using Model;
|
||
using System.Linq.Expressions;
|
||
using NLog;
|
||
using Syncfusion.XlsIO.Implementation.XmlSerialization;
|
||
using System.Security.Authentication.ExtendedProtection;
|
||
using System.Drawing;
|
||
using Syncfusion.XPS;
|
||
using System.Xml.Linq;
|
||
using System.Text.RegularExpressions;
|
||
|
||
namespace DOCGEN.Klassen
|
||
{
|
||
public class SynFWord
|
||
{
|
||
public string Generate_Word(string base64, clsDocData docdata)
|
||
{
|
||
|
||
string formattype = "";
|
||
MemoryStream ms = new MemoryStream(Helper.EncodeExtensions.DecodeBase642ByteArray(base64));
|
||
WordDocument document = new WordDocument(ms, FormatType.Doc);
|
||
formattype = document.ActualFormatType.ToString();
|
||
var formattype_original = document.ActualFormatType;
|
||
|
||
foreach (clsDocValue dv in docdata.DocValues)
|
||
{
|
||
if (dv.TMBeginn.ToString() != "" && dv.TMEnd.ToString() == "")
|
||
{
|
||
try
|
||
{
|
||
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
|
||
if ((dv.TMBeginn.ToString() == "TGEDKVornameNameLinksB99" || dv.TMBeginn.ToString() == "TGEDKVornameNameRechtsB99") && docdata.As_Faksimile == "True")
|
||
{
|
||
bookmarkNavigator.MoveToBookmark(dv.TMBeginn.ToString());
|
||
IWParagraph paragraph = new WParagraph(document);
|
||
paragraph.AppendBreak(BreakType.LineBreak);
|
||
System.Drawing.Image image = System.Drawing.Image.FromFile(@"x:\docdemo\unterschriften\kube1.png");
|
||
paragraph.AppendPicture(image);
|
||
paragraph.AppendBreak(BreakType.LineBreak);
|
||
paragraph.AppendText(dv.Value.ToString());
|
||
bookmarkNavigator.InsertParagraph(paragraph);
|
||
|
||
|
||
}
|
||
else
|
||
{
|
||
bookmarkNavigator.MoveToBookmark(dv.TMBeginn.ToString());
|
||
bookmarkNavigator.InsertText(dv.Value.ToString());
|
||
}
|
||
}
|
||
catch { }
|
||
}
|
||
if (dv.TMBeginn.ToString() != "" && dv.TMEnd.ToString() != "")
|
||
{
|
||
try
|
||
{
|
||
ReplaceBookmarkContent(document, dv.TMBeginn.ToString(), dv.TMEnd.ToString(), dv.Value.ToString());
|
||
}
|
||
catch { }
|
||
}
|
||
|
||
if (dv.FieldName.ToString() != "")
|
||
{
|
||
try
|
||
{
|
||
foreach (WSection section in document.Sections)
|
||
//Iterates through section child elements
|
||
foreach (WTextBody textBody in section.ChildEntities)
|
||
{
|
||
//Iterates through form fields
|
||
foreach (WFormField formField in textBody.FormFields)
|
||
{
|
||
if (formField.Name == dv.FieldName.ToString())
|
||
{
|
||
formField.Text = dv.Value.ToString();
|
||
break;
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
catch { }
|
||
}
|
||
|
||
}
|
||
if (docdata.As_Faksimile == "True")
|
||
{
|
||
|
||
TextSelection[] textSelections = document.FindAll("Thurgauer Kantonalbank", false, true);
|
||
foreach (TextSelection selection in textSelections)
|
||
{
|
||
WTextRange textRange = selection.GetAsOneRange();
|
||
WTextBody ownerTextBody = textRange.OwnerParagraph.OwnerTextBody;
|
||
//Get the index of the paragraph with specified text
|
||
int ownerParaIndex = ownerTextBody.ChildEntities.IndexOf(textRange.OwnerParagraph);
|
||
//Check whether the previous and next paragraphs are empty and remove them
|
||
if (ownerParaIndex > 0 && ownerTextBody.ChildEntities[ownerParaIndex - 1] is WParagraph &&
|
||
(ownerTextBody.ChildEntities[ownerParaIndex - 1] as WParagraph).Items.Count == 0)
|
||
{
|
||
ownerTextBody.ChildEntities.RemoveAt(ownerParaIndex - 1);
|
||
}
|
||
if (ownerParaIndex + 1 < ownerTextBody.ChildEntities.Count &&
|
||
ownerTextBody.ChildEntities[ownerParaIndex + 1] is WParagraph &&
|
||
(ownerTextBody.ChildEntities[ownerParaIndex + 1] as WParagraph).Items.Count == 0)
|
||
{
|
||
for (int i = 0; i < 4; i++) {
|
||
try
|
||
{
|
||
ownerTextBody.ChildEntities.RemoveAt(ownerParaIndex + 1);
|
||
|
||
}
|
||
catch { }
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
document.Save(@"x:\docdemo\unterschriften\gaga.docx");
|
||
document.Save(ms, formattype_original);
|
||
document.Close();
|
||
byte[] imageArray = ms.ToArray();
|
||
return Convert.ToBase64String(imageArray);
|
||
}
|
||
|
||
|
||
|
||
public static void ReplaceBookmarkText(WordDocument document, string bookmarkName, string textToFind, string textToReplace)
|
||
{
|
||
//Check whether the bookmark name is valid.
|
||
if (string.IsNullOrEmpty(bookmarkName) || document.Bookmarks.FindByName(bookmarkName) == null)
|
||
return;
|
||
//Move to the virtual cursor before the bookmark end location of the bookmark.
|
||
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
|
||
bookmarksNavigator.MoveToBookmark(bookmarkName);
|
||
//Replace the bookmark content with new text.
|
||
TextBodyPart textBodyPart = bookmarksNavigator.GetBookmarkContent();
|
||
//Get paragraph from the textBody part.
|
||
foreach (TextBodyItem item in textBodyPart.BodyItems)
|
||
{
|
||
IterateTextBody(item, textToFind, textToReplace);
|
||
}
|
||
//Replace the bookmark content with text body part.
|
||
bookmarksNavigator.ReplaceBookmarkContent(textBodyPart);
|
||
}
|
||
public static void IterateTextBody(TextBodyItem item, string textToFind, string textToReplace)
|
||
{
|
||
switch (item.EntityType)
|
||
{
|
||
case EntityType.Paragraph:
|
||
WParagraph paragraph = (WParagraph)item;
|
||
//Replace a text in the bookmark content.
|
||
paragraph.Replace(new System.Text.RegularExpressions.Regex(textToFind), textToReplace);
|
||
break;
|
||
case EntityType.Table:
|
||
WTable table = (WTable)item;
|
||
foreach (WTableRow row in table.Rows)
|
||
{
|
||
foreach (WTableCell cell in row.Cells)
|
||
{
|
||
foreach (TextBodyItem bodyItem in cell.ChildEntities)
|
||
{
|
||
IterateTextBody(bodyItem, textToFind, textToReplace);
|
||
}
|
||
}
|
||
}
|
||
break;
|
||
case EntityType.BlockContentControl:
|
||
WTextBody body = (item as IBlockContentControl).TextBody;
|
||
foreach (TextBodyItem bodyitem in body.ChildEntities)
|
||
IterateTextBody(bodyitem, textToFind, textToReplace);
|
||
break;
|
||
}
|
||
}
|
||
|
||
public void ReplaceBookmarkContent(WordDocument document, String bookmark1, String bookmark2, String replacementContent)
|
||
{
|
||
//Temp Bookmark.
|
||
String tempBookmarkName = "tempBookmark";
|
||
|
||
#region Insert bookmark start after bookmark1.
|
||
//Get the bookmark instance by using FindByName method of BookmarkCollection with bookmark name.
|
||
Bookmark firstBookmark = document.Bookmarks.FindByName(bookmark1);
|
||
//Access the bookmark end’s owner paragraph by using bookmark.
|
||
WParagraph firstBookmarkOwnerPara = firstBookmark.BookmarkEnd.OwnerParagraph;
|
||
//Get the index of bookmark end of bookmark1.
|
||
int index = firstBookmarkOwnerPara.Items.IndexOf(firstBookmark.BookmarkEnd);
|
||
//Create and add new bookmark start after bookmark1.
|
||
BookmarkStart newBookmarkStart = new BookmarkStart(document, tempBookmarkName);
|
||
firstBookmarkOwnerPara.ChildEntities.Insert(index + 1, newBookmarkStart);
|
||
#endregion
|
||
|
||
#region Insert bookmark end before bookmark2.
|
||
//Get the bookmark instance by using FindByName method of BookmarkCollection with bookmark name.
|
||
Bookmark secondBookmark = document.Bookmarks.FindByName(bookmark2);
|
||
//Access the bookmark start’s owner paragraph by using bookmark.
|
||
WParagraph secondBookmarkOwnerPara = secondBookmark.BookmarkStart.OwnerParagraph;
|
||
//Get the index of bookmark start of bookmark2.
|
||
index = secondBookmarkOwnerPara.Items.IndexOf(secondBookmark.BookmarkStart);
|
||
//Create and add new bookmark end before bookmark2.
|
||
BookmarkEnd newBookmarkEnd = new BookmarkEnd(document, tempBookmarkName);
|
||
secondBookmarkOwnerPara.ChildEntities.Insert(index, newBookmarkEnd);
|
||
#endregion
|
||
|
||
#region Select bookmark content and replace.
|
||
//Create the bookmark navigator instance to access the newly created bookmark.
|
||
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
|
||
//Move the virtual cursor to the location of the temp bookmark.
|
||
bookmarkNavigator.MoveToBookmark(tempBookmarkName);
|
||
//Replace the bookmark content.
|
||
bookmarkNavigator.ReplaceBookmarkContent(replacementContent, true);
|
||
#endregion
|
||
|
||
#region Remove that temporary bookmark.
|
||
//Get the bookmark instance by using FindByName method of BookmarkCollection with bookmark name.
|
||
Bookmark bookmark = document.Bookmarks.FindByName(tempBookmarkName);
|
||
//Remove the temp bookmark named from Word document.
|
||
document.Bookmarks.Remove(bookmark);
|
||
#endregion
|
||
}
|
||
private void RemoveEmptyParagraph(IWSection curSection)
|
||
{
|
||
int j = 0;
|
||
int paraCount = 0;
|
||
while (j < curSection.Body.ChildEntities.Count)
|
||
{
|
||
//Read the section text body
|
||
WTextBody textBody = curSection.Body;
|
||
|
||
//Read all the body items collection
|
||
BodyItemCollection textbodycollection = textBody.ChildEntities as BodyItemCollection;
|
||
if (textbodycollection[j].EntityType == EntityType.Paragraph)
|
||
{
|
||
IWParagraph paragraph = textbodycollection[j] as IWParagraph;
|
||
if (paragraph.Items.Count == 0)
|
||
{
|
||
//Remove the empty paragraph to the Section
|
||
curSection.Paragraphs.RemoveAt(paraCount);
|
||
}
|
||
else
|
||
{
|
||
paraCount++;
|
||
j++;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
j++;
|
||
}
|
||
}
|
||
|
||
}
|
||
private static Entity GetOwnerEntity(BookmarkStart bookmarkStart)
|
||
{
|
||
Entity baseEntity = bookmarkStart.Owner;
|
||
|
||
while (!(baseEntity is WSection))
|
||
{
|
||
if (baseEntity is null)
|
||
return baseEntity;
|
||
baseEntity = baseEntity.Owner;
|
||
}
|
||
return baseEntity;
|
||
}
|
||
public Image resizeImage(Image imgToResize, Size size)
|
||
{
|
||
return (Image)(new Bitmap(imgToResize, size));
|
||
}
|
||
private static void FindAndReplaceInTable(WTable table, TextSelection textSelection)
|
||
{
|
||
//Iterate through the rows of table.
|
||
foreach (WTableRow row in table.Rows)
|
||
{
|
||
//Iterate through the cells of rows.
|
||
foreach (WTableCell cell in row.Cells)
|
||
{
|
||
//Iterates through the items in cell.
|
||
foreach (Entity entity in cell.ChildEntities)
|
||
{
|
||
if (entity.EntityType == EntityType.Paragraph)
|
||
{
|
||
WParagraph paragraph = entity as WParagraph;
|
||
//Replace the specified regular expression with a TextSelection in the paragraph.
|
||
paragraph.Replace(new Regex("<<(.*)>>"), textSelection);
|
||
}
|
||
else if (entity.EntityType == EntityType.Table)
|
||
{
|
||
FindAndReplaceInTable(entity as WTable, textSelection);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
|