Esempio n. 1
0
        internal static byte[] MergeWithAspose(byte[] doc, Dictionary <string, byte[]> attachments)
        {
            if (attachments == null || attachments.Count == 0)
            {
                return(doc);
            }

            var licensePath = ProjDirectory + @"\lib\Aspose.Total.lic";

            AsposeLicenseManager.RegWordsLibrary(AsposeLicenseManager.LicenceType.Production, licensePath, true);

            using (var outputStream = new MemoryStream())
            {
                using (var docStream = new MemoryStream(doc))
                {
                    var    currentDoc = new Document(docStream);
                    string text       = currentDoc.Cast <Section>().Aggregate("", (current, section) => current + section.GetText());

                    var matches            = MergeRegex.Matches(text);
                    var matchedAttachments = GetMatchedAttachments(attachments, matches);

                    var outputDoc = MergeAttachmentsInOutputDoc(currentDoc, matchedAttachments);
                    outputDoc.UpdatePageLayout();
                    outputDoc.Save(outputStream, SaveFormat.Docx);
                    outputDoc.Save(ProjDirectory + @"\output\Test_generated.docx", Aspose.Words.SaveFormat.Docx);
                    outputDoc.Save(ProjDirectory + @"\output\Test_generated.pdf", Aspose.Words.SaveFormat.Pdf);
                }
                return(outputStream.ToArray());
            }
        }