You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
362 lines
11 KiB
362 lines
11 KiB
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 Helper;
|
|
|
|
using Syncfusion.Pdf;
|
|
using Syncfusion.XlsIO;
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
using Syncfusion.Pdf.Parsing;
|
|
using System.Xml.Linq;
|
|
using System.Web;
|
|
using System.Net;
|
|
using System.Security.Policy;
|
|
using System.Data;
|
|
using Syncfusion.XlsIO.Parser.Biff_Records;
|
|
|
|
using Syncfusion.ExcelToPdfConverter;
|
|
using Model;
|
|
|
|
|
|
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("MzYzODg2NkAzMjM4MmUzMDJlMzBTOWljRmxNelA1d1VGOHpGR0lxQzB6UTAwKzIxK2VBNEhBZFp5alcxb1NVPQ==");
|
|
|
|
}
|
|
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_word_vorlage(int vorlagenr)
|
|
{
|
|
DB db = new DB(connectionstring);
|
|
string sql = "Select dbo.BinaryToBase64(vorlage) as Data from office_vorlage_datei where office_vorlage_dateinr=" + vorlagenr;
|
|
db.Get_Tabledata(sql, false, true);
|
|
string base64 = db.dsdaten.Tables[0].Rows[0][0].ToString();
|
|
MemoryStream ms = new MemoryStream(Helper.EncodeExtensions.DecodeBase642ByteArray(base64));
|
|
WordDocument document = new WordDocument(ms, FormatType.Automatic);
|
|
var formattype_original = document.ActualFormatType;
|
|
MemoryStream destms = new MemoryStream();
|
|
if (formattype_original.ToString().ToUpper() == "DOTMXXX")
|
|
{
|
|
document.Save(destms, FormatType.Docm);
|
|
}
|
|
|
|
else
|
|
{
|
|
document.Save(destms, formattype_original);
|
|
}
|
|
|
|
document.Close();
|
|
document.Dispose();
|
|
byte[] imageArray = destms.ToArray();
|
|
ms.Close();
|
|
ms.Dispose();
|
|
return Convert.ToBase64String(imageArray);
|
|
}
|
|
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, bool useseettings = false)
|
|
{
|
|
|
|
//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;
|
|
if (useseettings)
|
|
{
|
|
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 { return ""; }
|
|
|
|
}
|
|
|
|
public string Convert_Excel_To_PDF(string document)
|
|
{
|
|
try
|
|
{
|
|
using (ExcelEngine excelEngine = new ExcelEngine())
|
|
{
|
|
IApplication application = excelEngine.Excel;
|
|
application.DefaultVersion = ExcelVersion.Xlsx;
|
|
var streamexcel = new MemoryStream(Convert.FromBase64String(document));
|
|
IWorkbook workbook = application.Workbooks.Open(streamexcel);
|
|
|
|
//Initialize ExcelToPdfConverter
|
|
ExcelToPdfConverter converter = new ExcelToPdfConverter(workbook);
|
|
ExcelToPdfConverterSettings settings = new ExcelToPdfConverterSettings();
|
|
settings.EmbedFonts = true;
|
|
|
|
|
|
|
|
//Initialize PDF document
|
|
PdfDocument pdfDocument = new PdfDocument();
|
|
|
|
//Convert Excel document into PDF document
|
|
pdfDocument = converter.Convert(settings);
|
|
|
|
//Save the converted PDF document
|
|
|
|
MemoryStream outputStream = new MemoryStream();
|
|
pdfDocument.Save(outputStream);
|
|
byte[] bytes;
|
|
bytes = outputStream.ToArray();
|
|
return Convert.ToBase64String(bytes);
|
|
}
|
|
}
|
|
catch { }
|
|
return "";
|
|
}
|
|
|
|
|
|
}
|
|
|
|
}
|