using EDOKA_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; namespace DOCGEN { public class clsGetDoc { private string connectionstring = ""; public clsGetDoc(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 string GetDocAsPDF(string Docid) { clsdok dok = GetDoc(Docid); try { switch (dok.doktype.ToUpper().Substring(0,1)) { case "W": return Convert_Word_To_PDF(dok.dokument.ToString()); break; case "X": return Convert_Excel_To_PDF(dok.dokument.ToString()); break; case "P": return (dok.dokument.ToString()); break; default: return ""; } } catch { return ""; } return dok.dokument; } public clsdok GetDoc(string Docid) { EDOKA_DB db = new EDOKA_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; } } private 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()) { //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 ""; } } }