Вступление
Microsoft Word предоставляет маркеры и цифры, чтобы помочь нам упорядочить список элементов в нашем документе Word в правильном порядке. В этой статье показано, как добавить маркированные и нумерованные списки в документ Word, а затем прочитать и удалить маркированные и нумерованные списки из документа Word в приложении Java.
Библиотека, которая нам нужна
Свободный дух. Документ для Java
Прежде чем использовать приведенный ниже код, нам нужно добавить зависимость к FreeSpire. Документ для библиотеки Java. Для приложения maven мы можем добавить приведенный ниже код в pom.xml для установки FreeSpire. Документ для Java (версия 2.6.2) из репозитория maven:
com.e-iceblue e-iceblue http://repo.e-iceblue.com/nexus/content/groups/public/ e-iceblue spire.doc.free 2.6.2
Мы также можем скачать бесплатно Spire. Документ для Java с официального сайта: https://www.e-iceblue.com/Download/doc-for-java-free.html
Пример кода
Создавайте простые маркированные и нумерованные списки
import com.spire.doc.*; import com.spire.doc.documents.*; public class SimpleList { public static void main(String[] args){ //create a Word document and add a section Document document = new Document(); Section section = document.addSection(); //Add 8 paragraphs Paragraph paragraph = section.addParagraph(); paragraph.appendText("Part 1"); paragraph.applyStyle(BuiltinStyle.Heading_1); paragraph = section.addParagraph(); paragraph.appendText("Chapter 1"); paragraph = section.addParagraph(); paragraph.appendText("Chapter 2"); paragraph = section.addParagraph(); paragraph.appendText("Chapter 3"); paragraph = section.addParagraph(); paragraph.appendText("Part 2"); paragraph.applyStyle(BuiltinStyle.Heading_1); paragraph = section.addParagraph(); paragraph.appendText("Chapter 1"); paragraph = section.addParagraph(); paragraph.appendText("Chapter 2"); paragraph = section.addParagraph(); paragraph.appendText("Chapter 3"); //apply bullets to the 2-4 paragraphs for(int i = 1; i < 4; i++){ Paragraph para = section.getParagraphs().get(i); para.getListFormat().applyBulletStyle(); para.getListFormat().getCurrentListLevel().setNumberPosition(-10); } //apply numbering to the 6-8 paragraphs for(int i = 5; i < 8; i++){ Paragraph para = section.getParagraphs().get(i); para.getListFormat().applyNumberedStyle(); para.getListFormat().getCurrentListLevel().setNumberPosition(-10); } //save the resultant document document.saveToFile("Output.docx", FileFormat.Docx_2013); } }
Выход:
Создание многоуровневых маркированных и нумерованных списков
import com.spire.doc.*; import com.spire.doc.documents.*; public class MultiLevelList { public static void main(String[] args){ //create a Word document and add a section Document document = new Document(); Section section = document.addSection(); //add a title paragraph Paragraph paragraph = section.addParagraph(); paragraph.appendText("Lists"); paragraph.applyStyle(BuiltinStyle.Title); //add a paragraph paragraph = section.addParagraph(); paragraph.appendText("Numbered List:").getCharacterFormat().setBold(true); //create a numbered list ListStyle numberList = new ListStyle(document, ListType.Numbered); numberList.setName("numberList"); numberList.getLevels().get(1).setNumberPrefix("\u0000."); numberList.getLevels().get(1).setPatternType(ListPatternType.Arabic); numberList.getLevels().get(2).setNumberPrefix("\u0000.\u0001"); numberList.getLevels().get(2).setPatternType(ListPatternType.Arabic); //create a bulleted list ListStyle bulletList= new ListStyle(document, ListType.Bulleted); bulletList.setName("bulletList"); //add the numbered and bulleted list into the document document.getListStyles().add(numberList); document.getListStyles().add(bulletList); //add paragraphs and apply the numbered list paragraph = section.addParagraph(); paragraph.appendText("List Item 1"); paragraph.getListFormat().applyStyle(numberList.getName()); paragraph = section.addParagraph(); paragraph.appendText("List Item 2"); paragraph.getListFormat().applyStyle(numberList.getName()); paragraph = section.addParagraph(); paragraph.appendText("List Item 2.1"); paragraph.getListFormat().applyStyle(numberList.getName()); paragraph.getListFormat().setListLevelNumber(1); paragraph = section.addParagraph(); paragraph.appendText("List Item 2.2"); paragraph.getListFormat().applyStyle(numberList.getName()); paragraph.getListFormat().setListLevelNumber(1); paragraph = section.addParagraph(); paragraph.appendText("List Item 2.2.1"); paragraph.getListFormat().applyStyle(numberList.getName()); paragraph.getListFormat().setListLevelNumber(2); paragraph = section.addParagraph(); paragraph.appendText("List Item 2.2.2"); paragraph.getListFormat().applyStyle(numberList.getName()); paragraph.getListFormat().setListLevelNumber(2); paragraph = section.addParagraph(); paragraph.appendText("List Item 2.2.3"); paragraph.getListFormat().applyStyle(numberList.getName()); paragraph.getListFormat().setListLevelNumber(2); paragraph = section.addParagraph(); paragraph.appendText("List Item 2.3"); paragraph.getListFormat().applyStyle(numberList.getName()); paragraph.getListFormat().setListLevelNumber(1); paragraph = section.addParagraph(); paragraph.appendText("List Item 3"); paragraph.getListFormat().applyStyle(numberList.getName()); //add paragraphs and apply the bulleted list paragraph = section.addParagraph(); paragraph.appendText("Bulleted List:").getCharacterFormat().setBold(true); paragraph = section.addParagraph(); paragraph.appendText("List Item 1"); paragraph.getListFormat().applyStyle(bulletList.getName()); paragraph = section.addParagraph(); paragraph.appendText("List Item 2"); paragraph.getListFormat().applyStyle(bulletList.getName()); paragraph = section.addParagraph(); paragraph.appendText("List Item 2.1"); paragraph.getListFormat().applyStyle(bulletList.getName()); paragraph.getListFormat().setListLevelNumber(1); paragraph = section.addParagraph(); paragraph.appendText("List Item 2.2"); paragraph.getListFormat().applyStyle(bulletList.getName()); paragraph.getListFormat().setListLevelNumber(1); paragraph = section.addParagraph(); paragraph.appendText("List Item 3"); paragraph.getListFormat().applyStyle(bulletList.getName()); //save the resultant document document.saveToFile("MultiLevelList.docx", FileFormat.Docx); } }
Выход:
Чтение маркированных и нумерованных списков
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; public class ReadList { public static void main(String[] args){ //load the MultiLevelList.docx document Document document = new Document(); document.loadFromFile("MultiLevelList.docx"); //get the 1st section Section section = document.getSections().get(0); //loop through the paragraphs in the section and read lists for(int i = 0; i < section.getParagraphs().getCount(); i++){ Paragraph paragraph = section.getParagraphs().get(i); String list = paragraph.getListText(); System.out.println(list); } } }
Выход:
Удаление маркированных и нумерованных списков
import com.spire.doc.*; import com.spire.doc.documents.Paragraph; public class RemoveList { public static void main(String[] args){ //load the MultiLevelList.docx document Document document = new Document(); document.loadFromFile("MultiLevelList.docx"); //get the 1st section Section section = document.getSections().get(0); //Loop through the paragraphs in the section and remove lists for(int i = 0; i < section.getParagraphs().getCount(); i++){ Paragraph paragraph = section.getParagraphs().get(i); paragraph.getListFormat().removeList(); } //save the resultant document document.saveToFile("RemoveList.docx", FileFormat.Docx_2013); } }
Выход:
Дополнительная информация:
Главная страница продукта: https://www.e-iceblue.com/Introduce/free-doc-for-java.html Форум: https://www.e-iceblue.com/Introduce/free-doc-for-java.html
Оригинал: “https://dev.to/eiceblue/create-read-and-remove-bulleted-and-numbered-lists-in-word-in-java-16co”