Spire.Doc for Java 3.4.10现已发布。该版本主要修复了一些在加载、操作和保存Word文档时出现的问题。详情如下。
问题修复:
本文将介绍如何使用Spire.Doc for Java更改Word文档中段落文本的字体颜色。
原Word文档:

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class ChangeFontColor {
public static void main(String[] args){
//创建Document实例
Document doc = new Document();
//加载Word文档
doc.loadFromFile("FontColorExample.docx");
//获取第一个节
Section section = doc.getSections().get(0);
//获取第一节中的第二个段落
Paragraph p1 = section.getParagraphs().get(1);
//遍历第二个段落中的所有对象
for (int i = 0; i < p1.getChildObjects().getCount(); i ++)
{
//更改第二个段落中文本的字体颜色
if ( p1.getChildObjects().get(i) instanceof TextRange)
{
TextRange tr = (TextRange) p1.getChildObjects().get(i);
tr.getCharacterFormat().setTextColor(Color.green);
}
}
//获取第三个段落
Paragraph p2 = section.getParagraphs().get(2);
//遍历第三个段落中的所有对象
for (int j = 0; j < p2.getChildObjects().getCount(); j ++)
{
//更改第三个段落中文本的字体颜色
if ( p2.getChildObjects().get(j) instanceof TextRange)
{
TextRange tr = (TextRange) p2.getChildObjects().get(j);
tr.getCharacterFormat().setTextColor(Color.blue);
}
}
//保存文档
doc.saveToFile("ChangeFontColor.docx", FileFormat.Docx_2013);
}
}
结果文档:

今天,我们发布了Spire.Doc 8.4.10,该版本支持获取表格中对角线的属性,并修复了转换和处理Word文档出现的一些问题。具体更新如下。
新功能:
//获取表格的对角线属性
BorderStyle bs_UP = table.TableFormat.Borders.DiagonalUp.BorderType;
Color color_UP = table.TableFormat.Borders.DiagonalUp.Color;
float width_UP = table.TableFormat.Borders.DiagonalUp.LineWidth;
BorderStyle bs_Down = table.TableFormat.Borders.DiagonalDown.BorderType;
Color color_Down = table.TableFormat.Borders.DiagonalDown.Color;
float width_Down = table.TableFormat.Borders.DiagonalDown.LineWidth;
//获取表格单元格的对角线属性
bs_UP = table[rowIndex, columnIndex].CellFormat.Borders.DiagonalUp.BorderType;
width_UP = table[rowIndex, columnIndex].CellFormat.Borders.DiagonalUp.LineWidth;
color_UP = table[rowIndex, columnIndex].CellFormat.Borders.DiagonalUp.Color;
bs_Down = table[rowIndex, columnIndex].CellFormat.Borders.DiagonalDown.BorderType;
color_Down = table[rowIndex, columnIndex].CellFormat.Borders.DiagonalDown.Color;
width_Down = table[rowIndex, columnIndex].CellFormat.Borders.DiagonalDown.LineWidth;
问题修复:
Spire.Office for Java 3.4.0已正式发布,该版本新增加了一些功能,例如Spire.PDF支持在签名时自定义图片/名称/信息的宽度,支持给PdfAnnotation设置作者/主题/创建日期/修改日期属性,支持创建PDF启动操作,支持提取高亮文本和颜色,支持嵌入3D文件和声音文件;Spire.Presentation支持转换一个GroupShape到图片,支持设置/获取幻灯片标题,支持设置动画播放重复类型;Spire.Doc支持设置对角线表格边框,支持设置装订线;Spire.XLS支持拷贝单元格样式,支持移除DataValidation。此外,本次更新还修复了在加载、转换和操作PDF、PowerPoint、Word、Excel文档时出现的一些问题。新功能及问题修复详情如下。
获取Spire.Office for Java 3.4.0,请点击:https://www.e-iceblue.cn/Downloads/Spire-Office-JAVA.html
优化:
新功能:
signature.setCustomSignPosition(true).
signature.setCustomSignImagePosition(x,y,width,height).
signature.setCustomSignNamePosition(x,y,width,height)
signature.setCustomSignDetailPosition(x,y,width,height).
setAuthor(String value);
setSubject(String value);
setCreationDate(Date value);
setModifiedDate(Date value);
setExpandBookmark(boolean value)
PdfRadioButtonListFieldWidget radio = (PdfRadioButtonListFieldWidget)field;
//获取按钮样式
PdfCheckBoxStyle buttonStyle = radio.getButtonStyle();
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.getPages().add();
//Create a PDF Launch Action
PdfLaunchAction launchAction = new PdfLaunchAction(inputFile);
//Create a PDF Action Annotation with the PDF Launch Action
String text = "Click here to open file";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial",0, 13));
Rectangle2D rect = new Rectangle2D.Float(50, 50, 230, 15);
page.getCanvas().drawString(text, font, PdfBrushes.getForestGreen(), rect);
PdfActionAnnotation annotation = new PdfActionAnnotation(rect, launchAction);
//Add the PDF Action Annotation to page
page.getAnnotationsWidget().add(annotation);
//Save the document
doc.saveToFile(outputFile);
PdfDocument doc = new PdfDocument();
//Load a pdf file
doc.loadFromFile(inputFile);
FileWriter fw=new FileWriter(outputFile,true);
BufferedWriter bw=new BufferedWriter(fw);
bw.write("Extracted hightlighted text:"+"\n");
PdfPageBase page = doc.getPages().get(0);
for (int i = 0; i < page.getAnnotationsWidget().getCount(); i++)
{
if (page.getAnnotationsWidget().get(i) instanceof PdfTextMarkupAnnotationWidget)
{
PdfTextMarkupAnnotationWidget textMarkupAnnotation = (PdfTextMarkupAnnotationWidget)page.getAnnotationsWidget().get(i);
//Get the highlighted text
bw.write(page.extractText(textMarkupAnnotation.getBounds()));
//Get the highlighted color
PdfRGBColor color = textMarkupAnnotation.getTextMarkupColor();
bw.write("Color="+(color.getR() & 0XFF) +","+(color.getG() & 0XFF)+","+(color.getB() & 0XFF)+"\n");
}
}
PdfDocument pdf = new PdfDocument(folder + "testTOC.pdf");
//Get the first page
PdfPageBase page = pdf.getPages().get(0);
//Get the annotation collection
PdfDocumentLinkAnnotationWidget link;
PdfDestination destination;
PdfAnnotationCollection annotations = page.getAnnotationsWidget();
for (int i = 0; i < annotations.getCount(); i++)
{
if (annotations.get(i) instanceof PdfDocumentLinkAnnotationWidget)
{
link = (PdfDocumentLinkAnnotationWidget)annotations.get(i);
destination =link.getDestination();
int pageIndex = destination.getPageNumber();
System.out.println("page Index of "+link.getName()+":"+pageIndex);
Point2D location = destination.getLocation();
System.out.println("location: "+location.getX()+","+location.getY());
}
}
//Create a pen
PdfPen pen = new PdfPen(new PdfRGBColor(Color.red), 3f);
//Set dash style
pen.setDashStyle(PdfDashStyle.Dash);
String inputFile = "data/CreatePdf3DAnnotation.u3d";
String outputFile = "output/CreatePdf3DAnnotation.pdf";
//Create a Pdf document.
PdfDocument pdf = new PdfDocument();
//Add a new page.
PdfPageBase page = pdf.getPages().add();
//Draw a rectangle on the page to define the canvas area for the 3D file.
Rectangle rt = new Rectangle(0, 80, 200, 200);
//Initialize a new object of Pdf3DAnnotation, load the .u3d file as 3D annotation.
Pdf3DAnnotation annotation = new Pdf3DAnnotation(rt, inputFile);
annotation.setActivation(new Pdf3DActivation());
annotation.getActivation().setActivationMode(Pdf3DActivationMode.Page_Open);
Pdf3DView View = new Pdf3DView();
View.setBackground(new Pdf3DBackground(new PdfRGBColor(128,0,128)));
View.setViewNodeName("3DAnnotation");
View.setRenderMode(new Pdf3DRendermode(Pdf3DRenderStyle.Solid));
View.setInternalName("3DAnnotation");
View.setLightingScheme(new Pdf3DLighting());
View.getLightingScheme().setStyle(Pdf3DLightingStyle.Day);
//Set the 3D view mode for the annotation.
annotation.getViews().add(View);
//Add the annotation to Pdf.
page.getAnnotationsWidget().add(annotation);
//Save the document
pdf.saveToFile(outputFile);
String inputFile = "data/EmbedSoundFile.pdf";
String inputFile_1 = "data/Music.wav";
String outputFile = "output/EmbedSoundFile.pdf";
//create a pdf document
PdfDocument doc = new PdfDocument();
//load file from disk
doc.loadFromFile(inputFile);
//get the first page
PdfPageBase page = doc.getPages().get(0);
//create a sound action
PdfSoundAction soundAction = new PdfSoundAction(inputFile_1);
soundAction.getSound().setBits(16);//8时为杂音
soundAction.getSound().setChannels(PdfSoundChannels.Stereo);
soundAction.getSound().setEncoding(PdfSoundEncoding.Signed);
soundAction.setVolume(0.8f);
soundAction.setRepeat(true);
//set the sound action to be executed when the PDF document is opened
doc.setAfterOpenAction(soundAction);
//save the document
doc.saveToFile(outputFile);
PdfPageBase page = pdf.getPages().get(0);
page.SetTabOrder(TabOrder.Structure);
问题修复:
新功能:
Presentation ppt = new Presentation();
ppt.loadFromFile(input);
for (int i = 0; i < ppt.getSlides().get(0).getShapes().getCount(); i++){
String fileName = outputPath + "shapeToImage_"+i+".png";
BufferedImage image = ppt.getSlides().get(0).getShapes().saveAsImage(i);
ImageIO.write(image, "PNG", new File(fileName));
}
ISlide slide1 = ppt.getSlides().get(0);
slide1.getTitle();
slide1.setTitle("new title");
TextHighLightingOptions options = new TextHighLightingOptions();
options.setWholeWordsOnly(true);
options.setCaseSensitive(true);
shape.getTextFrame().HighLightText("Spire", Color.yellow, options);
AnimationEffectCollection animations = slide.getTimeline().getMainSequence(); animations.get(0).getTiming().setAnimationRepeatType(AnimationRepeatType.UtilEndOfSlide);
Presentation ppt = new Presentation();
ppt.loadFromFile(input);
for (int i = 0; i < ppt.getSlides().get(0).getShapes().getCount(); i++){
String fileName = outputPath + "shapeToImage_demo"+i+".png";
BufferedImage image = ppt.getSlides().get(0).getShapes().saveAsImage(i,300,300);
ImageIO.write(image, "PNG", new File(fileName));
}
Presentation ppt = new Presentation();
ppt.loadFromFile(inputFile);
IAutoShape shape = (IAutoShape)ppt.getSlides().get(0).getShapes().get(0);
Point2D location =shape.getTextFrame().getTextLocation();
String concent = "text在第一张幻灯片中Shape中的坐标: x= " + (location.getX() - shape.getLeft()) + " y= " + (location.getY() - shape.getTop());
System.out.println(concent);
问题修复:
新功能:
Document doc = new Document();
Section section = doc.addSection();
Table oTable = section.addTable(true);
section.addParagraph();
oTable.resetCells(10, 3);
oTable.getTableFormat().getPaddings().setTop(10);
oTable.getTableFormat().getPaddings().setBottom(10);
oTable.getTableFormat().setHorizontalAlignment(RowAlignment.Center); oTable.getLastCell().getCellFormat().getBorders().getDiagonalUp().setBorderType(BorderStyle.Single); oTable.getFirstRow().getCells().get(0).getCellFormat().getBorders().getDiagonalDown().setBorderType(BorderStyle.Double);
doc.saveToFile(outputFile);
Section section = document.getSections().get(0);
section.getPageSetup().setGutter(100f);
table.get(0,0).getCellFormat().getBorders().getDiagonalUp().setBorderType(BorderStyle.Single);
table.get(0,0).getCellFormat().getBorders().getDiagonalUp().setColor(Color.blue);
table.get(0,0).getCellFormat().getBorders().getDiagonalUp().setLineWidth(0.5f); table.get(0,0).getCellFormat().getBorders().getDiagonalDown().setBorderType(BorderStyle.Single);
table.get(0,0).getCellFormat().getBorders().getDiagonalDown().setColor(Color.blue);
table.get(0,0).getCellFormat().getBorders().getDiagonalDown().setLineWidth(0.5f);
问题修复:
新功能:
srcRange.copy(destRange,true,true);
公布GroupByTape 和ExpandCollapseFlags枚举类型。
worksheet.getRange().get("A1:C1").expandGroup(GroupByType.ByRows,ExpandCollapseFlags.ExpandParent);
worksheet.isDisplayZeros(false);
workbook.getWorksheets().get(0).getDVTable().remove(rectangles);
pageSetup.setPrintErrors(PrintErrorsType.NA);
//添加单选按钮
worksheet.getRadioButtons().add(5,5,20,20);
//拷贝线条
worksheet.getTypedLines().addCopy(line);
//拷贝单选按钮
worksheet.getTypedRadioButtons().addCopy(button);
//拷贝组合框
worksheet.getTypedComboBoxes().addCopy(combobox);
worksheet.saveToFile("CsvFilePath", ";", false);
问题修复:
Spire.Presentation for Java 3.4.3已正式发布。该版本新增了支持转换形状到图片时设置分辨率、获取文本在形状中的位置的功能;同时也修复了转换PPT到PDF/Word、添加HMTL时出现的一些问题。新功能及问题修复详情如下。
新功能:
Presentation ppt = new Presentation();
ppt.loadFromFile(input);
for (int i = 0; i < ppt.getSlides().get(0).getShapes().getCount(); i++){
String fileName = outputPath + "shapeToImage_demo"+i+".png";
BufferedImage image = ppt.getSlides().get(0).getShapes().saveAsImage(i,300,300);
ImageIO.write(image, "PNG", new File(fileName));
}
Presentation ppt = new Presentation();
ppt.loadFromFile(inputFile);
IAutoShape shape = (IAutoShape)ppt.getSlides().get(0).getShapes().get(0);
Point2D location =shape.getTextFrame().getTextLocation();
String concent = "text在第一张幻灯片中Shape中的坐标: x= " + (location.getX() - shape.getLeft()) + " y= " + (location.getY() - shape.getTop());
System.out.println(concent);
问题修复:
https://www.e-iceblue.cn/Downloads/Spire-Presentation-JAVA.html
文本介绍如何使用Spire.XLS for Java为Excel的第一页和其它页设置不同的页眉页脚。
import com.spire.xls.FileFormat;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class SetDifferentHeaderFooter {
public static void main(String[] args) {
//创建Workbook对象
Workbook workbook = new Workbook();
//获取第一个工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//在A1、J1写入文本
sheet.getCellRange("A1").setText("第一页");
sheet.getCellRange("J1").setText("第二页");
//设置不同的第一页页眉页脚
sheet.getPageSetup().setDifferentFirst((byte)1);
//为第一页设置页眉页脚
sheet.getPageSetup().setFirstHeaderString("&\"Arial\"&B&14&KFF0000第一页页眉");
sheet.getPageSetup().setFirstFooterString("&\"Arial\"&B&14&KFF0000第一页页脚");
//为其它页设置页眉页脚
sheet.getPageSetup().setCenterHeader("&\"Arial\"&B&14&K191970其它页页眉");
sheet.getPageSetup().setCenterFooter("&\"Arial\"&B&14&K191970其它页页脚");
//保存文档
workbook.saveToFile("DifferentFirstPage.xlsx", FileFormat.Version2016);
}
}

Spire.XLS for Java支持为Excel设置三色及四色交通灯。该文将介绍如何在Excel工作表中设置三色交通灯。
import com.spire.xls.*;
import com.spire.xls.core.IConditionalFormat;
import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats;
import java.awt.*;
public class setTrafficLightsIcons {
public static void main(String[] args) {
//新建实例
Workbook workbook = new Workbook();
//添加工作表(默认3个)
Worksheet sheet = workbook.getWorksheets().get(0);
//添加数据并设置样式
sheet.getCellRange("A1").setText("Traffic Lights");
sheet.getCellRange("A2").setNumberValue(0.95);
sheet.getCellRange("A2").setNumberFormat("0%");
sheet.getCellRange("A3").setNumberValue(0.5);
sheet.getCellRange("A3").setNumberFormat("0%");
sheet.getCellRange("A4").setNumberValue(0.1);
sheet.getCellRange("A4").setNumberFormat("0%");
sheet.getCellRange("A5").setNumberValue(0.9);
sheet.getCellRange("A5").setNumberFormat("0%");
sheet.getCellRange("A6").setNumberValue(0.7);
sheet.getCellRange("A6").setNumberFormat("0%");
sheet.getCellRange("A7").setNumberValue(0.6);
sheet.getCellRange("A7").setNumberFormat("0%");
//设置高度和宽度
sheet.getAllocatedRange().setRowHeight(20);
sheet.getAllocatedRange().setColumnWidth(25);
//添加条件格式
XlsConditionalFormats conditional = sheet.getConditionalFormats().add();
conditional.addRange(sheet.getAllocatedRange());
IConditionalFormat format1 = conditional.addCondition();
//设置背景色
format1.setFormatType(ConditionalFormatType.CellValue);
format1.setFirstFormula("300");
format1.setOperator(ComparisonOperatorType.Less);
format1.setFontColor(Color.black);
format1.setBackColor(Color.lightGray);
//使用条件格式设置交通灯
conditional.addRange(sheet.getAllocatedRange());
IConditionalFormat format = conditional.addCondition();
format.setFormatType(ConditionalFormatType.IconSet);
format.getIconSet().setIconSetType(IconSetType.ThreeTrafficLights1);
//保存文档
String result = "output/setTrafficLightsIcons_result.xlsx";
workbook.saveToFile(result, ExcelVersion.Version2013);
}
}
效果图:
![]()
本文介绍使用Spire.Doc for Java来复制Word文档的方法,复制的内容可支持包括文本、图片、表格、超链接、书签、批注、形状、编号列表、脚注、尾注等等在内的多种元素。
两个Word测试文档如下,将左边的文档内容复制到右边的文档:

import com.spire.doc.*;
public class CopyDoc {
public static void main(String[] args) {
//加载文档1
Document doc1 = new Document();
doc1.loadFromFile("input.docx");
//加载文档2
Document doc2 = new Document();
doc2.loadFromFile("target.docx");
//遍历文档1中的所有子对象
for (int i = 0; i < doc1.getSections().getCount(); i++) {
Section section = doc1.getSections().get(i);
for( int j = 0;j< section.getBody().getChildObjects().getCount();j++)
{
Object object = section.getBody().getChildObjects().get(j);
//复制文档1中的正文内容添加到文档2
doc2.getSections().get(0).getBody().getChildObjects().add(((DocumentObject) object).deepClone());
}
}
//保存文档2
doc2.saveToFile("CopyDoc.docx", FileFormat.Docx_2013);
doc2.dispose();
}
}
文档复制结果:

该文将介绍如何使用 Spire.XLS for Java插入图片到Excel图表
import com.spire.xls.*;
import com.spire.xls.core.IPictureShape;
import com.spire.xls.core.IShape;
import java.awt.*;
public class addPictureToChart {
public static void main(String[] args) {
//加载示例文档
Workbook workbook = new Workbook();
workbook.loadFromFile("Sample.xlsx");
//获取第一个工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//获取第一个图表
Chart chart = sheet.getCharts().get(0);
//插入图表并设置样式
IShape picture = chart.getShapes().addPicture("48.png");
((IPictureShape) picture).getLine().setDashStyle(ShapeDashLineStyleType.DashDotDot);
((IPictureShape) picture).getLine().setForeColor(Color.blue);
//保存文档
String result = "output/AddPictureToChart.xlsx";
workbook.saveToFile(result, ExcelVersion.Version2010);
}
}
效果图:

Spire.XLS for Java 3.4.8于今日发布。该版本拥有许多新的功能,例如它支持拷贝单元格格式,支持不显示零,支持设置PrintError为NA等。同时,一些在Excel转HTML/PDF及操作文档时出现的问题也得以成功解决。详情如下。
新功能:
srcRange.copy(destRange,true,true);
worksheet.getRange().get("A1:C1").expandGroup(GroupByType.ByRows,ExpandCollapseFlags.ExpandParent);
worksheet.isDisplayZeros(false);
workbook.getWorksheets().get(0).getDVTable().remove(rectangles);
pageSetup.setPrintErrors(PrintErrorsType.NA);
//添加单选按钮
worksheet.getRadioButtons().add(5,5,20,20);
//拷贝线条
worksheet.getTypedLines().addCopy(line);
//拷贝单选按钮
worksheet.getTypedRadioButtons().addCopy(button);
//拷贝组合框
worksheet.getTypedComboBoxes().addCopy(combobox);
worksheet.saveToFile("CsvFilePath", ";", false);
问题修复: