コード例 #1
0
        private void AppendImageToElement(KeyValuePair <string, ImageElement> placeholder, OpenXmlElement element, WordprocessingDocument wordprocessingDocument)
        {
            string imageExtension = ImageHandler.GetImageTypeFromMemStream(placeholder.Value.memStream);

            MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;

            Uri imageUri = new Uri("/word/media/" +
                                   placeholder.Key + _imageCounter + "." + imageExtension, UriKind.Relative);

            // Create "image" part in /word/media
            // Change content type for other image types.
            PackagePart packageImagePart =
                wordprocessingDocument.Package.CreatePart(imageUri, "Image/" + imageExtension);


            // Feed data.
            placeholder.Value.memStream.Position = 0;
            byte[] imageBytes = placeholder.Value.memStream.ToArray();// File.ReadAllBytes(fileName);
            packageImagePart.GetStream().Write(imageBytes, 0, imageBytes.Length);

            PackagePart documentPackagePart =
                mainPart.OpenXmlPackage.Package.GetPart(new Uri("/word/document.xml", UriKind.Relative));

            // URI to the image is relative to relationship document.
            PackageRelationship imageRelationshipPart = documentPackagePart.CreateRelationship(
                new Uri("media/" + placeholder.Key + _imageCounter + "." + imageExtension, UriKind.Relative),
                TargetMode.Internal, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image");


            //AddImageToBody(wordprocessingDocument, imageRelationshipPart.Id);


            var imgTmp = ImageHandler.GetImageFromStream(placeholder.Value.memStream);

            var drawing = GetImageElement(imageRelationshipPart.Id, placeholder.Key, "picture", imgTmp.Width, imgTmp.Height, placeholder.Value.Dpi);

            element.AppendChild(drawing);
        }
コード例 #2
0
        public static string ReplaceAll(string html, Placeholders _rep)
        {
            if (_rep == null)
            {
                return(html);
            }

            /*
             * Replace Texts
             */
            foreach (var trDict in _rep.TextPlaceholders)
            {
                html = html.Replace(_rep.TextPlaceholderStartTag + trDict.Key + _rep.TextPlaceholderEndTag,
                                    trDict.Value);
            }

            /*
             * Replace images
             */
            foreach (var replace in _rep.ImagePlaceholders)
            {
                html = html.Replace(_rep.ImagePlaceholderStartTag + replace.Key + _rep.ImagePlaceholderEndTag,
                                    "<img src=\"data:image/" + ImageHandler.GetImageTypeFromMemStream(replace.Value.memStream) + ";base64," +
                                    ImageHandler.GetBase64FromMemStream(replace.Value.memStream) + "\"/>");
            }

            /*
             * Replace Tables
             */
            foreach (var table in _rep.TablePlaceholders) //Take a Row/Table (one Dictionary) at a time
            {
                var tableCol0 = table.First();            //This is the first placeholder in the row, so the first columns. We'll need
                //just that one to find a matching table col
                // Find the first text element matching the search string - Then we will find the row -
                // where the text (placeholder) is inside a table cell --> this is the row we are searching for.
                var placeholder = _rep.TablePlaceholderStartTag + tableCol0.Key + _rep.TablePlaceholderEndTag;

                var regex = new Regex("<tr((?!<tr)[\\s\\S])*" + placeholder + "[\\s\\S]*?</tr>");
                var match = regex.Match(html);
                if (match.Success) //Now we have the correct table and the row containing the placeholders
                {
                    string copiedRow = match.Value;
                    var    laenge    = copiedRow.Length;
                    int    differenceInNoCharacters = 0;

                    for (var newRow = 0; newRow < tableCol0.Value.Length; newRow++) //Lets create new row by new row and replace placeholders
                    {
                        for (var tableCol = 0;
                             tableCol < table.Count;
                             tableCol++) //Now cycle through the "columns" (keys) of the Dictionary and replace item by item
                        {
                            var colPlaceholder = table.ElementAt(tableCol);

                            if (html.Contains(_rep.TablePlaceholderStartTag + colPlaceholder.Key + _rep.TablePlaceholderEndTag))
                            {
                                var oldHtml = html;
                                html = html.Replace(
                                    _rep.TablePlaceholderStartTag + colPlaceholder.Key + _rep.TablePlaceholderEndTag,
                                    colPlaceholder.Value[newRow]);
                                differenceInNoCharacters += html.Length - oldHtml.Length;
                            }
                        }



                        if (newRow < tableCol0.Value.Length - 1)//If we have not reached the end of the rows to insert, we
                        //can insert the resulting row
                        {
                            html = html.Insert((match.Index + ((newRow + 1) * match.Length) + differenceInNoCharacters), copiedRow);
                        }
                    }

                    html = html.Replace(_rep.NewLineTag, "<br>");
                }
            }

            return(html);
        }