Use Docx4j PPT Guide Series 1)
-- Generate and add a new page for the pptx document
Preface
Because the project requires the system to generate ppt documents, docx4j is selected in open source technology. Docx4j is even more rare when there is almost no Chinese documentation for ppt operations.) All Things can only be explored by yourself. Therefore, this series of documents can be considered as a reference, due to project use cases, this series only describes some of the functions used in the project. I want to cover almost all the basic functions. You are welcome to communicate with me and make common progress.
Preparations
Download docx4j, project URL: http://www.docx4java.org/trac/docx4j
:
- SVN trunk: http://www.docx4java.org/svn/docx4j/trunk/
- 2.7.1 release: http://www.docx4java.org/svn/docx4j/tags/VERSION_2_7_1/
Maven Configuration:
<dependency> <groupId>org.docx4j</groupId> <artifactId>docx4j</artifactId> <version>2.7.1</version> </dependency> |
Note: docx4j is mainly used to operate the Office2007 version.
Create ppt document
|
Public class CreatePPTX
{
Public static void main (String [] args)
Throws Exception
{
String outputfilepath = "c: // pptx-test.pptx ";
// Create the document subject
PresentationMLPackage presentationMLPackage = PresentationMLPackage. createPackage ();
// Obtain MainPresentationPart, which is actually used
// PresentationMLPackage. getMainPresentationPart (); The method can also be
MainPresentationPart pp = (MainPresentationPart) presentationMLPackage. getParts (). getParts (). get (new PartName ("/ppt/presentation. xml "));
// Obtain the layout information, which is actually used to obtain the layout page of the slide master. This function is very important.
// You must apply a layout when creating a new page.
// "/Ppt/slideLayouts/slideLayout1.xml" indicates the id of the master page, depending on the layout of the master page.
// Bureau style, which is arranged in order of 1, 2, 3, and 4
SlideLayoutPart layoutPart = (SlideLayoutPart) presentationMLPackage. getParts (). getParts (). get (new PartName ("/ppt/slideLayouts/slideLayout1.xml "));
// Create a new page with layout information. Otherwise, an error is reported when you open the ppt,
// Automatically specify a default layout style
// "/Ppt/slides/slide1.xml" The partName of each page cannot be repeated.
SlidePart slidePart = PresentationMLPackage. createSlidePart (pp, layoutPart, new PartName ("/ppt/slides/slide1.xml "));
// Save as an object
PresentationMLPackage. save (new File (outputfilepath ));
System. out. println ("\ n done .. saved" + outputfilepath );
}
} |
Personally, it is recommended that you do not directly create new ppts. Generally, there are several specific ppt templates for enterprises. Using these templates can save a lot of time, in addition, the layout of the cover page and master page is customized and can be used only.
The unique code is different. It is loaded from a file, as shown below:
PresentationMLPackage presentationMLPackage = PresentationMLPackage. load (new File (
PSMSConstants. REPORT_PPT2007_TEMPLATE_PATH )); |
OK. In this section, you should master how to use docx4j to create pptx files and add content pages. In the next section, this section describes how to add text and graphics to the content page.
This article is from the "universe in the shell" blog, please be sure to keep this source http://williamx.blog.51cto.com/3629295/758955