Update 20241112
This commit is contained in:
430
DOCGEN_sik/clsDocGet.cs
Normal file
430
DOCGEN_sik/clsDocGet.cs
Normal file
@@ -0,0 +1,430 @@
|
||||
using Database;
|
||||
using Syncfusion.DocIO.DLS;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Syncfusion.DocIO;
|
||||
using Syncfusion.DocIO.DLS;
|
||||
using Syncfusion.Pdf;
|
||||
using Syncfusion.XlsIO;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
using Syncfusion.Pdf.Parsing;
|
||||
using System.Xml.Linq;
|
||||
using Model;
|
||||
using SkiaSharp;
|
||||
using System.Web;
|
||||
using System.Net;
|
||||
using System.Security.Policy;
|
||||
using DOCGEN.Klassen;
|
||||
using System.Data;
|
||||
using Helper;
|
||||
using Syncfusion.XlsIO.Parser.Biff_Records;
|
||||
|
||||
|
||||
namespace DOCGEN
|
||||
{
|
||||
public class DocGet
|
||||
{
|
||||
private string connectionstring = "";
|
||||
private string URI = "https://localhost:44334/";
|
||||
|
||||
public DocGet(string Connectionstring)
|
||||
{
|
||||
this.connectionstring = Connectionstring;
|
||||
Lic();
|
||||
}
|
||||
|
||||
public void Lic()
|
||||
{
|
||||
Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("Ngo9BigBOggjHTQxAR8/V1NCaF5cXmZCf1FpRmJGdld5fUVHYVZUTXxaS00DNHVRdkdnWXdecXRdQ2heUU13XUI=");
|
||||
|
||||
}
|
||||
public string test_filetype(string filename)
|
||||
{
|
||||
string b64 = ConvertFileToBase64(filename);
|
||||
var streamword = new MemoryStream(Convert.FromBase64String(b64));
|
||||
var streamexcel = new MemoryStream(Convert.FromBase64String(b64));
|
||||
|
||||
try
|
||||
{
|
||||
if (Is_WordDocument(streamword) == true)
|
||||
{
|
||||
return "Word";
|
||||
}
|
||||
if (Is_ExcelDocument(streamexcel) == true)
|
||||
{
|
||||
return "Excel";
|
||||
}
|
||||
if (Is_PDFDocument(b64))
|
||||
{
|
||||
return "PDF";
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "";
|
||||
}
|
||||
finally
|
||||
{
|
||||
streamword.Dispose();
|
||||
streamexcel.Dispose();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public string Get_FileType(string b64)
|
||||
{
|
||||
var streamword = new MemoryStream(Convert.FromBase64String(b64));
|
||||
var streamexcel = new MemoryStream(Convert.FromBase64String(b64));
|
||||
|
||||
try
|
||||
{
|
||||
if (Is_WordDocument(streamword) == true)
|
||||
{
|
||||
return "W";
|
||||
}
|
||||
if (Is_ExcelDocument(streamexcel) == true)
|
||||
{
|
||||
return "X";
|
||||
}
|
||||
if (Is_PDFDocument(b64))
|
||||
{
|
||||
return "P";
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "";
|
||||
}
|
||||
finally
|
||||
{
|
||||
streamword.Dispose();
|
||||
streamexcel.Dispose();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public clsdok GetDocAsFinalPDF(string DocID)
|
||||
{
|
||||
Database.DB db = new Database.DB(connectionstring);
|
||||
db.Get_Tabledata("select * from Relaunch_View_PDF_Parameter where dokumentid='" + DocID + "' order by sort", false, true);
|
||||
return GetDocAsPDF(DocID, db.dsdaten.Tables[0]);
|
||||
}
|
||||
public clsdok GetDocAsPDF(string Docid, DataTable pdfparameters = null)
|
||||
{
|
||||
clsdok dok = GetDoc(Docid);
|
||||
|
||||
try
|
||||
{
|
||||
switch (dok.doktype.ToUpper().Substring(0, 1))
|
||||
{
|
||||
case "D":
|
||||
dok.dokument = Convert_Word_To_PDF(dok.dokument.ToString(), pdfparameters);
|
||||
dok.extension = "pdf";
|
||||
return dok;
|
||||
break;
|
||||
case "X":
|
||||
|
||||
dok.dokument = Convert_Excel_To_PDF(dok.dokument.ToString());
|
||||
dok.extension = "pdf";
|
||||
return dok;
|
||||
break;
|
||||
case "P":
|
||||
return dok;
|
||||
dok.extension = "pdf";
|
||||
break;
|
||||
default:
|
||||
return dok;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return dok;
|
||||
}
|
||||
return dok;
|
||||
}
|
||||
public clsdok GetDoc(string Docid)
|
||||
{
|
||||
//if (URI !="")
|
||||
//{
|
||||
// HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URI+ "api/GetDocument?dokid=" + Docid);
|
||||
// request.Method = "GET";
|
||||
// WebResponse response = (WebResponse)request.GetResponse();
|
||||
|
||||
//}
|
||||
DB db = new DB(connectionstring);
|
||||
clsdok dok = new clsdok("", "", "");
|
||||
dok = db.GetDocumentAsBase64(Docid);
|
||||
|
||||
if (dok.doktype == "")
|
||||
{
|
||||
dok.doktype = Get_FileType(dok.dokument);
|
||||
}
|
||||
|
||||
return dok;
|
||||
|
||||
}
|
||||
|
||||
public string ConvertFileToBase64(string fileName)
|
||||
{
|
||||
string ReturnValue = "";
|
||||
|
||||
|
||||
using (FileStream BinaryFile = new FileStream(fileName, FileMode.Open))
|
||||
{
|
||||
BinaryReader BinRead = new BinaryReader(BinaryFile);
|
||||
byte[] BinBytes = BinRead.ReadBytes(System.Convert.ToInt32(BinaryFile.Length));
|
||||
ReturnValue = Convert.ToBase64String(BinBytes);
|
||||
BinaryFile.Close();
|
||||
}
|
||||
|
||||
return ReturnValue;
|
||||
}
|
||||
|
||||
private bool Is_WordDocument(MemoryStream stream)
|
||||
{
|
||||
Syncfusion.DocIO.DLS.WordDocument WordDocument = new Syncfusion.DocIO.DLS.WordDocument();
|
||||
try
|
||||
{
|
||||
WordDocument.Open(stream, FormatType.Automatic);
|
||||
string type = "";
|
||||
type = WordDocument.ActualFormatType.ToString();
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
WordDocument.Dispose();
|
||||
WordDocument = null;
|
||||
}
|
||||
}
|
||||
|
||||
private bool Is_ExcelDocument(MemoryStream stream)
|
||||
{
|
||||
ExcelEngine excelEngine = new ExcelEngine();
|
||||
IApplication application = excelEngine.Excel;
|
||||
//IWorkbook workbook = application.Workbooks.Open(stream);
|
||||
IWorkbook workbook;
|
||||
try
|
||||
{
|
||||
string type = "";
|
||||
workbook = application.Workbooks.Open(stream);
|
||||
type = workbook.Version.ToString();
|
||||
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
excelEngine.Dispose();
|
||||
workbook = null;
|
||||
application = null;
|
||||
excelEngine = null;
|
||||
}
|
||||
}
|
||||
|
||||
private bool Is_PDFDocument(string b64)
|
||||
{
|
||||
PdfLoadedDocument LoadedDocument;
|
||||
try
|
||||
{
|
||||
LoadedDocument = new PdfLoadedDocument(Helper.EncodeExtensions.DecodeBase642ByteArray(b64));
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
LoadedDocument = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public string Convert_Word_To_PDF(string document, DataTable pdfparameters = null)
|
||||
{
|
||||
|
||||
//Dokument erstellen
|
||||
var streamword = new MemoryStream(Convert.FromBase64String(document));
|
||||
WordDocument wordDocument = new WordDocument(streamword, Syncfusion.DocIO.FormatType.Automatic);
|
||||
|
||||
// Keine Parameter / Dokument als PDF konvertieren
|
||||
if (pdfparameters == null)
|
||||
{
|
||||
using (Syncfusion.DocToPDFConverter.DocToPDFConverter converter = new Syncfusion.DocToPDFConverter.DocToPDFConverter())
|
||||
{
|
||||
converter.Settings.EmbedFonts = true;
|
||||
converter.Settings.UpdateDocumentFields = true;
|
||||
converter.Settings.EnableAlternateChunks = true;
|
||||
converter.Settings.AutoDetectComplexScript = true;
|
||||
|
||||
//Converts Word document into PDF document
|
||||
using (PdfDocument pdfDocument = converter.ConvertToPDF(wordDocument))
|
||||
{
|
||||
MemoryStream outputStream = new MemoryStream();
|
||||
pdfDocument.Save(outputStream);
|
||||
byte[] bytes;
|
||||
bytes = outputStream.ToArray();
|
||||
return Convert.ToBase64String(bytes);
|
||||
}
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
WordDocument[] words = new WordDocument[2];
|
||||
WordDocument[] finalwords = new WordDocument[3];
|
||||
bool splitted = false;
|
||||
SyncFWord sfword = new SyncFWord();
|
||||
// Texte ersetzen
|
||||
foreach (DataRow r in pdfparameters.Rows)
|
||||
{
|
||||
// Text ersetzen im ganzen Dokument
|
||||
if (r["Funktion"].ToString() == "1")
|
||||
{
|
||||
switch (r["OfficeElementType"].ToString())
|
||||
{
|
||||
case "2":
|
||||
sfword.Replace_Fieldtext(ref wordDocument, r["OfficeElement"].ToString(), r["OfficeElementInhaltNeu"].ToString());
|
||||
break;
|
||||
case "3":
|
||||
//Bookmarkcontent
|
||||
break;
|
||||
case "4":
|
||||
sfword.Replace_Text(ref wordDocument, r["OfficeElementInhaltAlt"].ToString(), r["OfficeElementInhaltNeu"].ToString());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
//Split
|
||||
foreach (DataRow r in pdfparameters.Rows)
|
||||
{
|
||||
if (r["Funktion"].ToString() == "2")
|
||||
{
|
||||
words[0] = new WordDocument();
|
||||
words[1] = new WordDocument();
|
||||
|
||||
sfword.SplitDocument(ref wordDocument,ref words[0], Convert.ToInt32(r["OfficeElementInhaltneu"]));
|
||||
sfword.SplitDocument_Part2(ref wordDocument, ref words[1], Convert.ToInt32(r["OfficeElementInhaltneu"]));
|
||||
splitted= true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (splitted==false)
|
||||
{
|
||||
finalwords[0] = words[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
finalwords[0]=words[0];
|
||||
//Dokument kopieren
|
||||
int copynr = 0;
|
||||
foreach (DataRow r in pdfparameters.Rows)
|
||||
{
|
||||
if (Convert.ToInt32(r["Kopienummer"]) > copynr)
|
||||
{
|
||||
copynr = Convert.ToInt32(r["Kopienummer"]);
|
||||
finalwords[copynr] = new WordDocument();
|
||||
finalwords[copynr]= words[Convert.ToInt32(r["dokumentteil"]) - 1];
|
||||
|
||||
}
|
||||
}
|
||||
foreach(DataRow r in pdfparameters.Rows)
|
||||
{
|
||||
if (r["Funktion"].ToString() == "3")
|
||||
{
|
||||
switch (r["OfficeElementType"].ToString())
|
||||
{
|
||||
case "2":
|
||||
wordDocument = finalwords[Convert.ToInt32(r["Kopienummer"])];
|
||||
sfword.Replace_Fieldtext(ref wordDocument, r["OfficeElement"].ToString(), r["OfficeElementInhaltNeu"].ToString());
|
||||
wordDocument.UpdateDocumentFields();
|
||||
finalwords[Convert.ToInt32(r["Kopienummer"])]=wordDocument;
|
||||
|
||||
break;
|
||||
case "3":
|
||||
//Bookmarkcontent
|
||||
break;
|
||||
case "4":
|
||||
wordDocument = finalwords[Convert.ToInt32(r["Kopienummer"])];
|
||||
sfword.Replace_Text(ref wordDocument, r["OfficeElementInhaltAlt"].ToString(), r["OfficeElementInhaltNeu"].ToString());
|
||||
wordDocument.UpdateDocumentFields();
|
||||
finalwords[Convert.ToInt32(r["Kopienummer"])] = wordDocument;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
PdfDocument finaldoc = new PdfDocument();
|
||||
PdfDocument[] pdfs = new PdfDocument[finalwords.Length];
|
||||
for (int i = 0; i< finalwords.Count(); i++)
|
||||
{
|
||||
using (Syncfusion.DocToPDFConverter.DocToPDFConverter converter = new Syncfusion.DocToPDFConverter.DocToPDFConverter())
|
||||
{
|
||||
wordDocument = finalwords[i];
|
||||
converter.Settings.EmbedFonts = true;
|
||||
pdfs[i] = new PdfDocument();
|
||||
pdfs[i] = converter.ConvertToPDF(finalwords[i]);
|
||||
};
|
||||
}
|
||||
|
||||
MemoryStream[] streams = new MemoryStream[pdfs.Count()];
|
||||
for (int i = 0; i< pdfs.Count(); i++)
|
||||
{
|
||||
streams[i] = new MemoryStream();
|
||||
pdfs[i].Save(streams[i]);
|
||||
}
|
||||
PdfDocumentBase.Merge(finaldoc,streams);
|
||||
MemoryStream outputStream = new MemoryStream();
|
||||
finaldoc.Save(outputStream);
|
||||
finaldoc.Save(@"h:\tssettings\edoka\test.pdf");
|
||||
byte[] bytes;
|
||||
bytes = outputStream.ToArray();
|
||||
return Convert.ToBase64String(bytes);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
//Exemplare
|
||||
//foreach (DataRow r in pdfparameters.Rows)
|
||||
//{
|
||||
// if (r["Funktion"].ToString() == "3")
|
||||
// {
|
||||
// words.Add(sfword.SplitDocument(wordDocument, Convert.ToInt32(r["OfficeElementInhaltneu"])));
|
||||
// words.Add(sfword.SplitDocument_Part2(wordDocument, Convert.ToInt32(r["OfficeElementInhaltneu"])));
|
||||
// }
|
||||
//}
|
||||
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
public string Convert_Excel_To_PDF(string document)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
public void Print_Doc(string document)
|
||||
{
|
||||
SyncFWord sfword = new SyncFWord();
|
||||
sfword.Print_Word(document);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user