Java dynamically generates word documents (with pictures and texts)

In many cases, software developers need to read data from the database, and then dynamically fill the data into manually pre-prepared Word template documents, which is very useful for mass-producing formal documents with the same format and layout. Basic dynamic fill function can be achieved. However, if the user wants to dynamically generate an official document without a fixed template, in other words, when there is no way to prepare a template with a fixed format in advance, the developer needs to use the code in the background to realize the dynamic generation function of the Word document from zero to full of pictures and texts. . The "zero" here refers to a blank Word document.

  Then how to realize Word document from scratch, let's introduce the process of using PageOffice to realize this function. For example, if you want to generate a Word document, the content in it is: title (bold, bold, font size 20, centered), the first paragraph of content (content (omitted), font slanted, font size 10, Chinese "Kaiti" ", English "Times New Roman", red, minimum line spacing, left alignment, first line indent), the second paragraph of content (content (omitted), font size 12, bold, 1.5 double line spacing, left alignment, first line Line indentation), the third paragraph of content (content (omitted), font size 14, Chinese colorful clouds, double line spacing, left alignment, first line indentation), and the fourth paragraph of content to insert a picture.

  1. Go to PageOffice on Baidu and download the development kit of PageOffice for Java from the official website;

  2. Copy the lib folder in the "integration files" folder to the WEB-INF directory of your own web project;

  3. Open the web.xml file in the "Integration Files" folder, and copy the relevant configuration of PageOffice to the web.xml file in your own web project;

  4. Programmatically call the WordDocument object of PageOffice to generate a document with pictures and texts. code show as below:

copy code

PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
    poCtrl1.setServerPage(request.getContextPath()+"/poserver.zz");
    //Create a WordDocument object
    WordDocument doc = new WordDocument();
    //set content title
    //Create a DataRegion object, PO_title is the automatically added bookmark name, the bookmark name needs to be prefixed with "PO_", and the bookmark name cannot be repeated
    //The three parameters are the name of the new bookmark to be inserted, the insertion position of the new bookmark, and the associated bookmark name ("[home]" represents the first position of the Word document)
    DataRegion title = doc.createDataRegion("PO_title",
            DataRegionInsertType.After, "[home]");
    //Assign a value to the DataRegion object
    title.setValue("Socket multi-threaded programming example in C#\n");
    //Set the font: weight, size, font name, whether it is italic
    title.getFont().setBold(true);
    title.getFont().setSize(20);
    title.getFont().setName("bold");
    title.getFont().setItalic(false);
    //define the paragraph object
    ParagraphFormat titlePara = title.getParagraphFormat();
    //Set paragraph alignment
    titlePara.setAlignment(WdParagraphAlignment.wdAlignParagraphCenter);
    //Set the paragraph line spacing
    titlePara.setLineSpacingRule(WdLineSpacing.wdLineSpaceMultiple);

    //set content
    //first paragraph
    //Create a DataRegion object, PO_body is the automatically added bookmark name
    DataRegion body = doc.createDataRegion("PO_body",DataRegionInsertType.After, "PO_title");
    //Set the font: thickness, italic, size, font name, font color
    body.getFont().setBold(false);
    body.getFont().setItalic(true);
    body.getFont().setSize(10);
    //Set the Chinese font name
    body.getFont().setName("Italics");
    //Set the English font name
    body.getFont().setName("Times New Roman");
    body.getFont().setColor(Color.RED);
    //Assign a value to the DataRegion object
    body.setValue(" is a new language introduced by Microsoft along with VS.net. As an emerging language, it has the robustness of C++ and the RAD features of VB and so on. Moreover, the main purpose of Microsoft's introduction of C# is to Against Sun's Java. Everyone knows the powerful functions of the Java language, especially in network programming. Therefore, C# is naturally unwilling to lag behind in network programming. This article will introduce you to the implementation of sockets (Sockets) under C#. Some basic knowledge of programming, in order to give you a general understanding of this. First, let me introduce you to the concept of sockets.\n");
    //Create ParagraphFormat object
    ParagraphFormat bodyPara = body.getParagraphFormat();
    //Set the line spacing, alignment, and first line indentation of the paragraph
    bodyPara.setLineSpacingRule(WdLineSpacing.wdLineSpaceAtLeast);
    bodyPara.setAlignment(WdParagraphAlignment.wdAlignParagraphLeft);
    bodyPara.setFirstLineIndent(21);

    //Second paragraph
    DataRegion body2 = doc.createDataRegion("PO_body2",DataRegionInsertType.After, "PO_body");
    body2.getFont().setBold(false);
    body2.getFont().setSize(12);
    body2.getFont().setName("Black Body");
    body2.setValue("The socket is the cornerstone of communication and the basic operation unit of network communication that supports the TCP/IP protocol. The socket can be regarded as the endpoint for two-way communication between processes between different hosts, which constitutes a single host The programming interface within and between the entire network. Sockets exist in the communication domain, which is an abstract concept introduced to deal with general thread communication through sockets. Sockets are usually associated with sockets in the same domain. Sockets exchange data (data exchanges may also cross domain boundaries, but in this case some kind of interpreter must be executed). Various processes use this same domain to communicate with each other using the Internet Protocol Suite.\n");
    //body2.setValue("[image]../images/logo.jpg[/image]");
    ParagraphFormat bodyPara2 = body2.getParagraphFormat();
    bodyPara2.setLineSpacingRule(WdLineSpacing.wdLineSpace1pt5);
    bodyPara2.setAlignment(WdParagraphAlignment.wdAlignParagraphLeft);
    bodyPara2.setFirstLineIndent(21);

    // third paragraph
    DataRegion body3 = doc.createDataRegion("PO_body3", DataRegionInsertType.After, "PO_body2");
    body3.getFont().setBold(false);
    body3.getFont().setColor(Color.getHSBColor(0, 128, 228));
    body3.getFont().setSize(14);
    body3.getFont().setName("Chinese Color Cloud");
    body3.setValue("Sockets can be classified according to the nature of communication, which is visible to users. Applications generally only communicate between sockets of the same class. However, as long as the underlying communication protocol allows, different types of sockets Communication between sockets is also possible. There are two different types of sockets: stream sockets and datagram sockets.\n");
    ParagraphFormat bodyPara3 = body3.getParagraphFormat();
    bodyPara3.setLineSpacingRule(WdLineSpacing.wdLineSpaceDouble);
    bodyPara3.setAlignment(WdParagraphAlignment.wdAlignParagraphLeft);
    bodyPara3.setFirstLineIndent(21);

        //The fourth paragraph, insert the picture
    DataRegion body4 = doc.createDataRegion("PO_body4", DataRegionInsertType.After, "PO_body3");
    body4.setValue("[image]doc/logo.png[/image]");
    //body4.setValue("[word]doc/1.doc[/word]");//You can also embed other Word files
    ParagraphFormat bodyPara4 = body4.getParagraphFormat();
    bodyPara4.setAlignment(WdParagraphAlignment.wdAlignParagraphCenter);

    poCtrl1.setWriter(doc);
    poCtrl1.webOpen("doc/template.doc", OpenModeType.docNormalEdit,"张三");

copy code

  5. The effect of the generated file is as follows:

  

  6. Related examples: Copy the Samples4 folder in the PageOffice development kit to the Webapps directory of Tomcat, visit: http://localhost:8080/Samples4/index.html, and view the examples: 3. 7. Complete programming to dynamically generate Word document

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325609970&siteId=291194637