java inserts pictures into word files online and sets the size of pictures

I. Preface:

Sometimes we want to add a picture on the server at a specified location in a word document and set the size of the picture. This operation of the document must require a third-party api. There are POI and openoffice plug-ins for document processing on the market. The cost of learning is very high. The key is that the operating functions of documents are very limited through these plug-ins.

So today we will introduce a middleware technology - page office. This middleware technology has integrated most of the functions of operating office documents in the web system. We only need to pay attention to the business logic of our web system. The learning cost is low and the functions are rich. Now let's integrate page office in practice.


2. Core Code

Code for inserting pictures

DataRegion dataRegion = worddoc.openDataRegion("PO_beginning Book signature name");
dataRegion.setValue("[image]doc/1.jpg[/image]");

Insert the image and set the image size code

data1.setValue("[image width=500.2 height=200]doc/1.jpg[/image]");

Note: 500.2 units are pounds.

Specific implementation process

1. Official website http://www.zhuozhengsoft.com/dowm/ Download integration files, introduce jar packages, configure web.xml

2. Place an a tag or button on the parent page index. JSP (page that needs to open the document)

Introduce the js file required by pageoffice before writing a tag (the path of the js file is in the root directory of the project)

<script type="text/javascript" src="/jquery.min.js"></script>
<script type="text/javascript" src="/pageoffice.js" id="po_js_main"></script>

Then add a tag

<a href="javascript:POBrowser.openWindowModeless('Word.jsp' , 'width=1200px;height=800px;');">Open the file</a>

3. Create a file named Word.jsp in the parent page's sibling directory

<%@ page language="java"
	import="java.util.*,com.zhuozhengsoft.pageoffice.*,com.zhuozhengsoft.pageoffice.wordwriter.*"
	pageEncoding="utf-8"%>
<%
	//*****************************************************************************************************************************************************
	PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
	poCtrl1.setServerPage(request.getContextPath()+"/poserver.zz"); //This trip must be

	WordDocument worddoc = new WordDocument();

	//To insert a bookmark manually at the place where the word file is to be inserted, the bookmark must be prefixed with "PO_"
	//Data Region is assigned in the form of: "[word]word file path [/word], [excel]excel file path [/excel], [image] picture path [/image]"
	DataRegion data1 = worddoc.openDataRegion("PO_p1");
	data1.setValue("[image width=100  height=100 ]doc/1.jpg[/image]");


	poCtrl1.setWriter(worddoc);
	poCtrl1.setCaption("Demonstration: Backstage programming inserts pictures into data area and sets picture size(Enterprise Edition)");

	

	poCtrl1.webOpen("doc/test.doc", OpenModeType.docNormalEdit, "Zhang San");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Demo: Background Programming Insert Pictures into Data Area(Professional Edition and Enterprise Edition)</title>

</head>
<body>
  
    <form id="form1" style="height: 100%;">
    <div style="height: 700px; width: auto;">
        <!--**************   PageOffice Client Code Start    ************************-->
        	        <%=poCtrl1.getHtmlCode("PageOfficeCtrl1")%>
        <!--**************   PageOffice Client Code End    ************************-->
    </div>
    </form>
</body>
</html>

Final effect

(1) When the size of the picture is set to 100 in width and height

(2) When the size, width and height of the picture are 500

Start project direct access. Installation plug-in will be prompted at this time, click on the successful installation prompt registration, fill in the relevant information, fill in the registration code I7TGD-71VV-FYD8-4NMYP, you can open the document.

Note that only the Enterprise Edition can support the function of setting image size.

If you are new to page office, you can also watch videos and get started quickly. http://www.zhuozhengsoft.com/Technical/

Tags: Programming JSP Javascript Excel Java

Posted on Thu, 29 Aug 2019 03:53:55 -0700 by KashKurcura