using SanShared; using Syncfusion.DocIO.DLS; using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BerichtGen { public class Bericht { private List _listImages; /// /// Erstellt /// /// Die zuverwendete Vorlagenname /// Die name zum Speichern der Datei /// Pfad zum Speichern /// Inhalt vom Dokument /// Ein doc datei soll erzeugt werden /// Ein Pdf datei soll erzeugt werden public void Erzeuge(string firma, string vorlage, string savepath, Hashtable daten,List bilderObjects, bool erzeugeDOC = false , bool erzeugePDF = true) { _listImages = new List(); foreach(BilderObject current in bilderObjects) { Image image = Image.FromFile(current.Path); _listImages.Add(ResizeImage(image, CalculateImageSizeForDocument(image.Height, image.Width))); image.Dispose(); } WordDocument wordDocument = new WordDocument("./documents/JUME/liner_einbau.docx"); string[] fieldnames = null; string[] fieldvalues = null; if(fieldnames == null || fieldvalues == null) { fieldnames = new string[daten.Count]; fieldvalues = new string[daten.Count]; } uint counter = 0; foreach(DictionaryEntry hashtable in daten) { fieldnames[counter] = hashtable.Key.ToString(); if (hashtable.Value == null) { fieldvalues[counter] = ""; } else { fieldvalues[counter] = hashtable.Value.ToString(); } counter++; } //wordDocument.MailMerge.Execute(fieldnames, fieldvalues); wordDocument.MailMerge.MergeImageField += new MergeImageFieldEventHandler(MailMerge_MergeImageField); //string[] fieldNames = new string[] { "UVImage" }; //string[] fieldValues = new string[] { "test.png" }; wordDocument.MailMerge.Execute(fieldnames, fieldvalues); wordDocument.Save("test.docx", Syncfusion.DocIO.FormatType.Docx); //wordDocument.Close(); //wordDocument.MailMerge.ExecuteGroup() } private readonly double _cmPixel = 0.393700787; private readonly int _dpi = 120; private readonly double _imgWidthCmMax = 8.0; private Size CalculateImageSizeForDocument(int height, int width) { double num = (double)height / (double)width; double num2 = this._imgWidthCmMax * num; int h = (int)(num2 * _cmPixel * (double)_dpi); int w = (int)(_imgWidthCmMax * _cmPixel * (double)_dpi); return new Size(w, h); } private Image ResizeImage(Image image, Size size) { int width = image.Width; int height = image.Height; float num = (float)size.Width / (float)width; float num2 = (float)size.Height / (float)height; float num3 = (num2 < num) ? num2 : num; int width2 = (int)((float)width * num3); int height2 = (int)((float)height * num3); Bitmap bitmap = new Bitmap(width2, height2); try { bitmap.SetResolution(120f, 120f); Graphics graphics = Graphics.FromImage(bitmap); graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; graphics.DrawImage(image, 0, 0, width2, height2); graphics.Dispose(); } catch(Exception ex) { } finally { System.GC.Collect(); } return bitmap; } private void MailMerge_MergeImageField(object sender, MergeImageFieldEventArgs args) { if(args.FieldName == "UVImage") { string source = args.FieldValue.ToString(); args.Image = Image.FromFile(source); } } } }