public void FieldAdvance() { //ExStart //ExFor:Fields.FieldAdvance //ExFor:Fields.FieldAdvance.DownOffset //ExFor:Fields.FieldAdvance.HorizontalPosition //ExFor:Fields.FieldAdvance.LeftOffset //ExFor:Fields.FieldAdvance.RightOffset //ExFor:Fields.FieldAdvance.UpOffset //ExFor:Fields.FieldAdvance.VerticalPosition //ExSummary:Shows how to insert an advance field and edit its properties. Document doc = new Document(); DocumentBuilder builder = new DocumentBuilder(doc); builder.Write("This text is in its normal place."); // Create an advance field using document builder FieldAdvance field = (FieldAdvance)builder.InsertField(FieldType.FieldAdvance, true); builder.Write("This text is moved up and to the right."); Assert.AreEqual(FieldType.FieldAdvance, field.Type); Assert.AreEqual(" ADVANCE ", field.GetFieldCode()); // The second text that the builder added will now be moved field.RightOffset = "5"; field.UpOffset = "5"; Assert.AreEqual(" ADVANCE \\r 5 \\u 5", field.GetFieldCode()); // If we want to move text in the other direction, and try do that by using negative values for the above field members, we will get an error in our document // Instead, we need to specify a positive value for the opposite respective field directional variable field = (FieldAdvance)builder.InsertField(FieldType.FieldAdvance, true); field.DownOffset = "5"; field.LeftOffset = "100"; Assert.AreEqual(" ADVANCE \\d 5 \\l 100", field.GetFieldCode()); // We are still on one paragraph Assert.AreEqual(1, doc.FirstSection.Body.Paragraphs.Count); // Since we're setting horizontal and vertical positions next, we need to end the paragraph so the previous line does not get moved with the next one builder.Writeln("This text is moved down and to the left, overlapping the previous text."); // This time we can also use negative values field = (FieldAdvance)builder.InsertField(FieldType.FieldAdvance, true); field.HorizontalPosition = "-100"; field.VerticalPosition = "200"; Assert.AreEqual(" ADVANCE \\x -100 \\y 200", field.GetFieldCode()); builder.Write("This text is in a custom position."); doc.Save(MyDir + @"\Artifacts\Field.Advance.docx"); //ExEnd }
public static void Run() { //ExStart:InsertAdvanceFieldWithOutDocumentBuilder // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithFields(); Document doc = new Document(dataDir + "in.doc"); // Get paragraph you want to append this Advance field to Paragraph para = (Paragraph)doc.GetChildNodes(NodeType.Paragraph, true)[1]; // We want to insert an Advance field like this: // { ADVANCE \\d 10 \\l 10 \\r -3.3 \\u 0 \\x 100 \\y 100 } // Create instance of FieldAdvance class and lets build the above field code FieldAdvance field = (FieldAdvance)para.AppendField(FieldType.FieldAdvance, false); // { ADVANCE \\d 10 " } field.DownOffset = "10"; // { ADVANCE \\d 10 \\l 10 } field.LeftOffset = "10"; // { ADVANCE \\d 10 \\l 10 \\r -3.3 } field.RightOffset = "-3.3"; // { ADVANCE \\d 10 \\l 10 \\r -3.3 \\u 0 } field.UpOffset = "0"; // { ADVANCE \\d 10 \\l 10 \\r -3.3 \\u 0 \\x 100 } field.HorizontalPosition = "100"; // { ADVANCE \\d 10 \\l 10 \\r -3.3 \\u 0 \\x 100 \\y 100 } field.VerticalPosition = "100"; // Finally update this Advance field field.Update(); dataDir = dataDir + "InsertAdvanceFieldWithOutDocumentBuilder_out_.doc"; doc.Save(dataDir); //ExEnd:InsertAdvanceFieldWithOutDocumentBuilder Console.WriteLine("\nAdvance field without using document builder inserted successfully.\nFile saved at " + dataDir); }