Являясь мощным и эффективным инструментом обработки слов, Word играет важную роль в нашей повседневной работе. В этой статье будет рассказано, как создать документ Word в приложении Java с помощью FreeSpire. Doc для Java, а затем вставьте в него изображение, а также задайте его стили абзацев.
Установка Способ 1: Скачайте Бесплатный Spire. Doc для Java и распакуйте его. Затем добавьте Spire.Doc.jar файл в ваше Java-приложение в качестве зависимости.
Метод 2: Вы также можете добавить зависимость jar в проект maven, добавив следующие конфигурации в pom.xml .
com.e-iceblue e-iceblue http://repo.e-iceblue.com/nexus/content/groups/public/ e-iceblue spire.doc.free 2.7.3
Создание документа Word:
import com.spire.doc.*; import com.spire.doc.documents.HorizontalAlignment; import com.spire.doc.documents.Paragraph; import com.spire.doc.documents.ParagraphStyle; import com.spire.doc.fields.DocPicture; import java.awt.*; public class CreateWordDocument { public static void main(String[] args){ //Create a Document instance Document document = new Document(); //Add a section Section section = document.addSection(); //Add 4 paragraphs to the section Paragraph para1 = section.addParagraph(); para1.appendText("What Is Insomnia?"); Paragraph para2 = section.addParagraph(); para2.appendText("Insomnia is characterized by an inability to obtain a sufficient amount of sleep to feel rested. "+ "It can be due to either difficulty falling or staying asleep. It may also result in waking earlier than desired. "+ "The sleep is often reported to be of chronically poor quality, light, and unrefreshing. "+ "As a result of this, people with insomnia suffer from daytime symptoms like poor attention, irritability, and reduced energy."); Paragraph para3 = section.addParagraph(); para3.appendText("Everyone has the potential to develop the kind of difficulty sleeping that characterizes insomnia. "+ "This is referred to as a predisposition or threshold. The threshold for developing insomnia will vary for each person."); //Add an image to paragraph 4 Paragraph para4 = section.addParagraph(); DocPicture picture = para4.appendPicture("pic1.png"); //Set image width picture.setWidth(300f); //Set image height picture.setHeight(250f); //Set title style for paragraph 1 ParagraphStyle style1 = new ParagraphStyle(document); style1.setName("titleStyle"); style1.getCharacterFormat().setBold(true); style1.getCharacterFormat().setTextColor(Color.BLUE); style1.getCharacterFormat().setFontName("Arial"); style1.getCharacterFormat().setFontSize(12f); document.getStyles().add(style1); para1.applyStyle("titleStyle"); //Set style for paragraph 2 and 3 ParagraphStyle style2 = new ParagraphStyle(document); style2.setName("paraStyle"); style2.getCharacterFormat().setFontName("Arial"); style2.getCharacterFormat().setFontSize(11f); document.getStyles().add(style2); para2.applyStyle("paraStyle"); para3.applyStyle("paraStyle"); //Set horizontal alignment for paragraph 1 and paragraph 4 para1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); para4.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); //Set first-line indent for paragraph 2 and 3 para2.getFormat().setFirstLineIndent(25f); para3.getFormat().setFirstLineIndent(25f); //Set spaces after paragraph 1, 2 and 3 para1.getFormat().setAfterSpacing(15f); para2.getFormat().setAfterSpacing(10f); para3.getFormat().setAfterSpacing(10f); //Save the document document.saveToFile("Word.docx", FileFormat.Docx); } }
Оригинал: “https://dev.to/jazzzzz/creating-word-document-in-java-application-5452”