Initial Comit
This commit is contained in:
95
DOCGEN/Klassen/DocGenerator_from_EDOKA.cs
Normal file
95
DOCGEN/Klassen/DocGenerator_from_EDOKA.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using edoka_dms;
|
||||
using DOCGEN.Klassen;
|
||||
using EDOKA_Database;
|
||||
using System.Data;
|
||||
using Model;
|
||||
using System.Diagnostics;
|
||||
using NLog;
|
||||
|
||||
namespace DOCGEN.Generator
|
||||
{
|
||||
|
||||
public class DocGenerator_from_EDOKA
|
||||
{
|
||||
public string connectionstring;
|
||||
public string DokumentID;
|
||||
public string tempdir;
|
||||
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
|
||||
public DocGenerator_from_EDOKA(string connectionstring)
|
||||
{
|
||||
this.connectionstring = connectionstring;
|
||||
}
|
||||
|
||||
public DocGenerator_from_EDOKA(string connectionstring, string tempdir)
|
||||
{
|
||||
this.connectionstring = connectionstring;
|
||||
this.tempdir = tempdir;
|
||||
}
|
||||
|
||||
public string Generate_Doc_EDOKA(string dokumentid)
|
||||
{
|
||||
EDOKA_DB db = new EDOKA_DB(connectionstring);
|
||||
DataTable dt = new DataTable();
|
||||
string Apptype = "";
|
||||
dt = db.Get_Vorlage_By_DokumentID(DokumentID);
|
||||
db.dsdaten.Tables.Clear();
|
||||
Apptype = db.Get_ApplicationType_from_Vorlage(Convert.ToInt32(dt.Rows[0]["nr"].ToString()));
|
||||
switch(Apptype.ToUpper())
|
||||
{
|
||||
case "DOCX": case "DOCM": case "DOC": case "DOT": case "Word":
|
||||
SynFWord sfword = new SynFWord();
|
||||
clsDocData docdata = new clsDocData();
|
||||
docdata.PartnerNr = "1234";
|
||||
generate_docdata(dokumentid, ref docdata);
|
||||
string doc = sfword.Generate_Word(dt.Rows[0][0].ToString(),docdata);
|
||||
return doc;
|
||||
//System.IO.File.WriteAllBytes(tempdir + @"\" + dokumentid + ".docx", Convert.FromBase64String(doc));
|
||||
//Process.Start(tempdir + @"\" + dokumentid + ".docx");
|
||||
//break;
|
||||
case "XLSM": case "XLSX": case "XLST": case "XLS": case "XLT":
|
||||
return "";
|
||||
//break;
|
||||
case "PDF": case "ACROBAT": case "FORMULAR": case "DOKUMENT":
|
||||
string pdf = dt.Rows[0][0].ToString();
|
||||
return pdf;
|
||||
//break ;
|
||||
default:
|
||||
Logger.Info("DocGenerator_from_EDOKA: FileType unknown"+ Apptype);
|
||||
return "";
|
||||
//break;
|
||||
}
|
||||
|
||||
//db.get_vorlage_to_file(db.dsdaten.Tables[0].Rows[0][1].ToString(),tempdir,dokumentid );
|
||||
}
|
||||
|
||||
public void generate_docdata(string dokumentid, ref clsDocData docdata)
|
||||
{
|
||||
List<clsDocValue> docValues = new List<clsDocValue>();
|
||||
EDOKA_Database.EDOKA_DB db = new EDOKA_Database.EDOKA_DB(connectionstring);
|
||||
db.Get_Tabledata("exec sp_dokumentwerte '" + dokumentid + "',0", false, true);
|
||||
foreach (DataRow r in db.dsdaten.Tables[0].Rows)
|
||||
{
|
||||
clsDocValue dv = new clsDocValue();
|
||||
dv.TMBeginn = r["beginntextmarke"].ToString();
|
||||
dv.TMEnd = r["endetextmarke"].ToString();
|
||||
dv.FieldName = r["feldname"].ToString();
|
||||
dv.Value = r["xvalue"].ToString();
|
||||
docValues.Add(dv);
|
||||
}
|
||||
clsDocValue dv1 = new clsDocValue();
|
||||
dv1.TMBeginn = "";
|
||||
dv1.TMEnd = "";
|
||||
dv1.FieldName = "Text1";
|
||||
dv1.Value = "Das ist der Text, welcher automatisch in die Briefvorlage kopiert wird. " + "\r\n"+
|
||||
"Mit einem Zeilenumbruch";
|
||||
docValues.Add(dv1);
|
||||
docdata.As_Faksimile = "True";
|
||||
docdata.DocValues = docValues;
|
||||
}
|
||||
}
|
||||
}
|
||||
308
DOCGEN/Klassen/SynFWord.cs
Normal file
308
DOCGEN/Klassen/SynFWord.cs
Normal file
@@ -0,0 +1,308 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
13
DOCGEN/Klassen/SyncFPDF.cs
Normal file
13
DOCGEN/Klassen/SyncFPDF.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DOCGEN.Klassen
|
||||
{
|
||||
public class SyncFPDF
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
12
DOCGEN/Klassen/SyndFExcel.cs
Normal file
12
DOCGEN/Klassen/SyndFExcel.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DOCGEN.Klassen
|
||||
{
|
||||
internal class SyndFExcel
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user