예제 #1
0
        public byte[] MergeFieldsInPdfFile(byte[] inputPdf, IMergeFieldsProvider sourceObject)
        {
            var pr  = new PdfReader(inputPdf);
            var ims = new MemoryStream();

            var ps = new PdfStamper(pr, ims);

            AcroFields af = ps.AcroFields;

            foreach (string fieldName in af.Fields.Keys)
            {
                try
                {
                    string content = sourceObject.GetContentsFor(fieldName);
                    af.SetField(fieldName, content);
                }
                catch
                {
                    //Field not found.
                }
            }

            ps.Close();

            byte[] output = ims.ToArray();

            return(output);
        }
        public static void ReplaceMailMergeFields(string dir, string msWordFileName, IMergeFieldsProvider domainObject, string fileSuffix)
        {
            object docName = msWordFileName;

            object missing = Missing.Value;

            // Hide MS Word's window.
            var app = new Application {
                Visible = false
            } as _Application;

            var path = dir + docName as object;

            _Document doc = app.Documents.Open(ref path);

            MailMerge mailMerge = doc.MailMerge;

            // Try to find the field name.

            foreach (MailMergeField f in mailMerge.Fields)
            {
                // Assuming the field code is: MERGEFIELD  "mailMergeFieldName"
                string mailMergeFieldName = f.Code.Text;

                string newValue = domainObject.GetContentsFor(mailMergeFieldName);

                if (!string.IsNullOrWhiteSpace(newValue))
                {
                    f.Select();

                    // Replace selected field with supplied value.

                    app.Selection.TypeText(newValue);
                }
            }

            // Save changes and close MS Word.

            object newName = dir + msWordFileName.Replace(".docx", "") + "-" + fileSuffix + ".docx";

            doc.SaveAs2(ref newName);

            object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;

            doc.Close(ref saveChanges, ref missing, ref missing);

            app.Quit(ref missing, ref missing, ref missing);
        }