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; 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/V1NBaF5cXmZCf1FpRmJGdld5fUVHYVZUTXxaS00DNHVRdkdnWXxccXVWR2VdUUZ3VkM="); } 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 GetDocAsPDF(string Docid) { clsdok dok = GetDoc(Docid); try { switch (dok.doktype.ToUpper().Substring(0,1)) { case "D": dok.dokument = Convert_Word_To_PDF(dok.dokument.ToString()); return dok; break; case "X": dok.dokument = Convert_Excel_To_PDF(dok.dokument.ToString()); return dok; break; case "P": return dok; 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) { var streamword = new MemoryStream(Convert.FromBase64String(document)); WordDocument wordDocument = new WordDocument(streamword, Syncfusion.DocIO.FormatType.Automatic); using (Syncfusion.DocToPDFConverter.DocToPDFConverter converter = new Syncfusion.DocToPDFConverter.DocToPDFConverter()) { converter.Settings.EmbedFonts = 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); } }; } private string Convert_Excel_To_PDF(string document) { return ""; } public void Print_Doc(string document) { SynFWord sfword = new SynFWord(); sfword.Print_Word(document); } } }