Examples: Working with text in Java classes

  1. This agent gets the text in a rich text item a paragraph at a time.
    import lotus.domino.*;
    
    public class JavaAgent extends AgentBase {
    
      public void NotesMain() {
    
        try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();
    
          // (Your code goes here) 
          DocumentCollection dc = agentContext.getUnprocessedDocuments();
          Document doc = dc.getFirstDocument();
          RichTextItem body = (RichTextItem)doc.getFirstItem("Body");
          RichTextNavigator rtnav = body.createNavigator();
          if (rtnav.findFirstElement(
          RichTextItem.RTELEM_TYPE_TEXTPARAGRAPH)) {
            RichTextRange rtrange = body.createRange();
            int count = 0;
            do {
              count++;
              rtrange.setBegin(rtnav);
              System.out.println(
                count + ") " + rtrange.getTextParagraph());
            } while (rtnav.findNextElement());
          }
          else
            System.out.println("No text in Body item");
    
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  2. This agent gets the paragraphs in a rich text item, and the runs within each paragraph.
    import lotus.domino.*;
    
    public class JavaAgent extends AgentBase {
    
      public void NotesMain() {
    
        try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();
    
          // (Your code goes here) 
          DocumentCollection dc = agentContext.getUnprocessedDocuments();
          Document doc = dc.getFirstDocument();
          RichTextItem body = (RichTextItem)doc.getFirstItem("Body");
          RichTextNavigator rtnav = body.createNavigator();
          if (rtnav.findFirstElement(
          RichTextItem.RTELEM_TYPE_TEXTPARAGRAPH)) {
            RichTextRange rtrange = body.createRange();
          //  RichTextNavigator rtnav2 = body.createNavigator();
            RichTextRange rtrange2 = body.createRange();
            int count = 0;
            do {
              count++;
              // Set range for paragraph
              rtrange.setBegin(rtnav);
              rtrange.setEnd(rtnav);
              // Create navigator for paragraph
              RichTextNavigator rtnav2 = rtrange.getNavigator();
              // Find text runs in paragraph
              if (rtnav2.findFirstElement(
              RichTextItem.RTELEM_TYPE_TEXTRUN)) {
                int count2 = 0;
                do {
                  count2++;
                  // Set range for text run
                  rtrange2.setBegin(rtnav2);
                  rtrange2.setEnd(rtnav2);
                  // Print text of run
                  System.out.println(
                  count + ", " + count2 + ") " + rtrange2.getTextRun
                } while (rtnav2.findNextElement(
                    RichTextItem.RTELEM_TYPE_TEXTRUN));
              }
            } while (rtnav.findNextElement(
                RichTextItem.RTELEM_TYPE_TEXTPARAGRAPH));
          }
          else
            System.out.println("No text in Body item");
    
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  3. This agent creates a rich text item and populates it with two paragraphs of text.
    import lotus.domino.*;
    
    public class JavaAgent extends AgentBase {
    
      public void NotesMain() {
    
        try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();
    
          // (Your code goes here) 
          Database db = agentContext.getCurrentDatabase();
          Document doc = db.createDocument();
          doc.appendItemValue("Form", "MainTopic");
          doc.appendItemValue("Subject",
            "CreateRichTextItem");
          // Create rich text item and append text
          RichTextItem rti = doc.createRichTextItem("Body");
          rti.appendText("First paragraph.");
          rti.addNewLine(1);
          rti.appendText("Second paragraph.");
          doc.save(true, true);
    
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  4. This example creates a rich text item and uses several styles for the appended text.
    import lotus.domino.*;
    
    public class JavaAgent extends AgentBase {
    
      public void NotesMain() {
    
        try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();
    
          // (Your code goes here) 
          Database db = agentContext.getCurrentDatabase();
          Document doc = db.createDocument();
          doc.appendItemValue("Form", "Memo");
          doc.appendItemValue("Subject",
            "Meeting time changed");
          // Create 2 styles
          RichTextStyle rts1 = session.createRichTextStyle();
          rts1.setBold(RichTextStyle.NO);
          rts1.setFontSize(12);
          RichTextStyle rts2 = session.createRichTextStyle();
          rts2.setBold(RichTextStyle.YES);
          rts2.setFontSize(18);
          // Create rich text item and append styles and text
          RichTextItem rti = doc.createRichTextItem("Body");
          rti.appendStyle(rts1);
          rti.appendText("The meeting is at ");
          rti.appendStyle(rts2);
          rti.appendText("2 PM");
          rti.appendStyle(rts1);
          rti.appendText(" not 3 PM.");
          doc.send("MyGroup");
    
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  5. This example creates a rich text paragraph style and displays its properties.
    import lotus.domino.*;
    
    public class JavaAgent extends AgentBase {
    
      public void NotesMain() {
    
        try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();
    
          // (Your code goes here) 
          RichTextParagraphStyle rtps = session.createRichTextParagraphStyle();
          // Alignment
          switch (rtps.getAlignment()) {
            case RichTextParagraphStyle.ALIGN_CENTER:
              System.out.println("Alignment: Center"); break;
            case RichTextParagraphStyle.ALIGN_FULL:
              System.out.println("Alignment: Full"); break;
            case RichTextParagraphStyle.ALIGN_LEFT:
              System.out.println("Alignment: Left"); break;
            case RichTextParagraphStyle.ALIGN_NOWRAP:
              System.out.println("Alignment: No wrap"); break;
            case RichTextParagraphStyle.ALIGN_RIGHT:
              System.out.println("Alignment: Right"); break;
          }
          // First line left margin
          float margin = rtps.getFirstLineLeftMargin();
          System.out.println("First line left margin (inches): " +
            (margin / RichTextParagraphStyle.RULER_ONE_INCH));
          System.out.println("First line left margin (centimeters): " +
            (margin / RichTextParagraphStyle.RULER_ONE_CENTIMETER));
          // Interline spacing
          switch (rtps.getInterLineSpacing()) {
            case RichTextParagraphStyle.SPACING_DOUBLE:
              System.out.println("Interline spacing: Double"); break;
            case RichTextParagraphStyle.SPACING_ONE_POINT_50:
              System.out.println("Interline spacing: 1.5"); break;
            case RichTextParagraphStyle.SPACING_SINGLE:
              System.out.println("Interline spacing: Single"); break;
          }
          // Left margin
          margin = rtps.getLeftMargin();
          System.out.println("Left margin (inches): " +
            (margin / RichTextParagraphStyle.RULER_ONE_INCH));
          System.out.println("Left margin (centimeters): " +
            (margin / RichTextParagraphStyle.RULER_ONE_CENTIMETER));
          // Right margin
          margin = rtps.getRightMargin();
          System.out.println("Right margin (inches): " +
            (margin / RichTextParagraphStyle.RULER_ONE_INCH));
          System.out.println("Right margin (centimeters): " +
            (margin / RichTextParagraphStyle.RULER_ONE_CENTIMETER));
          // Spacing
          switch (rtps.getSpacingAbove()) {
            case RichTextParagraphStyle.SPACING_DOUBLE:
              System.out.println("Spacing: Double"); break;
            case RichTextParagraphStyle.SPACING_ONE_POINT_50:
              System.out.println("Spacing: 1.5"); break;
            case RichTextParagraphStyle.SPACING_SINGLE:
              System.out.println("Spacing: Single"); break;
          }
          // Spacing below
          switch (rtps.getSpacingBelow()) {
            case RichTextParagraphStyle.SPACING_DOUBLE:
              System.out.println("Spacing below: Double"); break;
            case RichTextParagraphStyle.SPACING_ONE_POINT_50:
              System.out.println("Spacing below: 1.5"); break;
            case RichTextParagraphStyle.SPACING_SINGLE:
              System.out.println("Spacing below: Single"); break;
          }
    
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }
  6. This example changes styles and paragraph styles in a rich text item.
    import lotus.domino.*;
    
    public class JavaAgent extends AgentBase {
    
      public void NotesMain() {
    
        try {
          Session session = getSession();
          AgentContext agentContext = session.getAgentContext();
    
          // (Your code goes here) 
          Database db = agentContext.getCurrentDatabase();
          Document doc = db.createDocument();
          doc.appendItemValue("Form", "MainTopic");
          doc.appendItemValue("Subject", "Q1 report");
          // Create rich text item and append styles and text
          RichTextItem rti = doc.createRichTextItem("Body");
          RichTextParagraphStyle rtps = session.createRichTextParagraphStyle();
          RichTextStyle rts = session.createRichTextStyle();
            // Heading
          rtps.setAlignment(RichTextParagraphStyle.ALIGN_CENTER);
          rtps.setSpacingBelow(RichTextParagraphStyle.SPACING_ONE_POINT_50);
          rts.setBold(RichTextStyle.YES);
          rti.appendStyle(rts);
          rti.appendParagraphStyle(rtps);
          rti.appendText("Q1 Report");
          rti.addNewLine(1);
            // Regular paragraphs
          rtps.setAlignment(RichTextParagraphStyle.ALIGN_LEFT);
          rtps.setSpacingBelow(RichTextParagraphStyle.SPACING_SINGLE);
          rts.setBold(RichTextStyle.NO);
          rti.appendStyle(rts);
          rti.appendParagraphStyle(rtps);
          rti.appendText("Blah. Blah. Blah.");
          doc.save(true, true);
    
        } catch(Exception e) {
          e.printStackTrace();
        }
      }
    }