Package org.odftoolkit.simple

Examples of org.odftoolkit.simple.TextDocument


 
 
  @Test
  public void testGetVariableFieldByName() {
    try {
      TextDocument doc = TextDocument.newTextDocument();
      Header header = doc.getHeader();
     
      header.declareVariable("headername", VariableType.USER);
      VariableField vField = header.getVariableFieldByName("headername");
      String vName = vField.getVariableName();
     
      //validate
      StyleHeaderElement styleHead = header.getOdfElement();
      Node nod = styleHead.getFirstChild().getFirstChild();
      NamedNodeMap nameMap = nod.getAttributes();
      Node nodtext = nameMap.getNamedItem("text:name");
      Assert.assertEquals(vName, nodtext.getNodeValue());

      //save
      doc.save(ResourceUtilities.newTestOutputFile("headerTableOutput.odt"));
    } catch (Exception e) {
      Logger.getLogger(HeaderTest.class.getName()).log(Level.SEVERE, null, e);
      Assert.fail(e.getMessage());
    }
  }
View Full Code Here


  }

  @Test
  public void testAppendNewSection() {
    try {
      TextDocument doc = TextDocument.newTextDocument();
      Header header = doc.getHeader();
      Section sect = header.appendSection("Section1");
      Assert.assertNotNull(sect);

      StyleHeaderElement styleHead = header.getOdfElement();
      Node nod = styleHead.getFirstChild();
View Full Code Here

      .getName());

  @Test
  public void testCreateDefaultTOC() {
    try {
      TextDocument doc = buildSample();
      Assert.assertNotNull(doc);
     
      Paragraph paragraph1 = doc.getParagraphByIndex(0, true);
      TextTableOfContentElement textTableOfContentElement = doc.createDefaultTOC(paragraph1,false);
      Assert.assertNotNull(textTableOfContentElement)
     
      Node pnode = paragraph1.getOdfElement().getNextSibling();
     
      if (pnode.equals(textTableOfContentElement)) {
        Assert.assertTrue(true);
      } else {
        Assert.fail();
      }
     
     
      try {
        // Should throw error!
        doc.createDefaultTOC(null, true);
        Assert.fail();
      } catch (Exception e) {
//        LOG.log(Level.SEVERE, null, e);
        Assert.assertTrue(true);
      }
      doc.save(ResourceUtilities.newTestOutputFile("DefaultTOC.odt"));
    } catch (Exception e) {
      LOG.log(Level.SEVERE, null, e);
      Assert.fail();
    }
  }
View Full Code Here

  }

  @Test
  public void testCreateStyleTOC() {
    try {
      TextDocument doc = buildSample();
      Assert.assertNotNull(doc);

      TOCStyle tocstyle = new TOCStyle();
      tocstyle.addStyle("User_20_Index_20_1", 1);
      tocstyle.addStyle("User_20_Index_20_2", 2);
      tocstyle.addStyle("User_20_Index_20_3", 3);
      tocstyle.addStyle("User_20_Index_20_4", 4);
      tocstyle.addStyle("User_20_Index_20_5", 5);
      tocstyle.addStyle("User_20_Index_20_6", 6);
      tocstyle.addStyle("User_20_Index_20_7", 7);
      tocstyle.addStyle("User_20_Index_20_8", 8);
      tocstyle.addStyle("User_20_Index_20_9", 9);
      tocstyle.addStyle("User_20_Index_20_10", 10);
      LOG.info(tocstyle.toString());

      Paragraph paragraph1 = doc.getParagraphByIndex(0, true);
      TextTableOfContentElement textTableOfContentElement = doc
          .createTOCwithStyle(paragraph1, tocstyle, true);
      Assert.assertNotNull(textTableOfContentElement);
      Node pnode = textTableOfContentElement.getNextSibling();
      if (pnode.equals(paragraph1.getOdfElement())) {
        Assert.assertTrue(true);
      } else {
        Assert.fail();
      }
      try {
        // Should throw error!
        doc.createTOCwithStyle(null, tocstyle, true);
        Assert.fail();
      } catch (Exception e) {
//        LOG.log(Level.SEVERE, null, e);
        Assert.assertTrue(true);
      }
      doc.save(ResourceUtilities.newTestOutputFile("TOC_Styles.odt"));
    } catch (Exception e) {
      LOG.log(Level.SEVERE, null, e);
      Assert.fail();
    }
View Full Code Here

    }

  }

  private TextDocument buildSample() throws Exception {
    TextDocument doc = TextDocument.newTextDocument();
    Paragraph p1 = doc
        .addParagraph("1.This is a test paragraph apply with Heading 1 style!");
    p1.applyHeading();
    Paragraph p11 = doc
        .addParagraph("1.1This is a test paragraph apply with heading 2 style!");
    p11.applyHeading(true, 2);
    Paragraph p2 = doc
        .addParagraph("2.This is a test paragraph apply with Heading 1 style!");
    p2.applyHeading();
    Paragraph p21 = doc
        .addParagraph("2.1This is a test paragraph apply with Heading 2 style!");
    p21.applyHeading(true, 2);
    Paragraph p22 = doc
        .addParagraph("2.2This is a test paragraph apply with Heading 2 style!");
    p22.applyHeading(true, 2);
    Paragraph p221 = doc
        .addParagraph("2.2.1This is a test paragraph apply with Heading 3 style!");
    p221.applyHeading(true, 3);
    Paragraph p222 = doc
        .addParagraph("2.2.2This is a test paragraph apply with Heading 3 style!");
    p222.applyHeading(true, 3);
    Paragraph p23 = doc
        .addParagraph("2.3This is a test paragraph apply with Heading 2 style!");
    p23.applyHeading(true, 2);
    Paragraph p3 = doc
        .addParagraph("3.This is a test paragraph apply with ");
    p3.applyHeading();
    TextHElement h3 = (TextHElement) p3.getOdfElement();
    TextTocMarkElement TextTocMark = h3
        .newTextTocMarkElement("TextTocMarkElement");
    TextTocMark.setTextOutlineLevelAttribute(2);
    h3.newTextNode("Heading ");
    TextTocMarkStartElement TextTocMarkStart = h3
        .newTextTocMarkStartElement("IMark159230668");
    TextTocMarkStart.setTextOutlineLevelAttribute(3);
    h3.newTextNode("1 style!");
    h3.newTextTocMarkEndElement("IMark159230668");

    Paragraph p4 = doc
        .addParagraph("4.This is a test paragraph apply with User Index 1 style!");
    p4.getOdfElement().setStyleName("User_20_Index_20_1");

    Paragraph p5 = doc
        .addParagraph("5.This is a test paragraph apply with User Index 1 style!");
    p5.getOdfElement().setStyleName("User_20_Index_20_1");

    Paragraph p51 = doc
        .addParagraph("5.1 This is a test paragraph apply with User Index 2 style!");
    p51.getOdfElement().setStyleName("User_20_Index_20_2");

    Paragraph p52 = doc
        .addParagraph("5.2 This is a test paragraph apply with User Index 2 style!");
    p52.getOdfElement().setStyleName("User_20_Index_20_2");

    Paragraph p6 = doc
        .addParagraph("6.This is a test paragraph apply with User Index 1 style!");
    p6.getOdfElement().setStyleName("User_20_Index_20_1");

    Paragraph p62 = doc
        .addParagraph("6.1This is a test paragraph apply with User Index 2 style!");
    p62.getOdfElement().setStyleName("User_20_Index_20_2");
    Paragraph p63 = doc
        .addParagraph("6.1.1This is a test paragraph apply with User Index 3 style!");
    p63.getOdfElement().setStyleName("User_20_Index_20_3");
    Paragraph p64 = doc
        .addParagraph("6.1.1.1This is a test paragraph apply with User Index 4 style!");
    p64.getOdfElement().setStyleName("User_20_Index_20_4");
    Paragraph p65 = doc
        .addParagraph("6.1.1.1.1This is a test paragraph apply with User Index 5 style!");
    p65.getOdfElement().setStyleName("User_20_Index_20_5");
    Paragraph p66 = doc
        .addParagraph("6.1.1.1.1.1This is a test paragraph apply with User Index 6 style!");
    p66.getOdfElement().setStyleName("User_20_Index_20_6");
    Paragraph p67 = doc
        .addParagraph("6.1.1.1.1.1.1.1This is a test paragraph apply with User Index 7 style!");
    p67.getOdfElement().setStyleName("User_20_Index_20_7");
    Paragraph p68 = doc
        .addParagraph("6.1.1.1.1.1.1.1.1This is a test paragraph apply with User Index 8 style!");
    p68.getOdfElement().setStyleName("User_20_Index_20_8");
    Paragraph p69 = doc
        .addParagraph("6.1.1.1.1.1.1.1.1.1This is a test paragraph apply with User Index 9 style!");
    p69.getOdfElement().setStyleName("User_20_Index_20_9");
    Paragraph p60 = doc
        .addParagraph("6.1.1.1.1.1.1.1.1.1.1This is a test paragraph apply with User Index 10 style!");
    p60.getOdfElement().setStyleName("User_20_Index_20_10");
    return doc;
  }
View Full Code Here

  String footerDocumentPath = "FooterTableDocument.odt";

  @Test
  public void testAddTable() {
    try {
      TextDocument doc = TextDocument.newTextDocument();
      Footer footer = doc.getFooter();
      Assert.assertNotNull(footer);

      Table table = footer.addTable(1, 1);
      table.setTableName("footerTable");
      int rowCount = table.getRowCount();
      int columnCount = table.getColumnCount();
      String expectedCellValue = "footer table cell";
      Cell cellByPosition = table.getCellByPosition(0, 0);
      cellByPosition.setStringValue(expectedCellValue);
      cellByPosition.setHorizontalAlignment(HorizontalAlignmentType.CENTER);
      cellByPosition.setCellBackgroundColor(Color.GREEN);
     
      //first page
      footer = doc.getFooter(true);
      Assert.assertNotNull(footer);
      table = footer.addTable(1, 2);
      table.setTableName("footerFTable");
      doc.save(ResourceUtilities.newTestOutputFile(footerDocumentPath));

      // load the document again.
      doc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream(footerDocumentPath));
      footer = doc.getFooter();
      table = footer.getTableByName("footerTable");
      Assert.assertEquals(rowCount, table.getRowCount());
      Assert.assertEquals(columnCount, table.getColumnCount());
      Assert.assertEquals(expectedCellValue, cellByPosition.getStringValue());

      footer = doc.getFooter(true);
      table = footer.getTableByName("footerFTable");
      Assert.assertNotNull(table);
    } catch (Exception e) {
      Logger.getLogger(FooterTest.class.getName()).log(Level.SEVERE, null, e);
      Assert.fail(e.getMessage());
View Full Code Here

  }
 
  @Test
  public void testFooterHidden() {
    try {
      TextDocument doc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("headerFooterHidden.odt"));
      Footer footer = doc.getFooter();
      Assert.assertEquals(true, footer.isVisible());
      footer.setVisible(false);
      Assert.assertEquals(false, footer.isVisible());
      doc.save(ResourceUtilities.newTestOutputFile("footerHiddenOutput.odt"));
    } catch (Exception e) {
      Logger.getLogger(FooterTest.class.getName()).log(Level.SEVERE, null, e);
      Assert.fail(e.getMessage());
    }
  }
View Full Code Here

 
  @Test
  public void testGetOdfElement() {
    try {
      //TextDocument doc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("headerFooterHidden.odt"));
      TextDocument doc = TextDocument.newTextDocument();
      Footer footer = doc.getFooter();
      StyleFooterElement footerEle = footer.getOdfElement();
      footerEle.setTextContent("hello world");
      Assert.assertEquals("hello world", footerEle.getTextContent());

      //save
View Full Code Here

 
  @Test
  public void testAddtable() {
    try {
      //TextDocument doc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("headerFooterHidden.odt"));
      TextDocument doc = TextDocument.newTextDocument();
      Footer footer = doc.getFooter();
      Table tab = footer.addTable();
      Assert.assertNotNull(tab);
      Assert.assertTrue(2 == tab.getRowCount());
      Assert.assertTrue(5 == tab.getColumnCount());
     
View Full Code Here

 
  @Test
  public void testGetTableList() {
    try {
      //TextDocument doc = TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("headerFooterHidden.odt"));
      TextDocument doc = TextDocument.newTextDocument();
      Footer footer = doc.getFooter();
      Table tab = footer.addTable();
     
      List<Table> tabList = footer.getTableList();
      Assert.assertEquals(tab, tabList.get(0));
     
View Full Code Here

TOP

Related Classes of org.odftoolkit.simple.TextDocument

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.