How does Java insert a page break at a specified location in Word?
This article mainly introduces the knowledge of "Java how to insert page breaks in the specified location of Word". The editor shows you the process of operation through an actual case. The method of operation is simple and fast, and it is practical. I hope that this article "how to achieve Java inserting page breaks in the specified location of Word" can help you solve the problem.
Program environment
There are two ways to import jar into a program:
Method 1: introduce it manually. Download Free Spire.Doc for Java locally, extract it, and find the Spire.Doc.jar file in the lib folder. Open the following interface in IDEA and introduce the jar file in the local path into the Java program:
Method 2 (recommended): download from the Maven repository. Configure pom.xml as follows:
Com.e-iceblue https://repo.e-iceblue.cn/repository/maven-public/ e-iceblue spire.doc.free 5. 2. 0 inserts page break 1. Insert a page break behind the specified paragraph
Java
Import com.spire.doc.Document;import com.spire.doc.FileFormat;import com.spire.doc.Section;import com.spire.doc.documents.BreakType;import com.spire.doc.documents.Paragraph;public class PageBreak1 {public static void main (String [] args) {/ / create the object of the Document class Document document = new Document (); / / load the Word document document.loadFromFile ("test.docx") / / get the first section Section section = document.getSections () .get (0); / / get the third paragraph in the first section Paragraph paragraph = section.getParagraphs () .get (2); / / add a page break paragraph.appendBreak (BreakType.Page_Break); / / Save the document document.saveToFile ("output.docx", FileFormat.Docx_2013);}}
Effect picture
two。 Inserts a page break after the specified text location
Java
Import com.spire.doc.Break;import com.spire.doc.Document;import com.spire.doc.FileFormat;import com.spire.doc.documents.BreakType;import com.spire.doc.documents.Paragraph;import com.spire.doc.documents.TextSelection;import com.spire.doc.fields.TextRange;public class PageBreak2 {public static void main (String [] args) {/ / create an instance of the Document class Document document = new Document () / / load the Word document document.loadFromFile ("test.docx"); / / find the specified text TextSelection selection = document.findString ("ASEAN Community" is declared established. " , true, true); / / get the text range found TextRange range = selection.getAsOneRange (); / / get the paragraph where the text range is located Paragraph paragraph = range.getOwnerParagraph (); / / get the position index of the text range in the paragraph int index = paragraph.getChildObjects (). IndexOf (range); / / create paging Break pageBreak = new Break (document, BreakType.Page_Break) / / insert the page break paragraph.getChildObjects () .insert (index + 1, pageBreak) after the found text location; / / Save the document document.saveToFile ("InsertPageBreakAfterText.docx", FileFormat.Docx_2013);}}
Effect picture
This is the end of the content about "how to insert page breaks in the specified location of Word by Java". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.