using System; using System.Collections.Generic; using System.Data.SqlTypes; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Remoting.Messaging; using System.Security; using System.Security.Principal; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; using edoka_dms; using Model; using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Parsing; namespace Versandstrasse { public class clsVersandstrasse { public bool SaveBase64ToFile(string Base64String, string filename) { try { System.IO.File.WriteAllBytes(filename, Convert.FromBase64String(Base64String)); return true; } catch { return false; } } private List loadedDocuments = new List(); public void Lic() { string lickey = "MzYzODg2NkAzMjM4MmUzMDJlMzBTOWljRmxNelA1d1VGOHpGR0lxQzB6UTAwKzIxK2VBNEhBZFp5alcxb1NVPQ=="; Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(lickey); } bool logo = false; bool color = false; int left = 0; int top = 0; int height = 0; int width = 0; string pages = ""; string xml = ""; string resturi = ""; string apikey = ""; public string Prepare_PDF(string dokumentid, string tempdir, string connectionstring, string resturi, string apikey, string startuppath, versandscript vs = null, serienbrief sb = null, string sbsourcedok="") { Lic(); this.resturi=resturi; this.apikey=apikey; clsdok dok = new clsdok("", "", "", ""); if (sbsourcedok != "") { dok.dokument = sbsourcedok; } else { OnDocOffice.OfficeToPDF officeToPDF = new OnDocOffice.OfficeToPDF(); dok.dokument = officeToPDF.word_to_pdf(dokumentid, connectionstring, tempdir); } //var stream = new MemoryStream(Convert.FromBase64String(dok.dokument)); ////Originalfile laden //PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream); Database.DB db = new Database.DB(connectionstring); db.clear_parameter(); db.add_parameter("@dokumentid", dokumentid); if (sb != null) { db.add_parameter("@doktyp", sb.dokumenttypnr.ToString()); } db.Get_Tabledata("ondoc_get_versandstrasse_parameter", true, false); versandscript vscript = new versandscript(); logo = Convert.ToBoolean(db.dsdaten.Tables[0].Rows[0]["logo"]); color = Convert.ToBoolean(db.dsdaten.Tables[0].Rows[0]["logocolor"]); top = Convert.ToInt32(db.dsdaten.Tables[0].Rows[0]["logotop"]); left = Convert.ToInt32(db.dsdaten.Tables[0].Rows[0]["logoleft"]); height = Convert.ToInt32(db.dsdaten.Tables[0].Rows[0]["logoheight"]); width = Convert.ToInt32(db.dsdaten.Tables[0].Rows[0]["logowidth"]); pages = db.dsdaten.Tables[0].Rows[0]["pages"].ToString(); xml = db.dsdaten.Tables[0].Rows[0]["vscript"].ToString(); if (xml != "") { XmlSerializer serializer = new XmlSerializer(typeof(versandscript)); using (StringReader reader = new StringReader(xml)) { vscript = (versandscript)(serializer.Deserialize(reader)); } foreach (vsdokument vsd in vscript.dokuments) { } if (vscript.Split_Doc !="") { split_doc(ref vscript,dok.dokument); } if (vscript.PartCopies != "") { create_copies(ref vscript, dok.dokument); } } else { vsdokument d = new vsdokument(); d.LogoTop = top; d.LogoLeft = left; d.LogoHeigth = height; d.LogoWidth = width; d.LogoColor = color; d.Logo = logo; d.LogoOnPages = pages; d.sourcedokument = dok.dokument; vscript.dokuments.Add(d); } foreach (vsdokument vdok in vscript.dokuments) { var stream = new MemoryStream(Convert.FromBase64String(vdok.sourcedokument)); PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream); if (vdok.Logo) { Add_Logo(ref loadedDocument, vdok.LogoColor, vdok.LogoLeft, vdok.LogoTop, vdok.LogoWidth, vdok.LogoHeigth, vdok.LogoOnPages,this.resturi,this.apikey,startuppath); } if (vdok.TextReplaces.Count > 0) { foreach (TextReplace tr in vdok.TextReplaces) { if (tr.TextToReplace.Trim() != "") { Replace_Text(ref loadedDocument, tr.TextToReplace, tr.NewText, tr.Fontname, tr.Fontsize); } } //loadedDocument.Save(@"x:\r1.pdf"); } if (loadedDocument.Pages.Count % 2 == 0) { } else { Add_EmptyPage(ref loadedDocument); } MemoryStream outputStream = new MemoryStream(); loadedDocument.Save(outputStream); byte[] bytes; bytes = outputStream.ToArray(); vdok.pdfdokument = Convert.ToBase64String(bytes); } if (vscript.dokuments.Count > 1) { int i = 0; Stream[] streams = new Stream[vscript.dokuments.Count]; foreach (vsdokument vdoc in vscript.dokuments) { var stream = new MemoryStream(Convert.FromBase64String(vdoc.pdfdokument)); streams[i] = stream; i++; } PdfDocument finalDoc = new PdfDocument(); PdfDocumentBase.Merge(finalDoc, streams); MemoryStream outputStream = new MemoryStream(); finalDoc.Save(outputStream); byte[] bytes; bytes = outputStream.ToArray(); dok.dokument = Convert.ToBase64String(bytes); vscript.FinalDoc = Convert.ToBase64String(bytes); finalDoc.Close(); finalDoc.Dispose(); streams = null; vscript.FinalDoc = vscript.FinalDoc; } else { vscript.FinalDoc = vscript.dokuments[0].pdfdokument.ToString(); } return vscript.FinalDoc; } private void split_doc(ref versandscript vscript, string document) { SaveBase64ToFile(document, @"x:\f0.pdf"); vscript.dokuments[0].sourcedokument = document; vsdokument vsd = vscript.dokuments[0]; var stream = new MemoryStream(Convert.FromBase64String(vscript.dokuments[0].sourcedokument)); PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream); TextSearchOptions to = new TextSearchOptions(); Dictionary> matchedTextbounds = new Dictionary>(); loadedDocument.FindText(vscript.Split_Doc, out matchedTextbounds); PdfDocument newdoc = new PdfDocument(); PdfDocument newdoc2 = new PdfDocument(); foreach (int index in matchedTextbounds.Keys) { if (matchedTextbounds[index].Count > 0) { newdoc.ImportPage(loadedDocument, index); newdoc2.ImportPageRange(loadedDocument, index + 1, loadedDocument.Pages.Count - (index+1)); } } //newdoc.Save(@"x:\nf1.pdf"); //newdoc2.Save(@"x:\nf2.pdf"); MemoryStream outputStream = new MemoryStream(); newdoc.Save(outputStream); byte[] bytes; byte[] bytes1; bytes = outputStream.ToArray(); vscript.dokuments[0].sourcedokument = Convert.ToBase64String(bytes); outputStream.Close(); outputStream = null; MemoryStream outputStream2 = new MemoryStream(); newdoc2.Save(outputStream2); bytes1 = outputStream2.ToArray(); vscript.dokuments[1].sourcedokument = Convert.ToBase64String(bytes1); outputStream2.Close(); outputStream2 = null; //SaveBase64ToFile(vscript.dokuments[0].sourcedokument, @"x:\f1.pdf"); //SaveBase64ToFile(vscript.dokuments[1].sourcedokument, @"x:\f2.pdf"); } private void create_copies(ref versandscript vscript, string dokument) { //vsdokument vsd = vscript.dokuments[0]; //if (vsd.LogoTop == 0) { vsd.LogoTop = top; } //if (vsd.LogoLeft == 0) { vsd.LogoLeft = left; } //if (vsd.LogoHeigth == 0) { vsd.LogoHeigth = height; } //if (vsd.LogoWidth == 0) { vsd.LogoWidth = width; } //vsd.sourcedokument = dokument; int i = 1; List copyparts = vscript.PartCopies.Split(';').ToList(); foreach (string cp in copyparts) { string[] part = cp.Split(':'); int part1 = Convert.ToInt32(part[0]); int part2 = Convert.ToInt32(part[1]); vscript.dokuments[part2-1].sourcedokument = vscript.dokuments[part1-1].sourcedokument; //vsdokument source = vscript.dokuments[part1 - 1]; //if (vscript.dokuments.Count > part2) { vscript.dokuments[i] = source; } else { vscript.dokuments.Add(source); } } } private void Replace_Text(ref PdfLoadedDocument document, string texttoreplace, string newtext, string fontname, string fontsize) { PdfLoadedPage loadedPage = document.Pages[0] as PdfLoadedPage; string fname = "Futura Book"; string fsize = "9"; if (fontname != "") { fname = fontname; fsize = fontsize; } PdfFont font = new PdfTrueTypeFont(new Font(fname, Convert.ToInt32(fsize))); Dictionary> matchedTextbounds = new Dictionary>(); document.FindText(texttoreplace, out matchedTextbounds); for (int i = 0; i < matchedTextbounds.Count; i++) { for (int i1 = 0; i1 < document.Pages.Count; i1++) { loadedPage = (PdfLoadedPage)document.Pages[i1]; foreach (RectangleF rectangle in matchedTextbounds[i]) { loadedPage.Graphics.DrawRectangle(PdfBrushes.White, rectangle); loadedPage.Graphics.DrawString(newtext, font, PdfBrushes.Black, rectangle.X - 2, rectangle.Y - 2); } } } } private string get_logo(bool color, string resturi, string apikey) { int imageid = 0; if (color) { imageid = 4; } else { imageid = 5; } return get_image(imageid,-1,-1,resturi,apikey); } private string get_image(int imageid, int width, int heigh, string resturi, string apikey) { string URL = resturi + "API/GetImageAsBase64?imageid=" + imageid.ToString() + "&ImageWidth=" + width.ToString() + "&ImageHeight=" + height.ToString(); HttpWebRequest webRequest = HttpWebRequest.Create(URL) as HttpWebRequest; webRequest.Method = WebRequestMethods.Http.Get; webRequest.Headers["Authorization"] = "Bearer " + apikey; try { using (HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse) { if (response.StatusCode == HttpStatusCode.OK) { StreamReader reader = new StreamReader(response.GetResponseStream()); string responseContent = reader.ReadToEnd(); return responseContent; } else { //Logging.Logging.Error(URL + ": " + response.StatusCode.ToString() + " / " + response.StatusDescription, "Client - GetImage", ""); return ""; } } } catch (Exception ex) { return ""; } } private void Add_Logo(ref PdfLoadedDocument document, bool color, int left, int top, int width, int height, string pages, string resturi, string apikey, string startuppath) { string imagefile = ""; imagefile = startuppath + @"\images\tkb_logo_ohne_claim_sw.jpg"; if (color) { imagefile = startuppath + @"\images\tkb_logo_ohne_claim_4c_c.jpg"; } //imagefile = get_logo(color,resturi,apikey); //byte[] bytes = Convert.FromBase64String(imagefile); //MemoryStream ms = new MemoryStream(bytes); PdfBitmap image = new PdfBitmap(imagefile); if (pages == "0") { return; } if (pages == "All") { for (int i = 0; i < document.Pages.Count; i++) { PdfLoadedPage loadedPage = document.Pages[i] as PdfLoadedPage; PdfGraphics graphics = loadedPage.Graphics; graphics.DrawImage(image, 85, 30); } } else { int docpages = document.Pages.Count; string[] page = pages.Split(';'); foreach (var p in page) { int pageno = Convert.ToInt32(p) - 1; PdfLoadedPage loadedPage = document.Pages[pageno] as PdfLoadedPage; PdfGraphics graphics = loadedPage.Graphics; graphics.DrawImage(image, 85, 30); } } } private void Add_EmptyPage(ref PdfLoadedDocument document) { document.Pages.Add(); } } public class versandscript { public string bezeichnung { get; set; } = "Versandstrassenparaemter"; public int dokumenttypnr { get; set; } = 0; public string Split_Doc { get; set; } = ""; public string PartCopies { get; set; } = ""; public string FinalDoc { get; set; } = ""; public List dokuments { get; set; } = new List(); } public class vsdokument { public bool Logo { get; set; } = true; public bool LogoColor { get; set; } = false; public int LogoTop { get; set; } = 0; public int LogoLeft { get; set; } = 0; public int LogoHeigth { get; set; } = 0; public int LogoWidth { get; set; } = 0; public string LogoOnPages { get; set; } = "1"; public string sourcedokument { get; set; } = ""; public string pdfdokument { get; set; } = ""; public List TextReplaces { get; set; } = new List(); } public class TextReplace { public string TextToReplace { get; set; } = ""; public string NewText { get; set; } = ""; public string Fontname { get; set; } = ""; public string Fontsize { get; set; } = ""; } }