Exemplo n.º 1
0
        public void FieldOptionsCurrentUser()
        {
            //ExStart
            //ExFor:Document.UpdateFields
            //ExFor:FieldOptions.CurrentUser
            //ExFor:UserInformation
            //ExFor:UserInformation.Name
            //ExFor:UserInformation.Initials
            //ExFor:UserInformation.Address
            //ExFor:UserInformation.DefaultUser
            //ExSummary:Shows how to set user details and display them with fields.
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            // Set user information
            UserInformation userInformation = new UserInformation();

            userInformation.Name         = "John Doe";
            userInformation.Initials     = "J. D.";
            userInformation.Address      = "123 Main Street";
            doc.FieldOptions.CurrentUser = userInformation;

            // Insert fields that reference our user information
            Assert.AreEqual(userInformation.Name, builder.InsertField(" USERNAME ").Result);
            Assert.AreEqual(userInformation.Initials, builder.InsertField(" USERINITIALS ").Result);
            Assert.AreEqual(userInformation.Address, builder.InsertField(" USERADDRESS ").Result);

            // The field options object also has a static default user value that fields from many documents can refer to
            UserInformation.DefaultUser.Name     = "Default User";
            UserInformation.DefaultUser.Initials = "D. U.";
            UserInformation.DefaultUser.Address  = "One Microsoft Way";
            doc.FieldOptions.CurrentUser         = UserInformation.DefaultUser;

            Assert.AreEqual("Default User", builder.InsertField(" USERNAME ").Result);
            Assert.AreEqual("D. U.", builder.InsertField(" USERINITIALS ").Result);
            Assert.AreEqual("One Microsoft Way", builder.InsertField(" USERADDRESS ").Result);

            doc.UpdateFields();
            doc.Save(ArtifactsDir + "FieldOptions.FieldOptionsCurrentUser.docx");
            //ExEnd

            doc = new Document(ArtifactsDir + "FieldOptions.FieldOptionsCurrentUser.docx");

            Assert.Null(doc.FieldOptions.CurrentUser);

            FieldUserName fieldUserName = (FieldUserName)doc.Range.Fields[0];

            Assert.Null(fieldUserName.UserName);
            Assert.AreEqual("Default User", fieldUserName.Result);

            FieldUserInitials fieldUserInitials = (FieldUserInitials)doc.Range.Fields[1];

            Assert.Null(fieldUserInitials.UserInitials);
            Assert.AreEqual("D. U.", fieldUserInitials.Result);

            FieldUserAddress fieldUserAddress = (FieldUserAddress)doc.Range.Fields[2];

            Assert.Null(fieldUserAddress.UserAddress);
            Assert.AreEqual("One Microsoft Way", fieldUserAddress.Result);
        }
Exemplo n.º 2
0
        public void CurrentUser()
        {
            //ExStart
            //ExFor:Document.UpdateFields
            //ExFor:FieldOptions.CurrentUser
            //ExFor:UserInformation
            //ExFor:UserInformation.Name
            //ExFor:UserInformation.Initials
            //ExFor:UserInformation.Address
            //ExFor:UserInformation.DefaultUser
            //ExSummary:Shows how to set user details, and display them using fields.
            Document        doc     = new Document();
            DocumentBuilder builder = new DocumentBuilder(doc);

            // Create a UserInformation object and set it as the data source for fields that display user information.
            UserInformation userInformation = new UserInformation
            {
                Name     = "John Doe",
                Initials = "J. D.",
                Address  = "123 Main Street"
            };

            doc.FieldOptions.CurrentUser = userInformation;

            // Insert USERNAME, USERINITIALS, and USERADDRESS fields, which display values of
            // the respective properties of the UserInformation object that we have created above.
            Assert.AreEqual(userInformation.Name, builder.InsertField(" USERNAME ").Result);
            Assert.AreEqual(userInformation.Initials, builder.InsertField(" USERINITIALS ").Result);
            Assert.AreEqual(userInformation.Address, builder.InsertField(" USERADDRESS ").Result);

            // The field options object also has a static default user that fields from all documents can refer to.
            UserInformation.DefaultUser.Name     = "Default User";
            UserInformation.DefaultUser.Initials = "D. U.";
            UserInformation.DefaultUser.Address  = "One Microsoft Way";
            doc.FieldOptions.CurrentUser         = UserInformation.DefaultUser;

            Assert.AreEqual("Default User", builder.InsertField(" USERNAME ").Result);
            Assert.AreEqual("D. U.", builder.InsertField(" USERINITIALS ").Result);
            Assert.AreEqual("One Microsoft Way", builder.InsertField(" USERADDRESS ").Result);

            doc.UpdateFields();
            doc.Save(ArtifactsDir + "FieldOptions.CurrentUser.docx");
            //ExEnd

            doc = new Document(ArtifactsDir + "FieldOptions.CurrentUser.docx");

            Assert.Null(doc.FieldOptions.CurrentUser);

            FieldUserName fieldUserName = (FieldUserName)doc.Range.Fields[0];

            Assert.Null(fieldUserName.UserName);
            Assert.AreEqual("Default User", fieldUserName.Result);

            FieldUserInitials fieldUserInitials = (FieldUserInitials)doc.Range.Fields[1];

            Assert.Null(fieldUserInitials.UserInitials);
            Assert.AreEqual("D. U.", fieldUserInitials.Result);

            FieldUserAddress fieldUserAddress = (FieldUserAddress)doc.Range.Fields[2];

            Assert.Null(fieldUserAddress.UserAddress);
            Assert.AreEqual("One Microsoft Way", fieldUserAddress.Result);
        }