当前位置:网站首页>(transfer) Aspose Documentbuilder I of words Programming Guide
(transfer) Aspose Documentbuilder I of words Programming Guide
2022-04-23 00:10:00 【zyb418】
https://blog.csdn.net/sinat_30276961/article/details/48267741
Last one Aspose.words Programming guide Working with Document It introduces Document Class related operations , contain document Properties of ,document Inherited from Node Some characteristics of , As well as its clone. This article will go on to the operation document A key class of :DocumentBuilder.
The overview
DocumentBuilder It's a device for operating Document A very powerful class . It provides a series of methods , It's convenient for you to insert text 、 The paragraph 、 list 、 form 、 Pictures and other content . Using it is a bit like using java Of StringBuilder.
DOM Of Node What can be done , Use DocumentBuilder It can also be done . And more than using dom How to operate document Less code .
DocumentBuilder A cursor is maintained internally Cursor, It allows you to point anywhere you want to point . We call DocumentBuilder.MoveToXXX This method points to . For example ,DocumentBuilder.MoveToDocumentStart,DocumentBuilder.MoveToField.
MoveToXXX after , You can go through DocumentBuilder.InsertXXX Insert text there , picture , Bookmarks , Fields or other elements . For example , DocumentBuilder.InsertField, DocumentBuilder.InsertHtml.
Insert elements
It follows that , I'll talk about it in detail DocumentBuilder How to insert various types of elements .
Insert string
To insert a string , We can do that by using DocumentBuilder.write. Then the text specification type , It's through Font Class to control .
Let's look at an example :
DocumentBuilder builder = new DocumentBuilder();
// Specify font formatting before adding text.
Font font = builder.getFont();
font.setSize(16);
font.setBold(true);
font.setColor(Color.BLUE);
font.setName("Arial");
font.setUnderline(Underline.DASH);
builder.write("Sample text.");
You can see ,Font It's through builder.getFont(); Acquired .
Insert Paragraph
DocumentBuilder.writeln, To see this , Smart, you must know , This is the ratio write More line breaks . Right , however , The line break here is also seen as paragraph break, seeing the name of a thing one thinks of its function , Designed to end this paragraph Of .
And then call DocumentBuilder.writeln, and write equally , need DocumentBuilder.Font, It can also need DocumentBuilder.ParagraphFormat, Used to set paragraph The format of .
Code instance :
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify font formatting
Font font = builder.getFont();
font.setSize(16);
font.setBold(true);
font.setColor(Color.BLUE);
font.setName("Arial");
font.setUnderline(Underline.DASH);
// Specify paragraph formatting
ParagraphFormat paragraphFormat = builder.getParagraphFormat();
paragraphFormat.setFirstLineIndent(8);
paragraphFormat.setAlignment(ParagraphAlignment.JUSTIFY);
paragraphFormat.setKeepTogether(true);
builder.writeln("A whole paragraph.");
Insert table
use DocumentBuilder Creating a table is a simple thing , It can be roughly divided into 7 Step :
1. Start to create , adopt DocumentBuilder.StartTable.
2. adopt DocumentBuilder.InsertCell Insert a cell . If you need to change the format , Use DocumentBuilder.CellFormat.
3. stay Cell Pass through DocumentBuilder insert text .
4. Repeat step 2 and 3, Insert multi grid content .
5. By calling DocumentBuilder.EndRow To end the current line . if necessary , You can go through DocumentBuilder.RowFormat To specify the format of a line .
6. Repeat step 2 - 5, Insert multiple lines of content .
7. call DocumentBuilder.EndTable To end a table edit .
Here we will explain each step in detail :
StartTable
The first step in creating a table is DocumentBuilder.StartTable. It should be noted that , This can also be called in a cell , This creates an embedded table .
InsertCell
By calling DocumentBuilder.InsertCell Insert a cell in the current row , On the inside , You can write content . Then continue to add cells to the current row , Continue to call DocumentBuilder.InsertCell.
adopt DocumentBuilder.CellFormat To specify the format of the cell .
EndRow
By calling DocumentBuilder.EndRow To end the editing of the current line . If you then call DocumentBuilder.InsertCell, That will start creating cells in a new row .
adopt DocumentBuilder.RowFormat To specify the format of a line .
EndTable
By calling DocumentBuilder.EndTable To end a table . It should be noted that , Before calling this , Want to call first DocumentBuilder.EndRow. The reason is simple , By calling endRow, hold DocumentBuilder Of Cursor Move the cursor out of that line .
Let's take a look at the code example :
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.startTable();
// Insert a cell
builder.insertCell();
// Use fixed column widths.
table.autoFit(AutoFitBehavior.FIXED_COLUMN_WIDTHS);
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
builder.write("This is row 1 cell 1");
// Insert a cell
builder.insertCell();
builder.write("This is row 1 cell 2");
builder.endRow();
// Insert a cell
builder.insertCell();
// Apply new row formatting
builder.getRowFormat().setHeight(100);
builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);
builder.getCellFormat().setOrientation(TextOrientation.UPWARD);
builder.writeln("This is row 2 cell 1");
// Insert a cell
builder.insertCell();
builder.getCellFormat().setOrientation(TextOrientation.DOWNWARD);
builder.writeln("This is row 2 cell 2");
builder.endRow();
builder.endTable();
The running results of the above three code examples , See figure ( I wrote them together ):

Insert break
If you want to make it clear to start a new line 、 A paragraph 、 A column of 、 One Section Or a page , By calling DocumentBuilder.InsertBreak. Separator break There are many types of , adopt BreakType Enumerate classes to distinguish .
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("This is page 1.");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("This is page 2.");
builder.insertBreak(BreakType.PAGE_BREAK);
builder.writeln("This is page 3.");
Insert a picture
We call DocumentBuilder.InsertImage To insert a picture , There are two ways to insert : Embedded and suspended , Pass respectively InsertImage Two overloading methods of .
In most cases , The picture we insert will be displayed as png The form is stored in the document .
The sources of inserted pictures can be divided into the following four types :
1. from file .
2. From an input stream .
3. From cache Image.
4. From a byte array.
then ,DocumentBuilder.InsertImage Will return a Shape type . If you need , You can modify some properties through it .
Someone might ask , What is the difference between an embedded picture and a floating picture ?
Embedded pictures , seeing the name of a thing one thinks of its function , Is to embed a picture inside . It is characterized by , After inserting the picture , The surrounding environment will adjust accordingly .
Suspended image , seeing the name of a thing one thinks of its function , Is the picture suspended above . It is characterized by , You can put it anywhere , It's no big deal to cover other content , It can also be set to not overwrite the form , So this needs to specify the location and type .
Insert embedded picture
The following code shows how to insert an embedded picture :
builder.moveToParagraph(1, 0);
Bitmap bitmap = MyApplication.decodeSampleBitmapResource(
getResources(), R.drawable.a, 200, 300
);
// inline image
builder.insertImage(bitmap,100,150);
The above pictures are compressed first :decodeSampleBitmapResource, If this is not clear , You can check what I wrote earlier Android Bitmap Extensive use does not produce OOM And “ Load large picture resource optimization ”
Here's the picture , It's an embedded picture :

Insert floating picture
The following code shows how to insert a floating picture :
builder.moveToParagraph(1, 0);
Bitmap bitmap = MyApplication.decodeSampleBitmapResource(
getResources(), R.drawable.a, 200, 300
);
// floating image
builder.insertImage(bitmap,
RelativeHorizontalPosition.MARGIN,
100,
RelativeVerticalPosition.MARGIN,
100,
100,
150,
WrapType.NONE);
here ,WrapType Used to specify the suspension type
Suspended image , Here's the picture :

Due to limited space , Let's write about insertion first , Next Aspose.words Programming guide DocumentBuilder Two Will continue to explain DocumentBuilder Insertion .
版权声明
本文为[zyb418]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230009058260.html
边栏推荐
- A ConvNet for the 2020s的总结
- 算法--两数相加 II(Kotlin)
- 两种方法实现mongodb自增id,推荐第二种
- For the pictures in the specified directory, take the picture name (excluding the extension of the picture), separate the file path from the picture name, and separate the file name from the picture e
- be based on. NETCORE development blog project starblog - (3) model design
- 编程2022-02 KTV
- WMS系统与ERP仓储管理的差异
- 教你两分钟做出一个精美好用的404页面
- Shell脚本笔记(5)- 本的条件语句
- (转)Matlab R2014a 64位与Visual Studio2015的mex相关问题
猜你喜欢

Reinstall windows10

What kind of community play do you do?

Nodejs学习笔记

Concurrent reachability analysis (three color marking method)

【ACM】47. Full Permutation II (consider whether the recorded elements under a branch will be repeatedly recorded when recursing at the next level (use the array used to record))

A convnet for the 2020s summary

Implementation method of interface between bar code WMS system and ERP

Detailed explanation of MySQL field types

What are the target keywords?

WMS仓储管理系统解决方案
随机推荐
Compared with the traditional anchor based method, fcos is unique
HyperMesh laminate composite case study notes
The latest MySQL tutorial is easy to understand
绝对定位不使用left,right,top,bottom等属性
For the pictures in the specified directory, take the picture name (excluding the extension of the picture), separate the file path from the picture name, and separate the file name from the picture e
Change the pycharm interface to Chinese and switch between Chinese and English
Literal aggregation
【ACM】90. Subset II (the de duplication problem of the same tree layer first needs to sort the array (use sort))
L1-065 nonsense code (5 points)
What if the website is attacked by DDoS?
Interviewer: how big is the performance difference between ArrayList and LinkedList?
Array de duplication - basic data type
BGP basic configuration
(转)C#最佳工具集合:IDE、分析、自动化工具等
Font adaptation
Learn rust
Daily CISSP certification common mistakes (April 22, 2022)
(转)Matlab R2014a 64位与Visual Studio2015的mex相关问题
基于.NetCore开发博客项目 StarBlog - (3) 模型设计
YASKAWA motor servo software sigmawin + cannot be connected to the servo driver