该文将详细介绍如何使用Spire.Doc for Java添加Latex 数学公式和 MathMLCode到word 文档。
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.omath.*;
public class Test {
public static void main(String[] args){
//新建Word示例,添加一个section
Document doc = new Document();
Section section = doc.addSection();
//添加段落
Paragraph paragraph = section.addParagraph();
//插入Latex 数学公式
OfficeMath officeMath = new OfficeMath(doc);
paragraph.getItems().add(officeMath);
officeMath.fromLatexMathCode("x^{2}+\\sqrt{{x^{2}+1}}+1");
//添加段落二
Paragraph paragraph2 = section.addParagraph();
//插入MathMLCode数学公式
OfficeMath officeMath2 = new OfficeMath(doc);
paragraph2.getItems().add(officeMath2);
officeMath2.fromMathMLCode("<mml:math xmlns:mml=\"http://www.w3.org/1998/Math/MathML\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\"><mml:msup><mml:mrow><mml:mi>x</mml:mi></mml:mrow><mml:mrow><mml:mn>2</mml:mn></mml:mrow></mml:msup><mml:mo>+</mml:mo><mml:msqrt><mml:msup><mml:mrow><mml:mi>x</mml:mi></mml:mrow><mml:mrow><mml:mn>2</mml:mn></mml:mrow></mml:msup><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:msqrt><mml:mo>+</mml:mo><mml:mn>1</mml:mn></mml:math>");
//保存文档
String result = "output/addMathEquation.docx";
doc.saveToFile(result, FileFormat.Docx_2013);
}
}
效果图:

数字签名是一种应用于数字数据的电子加密身份验证戳记,主要用于确认来自签名者的信息以及文档内容未被篡改。本文,将介绍如何使用 Spire.Doc for Java 以编程方式向 Word 文档添加数字签名。
首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。JAR 文件可以从此链接下载。 如果您使用 Maven,则可以将以下代码添加到项目的 pom.xml 文件中,从而在应用程序中导入 JAR 文件。
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>13.12.2</version>
</dependency>
</dependencies>
向 Word 文档添加数字签名时,可参考如下主要代码步骤:
import com.spire.doc.*;
public class AddSignature {
public static void main(String[] args) {
//加载Word文档
Document doc = new Document();
doc.loadFromFile("https://cdn.e-iceblue.cn/sample.docx");
//添加数字签名,并保存文档
doc.saveToFile("AddSignature.docx",FileFormat.Docx_2013,"Cermia.pfx","123654yes!");
}
}签名添加效果:

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。 获取有效期 30 天的临时许可证。
Spire.Doc for Java 4.5.1已发布。该版本支持验证文档的保护密码,增强了Word到PDF/HTML/SVG的转换功能,同时还修复了加载和合并文档等时出现的问题。详情请阅读以下内容。
新功能:
Document document = new Document();
document.loadFromFile(input);
boolean checkResult = document.checkProtectionPassWord(password);
String input="input.docx";
Document doc = new Document();
doc.loadFromFile(input);
String result = "AddDigitalSignature.docx";
doc.saveToFile(result, FileFormat.Docx, "gary.pfx", "e-iceblue");
问题修复:
本文介绍如何使用Spire.Doc在Word文档中添加超链接链接到文档内的某个书签。
using Spire.Doc;
using Spire.Doc.Documents;
namespace WordCodeExamples
{
class Program
{
static void Main(string[] args)
{
//创建Document对象
Document doc = new Document();
//添加两个节
Section section1 = doc.AddSection();
Section section2 = doc.AddSection();
//在第二节中添加一个段落,并在段落上添加书签“myBookmark”
Paragraph bookmarkParagrapg = section2.AddParagraph();
bookmarkParagrapg.AppendText("这里有一个书签myBookmark");
BookmarkStart start = bookmarkParagrapg.AppendBookmarkStart("myBookmark");
bookmarkParagrapg.Items.Insert(0, start);
bookmarkParagrapg.AppendBookmarkEnd("myBookmark");
//在第一节中添加段落,并添加一个链接到书签的超链接
Paragraph paragraph = section1.AddParagraph();
paragraph.AppendText("链接到书签: ");
paragraph.AppendHyperlink("myBookmark", "跳转至指定书签", HyperlinkType.Bookmark);
//保存文档
doc.SaveToFile("LinkToBookmark.docx", FileFormat.Docx2013);
}
}
}
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace WordCodeExamples
Class Program
Shared Sub Main(ByVal args() As String)
'创建Document对象
Document doc = New Document()
'添加两个节
Dim section1 As Section = doc.AddSection()
Dim section2 As Section = doc.AddSection()
'在第二节中添加一个段落,并在段落上添加书签“myBookmark”
Dim bookmarkParagrapg As Paragraph = section2.AddParagraph()
bookmarkParagrapg.AppendText("这里有一个书签myBookmark")
Dim start As BookmarkStart = bookmarkParagrapg.AppendBookmarkStart("myBookmark")
bookmarkParagrapg.Items.Insert(0, start)
bookmarkParagrapg.AppendBookmarkEnd("myBookmark")
'在第一节中添加段落,并添加一个链接到书签的超链接
Dim paragraph As Paragraph = section1.AddParagraph()
paragraph.AppendText("链接到书签: ")
paragraph.AppendHyperlink("myBookmark", "跳转至指定书签", HyperlinkType.Bookmark)
'保存文档
doc.SaveToFile("LinkToBookmark.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace

本文介绍使用Spire.XLS for Java来获取Excel批注中的图片。添加图片到Excel批注请参考这篇文章中的方法。
用于测试的Excel表格如图:

import com.spire.xls.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ExtractComment {
public static void main(String[] args)throws IOException {
//加载excel文档
Workbook wb = new Workbook();
wb.loadFromFile("sample.xlsx");
//获取工作表
Worksheet sheet = wb.getWorksheets().get(0);
//获取指定单元格中的批注背景图片
BufferedImage image = (BufferedImage) sheet.getRange().get("H2").getComment().getFill().getPicture();
ImageIO.write(image,"png",new File("ExtractedImages.png"));
}
}
批注中的图片获取结果:

该文章将详细介绍如何使用Spire.Doc for Java为word文档设置行间距和字间距。
import com.spire.doc.*;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.fields.TextRange;
import java.awt.*;
import java.io.*;
public class setSpacing {
public static void main(String[] args)throws IOException {
//加载示例文档
Document document= new Document("Sample.docx");
//添加新段落并设置段落文本和字体样式
Paragraph para = new Paragraph(document);
TextRange textRange1 = para.appendText("新加段落,设置行间距和字间距");
textRange1.getCharacterFormat().setTextColor(Color.BLUE);
textRange1.getCharacterFormat().setFontSize(15);
//设置段前段后间距
para.getFormat().setBeforeAutoSpacing(false);
para.getFormat().setBeforeSpacing(10);
para.getFormat().setAfterAutoSpacing(false);
para.getFormat().setAfterSpacing(10);
//设置字间距
for (DocumentObject object :(Iterable<DocumentObject>)para.getChildObjects())
{
TextRange textRange= (TextRange) object;
textRange.getCharacterFormat().setCharacterSpacing(3f);
}
//插入新加段落
document.getSections().get(0).getParagraphs().insert(1, para);
//保存文档
document.saveToFile("result.docx", FileFormat.Docx);
}
}
效果图:

本文将介绍如何使用Spire.Presentation for .NET给PowerPoint文档添加数字签名或删除PowerPoint文档中的现有数字签名。
添加数字签名
using Spire.Presentation;
using System;
namespace AddDigitalSignature
{
class Program
{
static void Main(string[] args)
{
//创建Presentation实例
Presentation ppt = new Presentation();
//加载PowerPoint文档
ppt.LoadFromFile("Input.pptx");
//添加数字签名
ppt.AddDigitalSignature("gary.pfx", "e-iceblue", "test", DateTime.Now);
//保存结果文档
ppt.SaveToFile("AddDigitalSignature_result.pptx", FileFormat.Pptx2010);
System.Diagnostics.Process.Start("AddDigitalSignature_result.pptx");
//释放
ppt.Dispose();
}
}
}
Imports Spire.Presentation
Imports System
Namespace AddDigitalSignature
Class Program
Private Shared Sub Main(ByVal args As String())
'加载ppt文档
Dim ppt As Presentation = New Presentation()
ppt.LoadFromFile("Input.pptx")
'添加签名
ppt.AddDigitalSignature("gary.pfx", "e-iceblue", "test", Date.Now)
'保存
ppt.SaveToFile("AddDigitalSignature.pptx", FileFormat.Pptx2013)
'释放
ppt.Dispose()
End Sub
End Class
End Namespace
结果文档:

删除数字签名
using Spire.Presentation;
using System;
using System.Security.Cryptography.X509Certificates;
namespace RemoveDigitalSignature
{
class Program
{
static void Main(string[] args)
{
//创建Presentation实例
Presentation ppt = new Presentation();
//加载PowerPoint文档
ppt.LoadFromFile("AddDigitalSignature.pptx");
//判断文档中是否有数字签名
if (ppt.IsDigitallySigned == true)
{
//删除所有数字签名
ppt.RemoveAllDigitalSignatures();
}
//保存结果文档
ppt.SaveToFile("RemoveDigitalSignature.pptx", FileFormat.Pptx2013);
}
}
}
Imports Spire.Presentation
Imports System
Imports System.Security.Cryptography.X509Certificates
Namespace RemoveDigitalSignature
Class Program
Private Shared Sub Main(ByVal args As String())
Dim ppt As Presentation = New Presentation()
ppt.LoadFromFile("AddDigitalSignature.pptx")
If ppt.IsDigitallySigned = True Then
ppt.RemoveAllDigitalSignatures()
End If
ppt.SaveToFile("RemoveDigitalSignature.pptx", FileFormat.Pptx2013)
End Sub
End Class
End Namespace
结果文档:

PPT中的动画可分为内置动画及自定义动画。设置内置动画效果可参考这篇文章,本文介绍使用Spire.Presentation for .NET来实现自定义动画效果。
using Spire.Presentation;
using Spire.Presentation.Collections;
using Spire.Presentation.Drawing.Animation;
using System.Drawing;
namespace CustomAnimation
{
class Program
{
static void Main(string[] args)
{
//创建一个幻灯片文档(新建的文档已默认包含一页幻灯片)
Presentation ppt = new Presentation();
ISlide slide = ppt.Slides[0];//获取第一页空白幻灯片
//添加形状(指定形状坐标、大小及相关格式设置)
IAutoShape shape = slide.Shapes.AppendShape(ShapeType.FivePointedStar, new RectangleF(100, 50, 180, 180));
shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Gradient;
shape.Fill.Gradient.GradientStops.Append(0, KnownColors.SkyBlue);
shape.Fill.Gradient.GradientStops.Append(1, KnownColors.Pink);
shape.ShapeStyle.LineColor.Color = Color.White;
//给形状设置动画效果
AnimationEffect effect = ppt.Slides[0].Timeline.MainSequence.AddEffect(shape, AnimationEffectType.PathUser);
CommonBehaviorCollection common = effect.CommonBehaviorCollection;
AnimationMotion motion = (AnimationMotion)common[0];
motion.Origin = AnimationMotionOrigin.Layout;
motion.PathEditMode = AnimationMotionPathEditMode.Relative;
MotionPath moinPath = new MotionPath();
moinPath.Add(MotionCommandPathType.MoveTo, new PointF[] { new PointF(0, 0) }, MotionPathPointsType.CurveAuto, true);
moinPath.Add(MotionCommandPathType.LineTo, new PointF[] { new PointF(0.18f, 0.18f) }, MotionPathPointsType.CurveAuto, true);
moinPath.Add(MotionCommandPathType.LineTo, new PointF[] { new PointF(-0.1f, 0.2f) }, MotionPathPointsType.CurveAuto, true);
moinPath.Add(MotionCommandPathType.LineTo, new PointF[] { new PointF(0.25f, 0.2f) }, MotionPathPointsType.CurveAuto, true);
moinPath.Add(MotionCommandPathType.End, new PointF[] { }, MotionPathPointsType.CurveStraight, true);
motion.Path = moinPath;
//保存文档
ppt.SaveToFile("CustomAnimation.pptx", FileFormat.Pptx2013);
System.Diagnostics.Process.Start("CustomAnimation.pptx");
}
}
}
Imports Spire.Presentation
Imports Spire.Presentation.Collections
Imports Spire.Presentation.Drawing.Animation
Imports System.Drawing
Namespace CustomAnimation
Class Program
Private Shared Sub Main(args As String())
'创建一个幻灯片文档(新建的文档已默认包含一页幻灯片)
Dim ppt As New Presentation()
'获取第一页空白幻灯片
Dim slide As ISlide = ppt.Slides(0)
'添加形状(指定形状坐标、大小及相关格式设置)
Dim shape As IAutoShape = slide.Shapes.AppendShape(ShapeType.FivePointedStar, New RectangleF(100, 50, 180, 180))
shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Gradient
shape.Fill.Gradient.GradientStops.Append(0, KnownColors.SkyBlue)
shape.Fill.Gradient.GradientStops.Append(1, KnownColors.Pink)
shape.ShapeStyle.LineColor.Color = Color.White
'给形状设置动画效果
Dim effect As AnimationEffect = ppt.Slides(0).Timeline.MainSequence.AddEffect(shape, AnimationEffectType.PathUser)
Dim common As CommonBehaviorCollection = effect.CommonBehaviorCollection
Dim motion As AnimationMotion = DirectCast(common(0), AnimationMotion)
motion.Origin = AnimationMotionOrigin.Layout
motion.PathEditMode = AnimationMotionPathEditMode.Relative
Dim moinPath As New MotionPath()
moinPath.Add(MotionCommandPathType.MoveTo, New PointF() {New PointF(0, 0)}, MotionPathPointsType.CurveAuto, True)
moinPath.Add(MotionCommandPathType.LineTo, New PointF() {New PointF(0.18F, 0.18F)}, MotionPathPointsType.CurveAuto, True)
moinPath.Add(MotionCommandPathType.LineTo, New PointF() {New PointF(-0.1F, 0.2F)}, MotionPathPointsType.CurveAuto, True)
moinPath.Add(MotionCommandPathType.LineTo, New PointF() {New PointF(0.25F, 0.2F)}, MotionPathPointsType.CurveAuto, True)
moinPath.Add(MotionCommandPathType.[End], New PointF() {}, MotionPathPointsType.CurveStraight, True)
motion.Path = moinPath
'保存文档
ppt.SaveToFile("CustomAnimation.pptx", FileFormat.Pptx2013)
System.Diagnostics.Process.Start("CustomAnimation.pptx")
End Sub
End Class
End Namespace
自定义动画效果:

本文介绍如何使用Spire.PDF从URL地址中下载PDF文档。
using System.IO;
using System.Net;
using Spire.Pdf;
namespace DownloadPdfFromUrl
{
class Program
{
static void Main(string[] args)
{
//创建PdfDocument对象
PdfDocument doc = new PdfDocument();
//创建WebClient对象
WebClient webClient = new WebClient();
//从URL地址下载PDF数据,并保存为流
using (MemoryStream ms = new MemoryStream(webClient.DownloadData("http://www.e-iceblue.com/images/sample.pdf")))
{
//加载流
doc.LoadFromStream(ms);
}
//保存为PDF文档
doc.SaveToFile("result.pdf", FileFormat.PDF);
}
}
}
Imports System.IO
Imports System.Net
Imports Spire.Pdf
Namespace DownloadPdfFromUrl
Class Program
Shared Sub Main(ByVal args() As String)
'创建PdfDocument对象
Dim doc As PdfDocument = New PdfDocument()
'创建WebClient对象
Dim webClient As WebClient = New WebClient()
'从URL地址下载PDF数据,并保存为流
Imports(MemoryStream ms = New MemoryStream(webClient.DownloadData("http:'www.e-iceblue.com/images/sample.pdf")))
{
'加载流
doc.LoadFromStream(ms)
}
'保存为PDF文档
doc.SaveToFile("result.pdf", FileFormat.PDF)
End Sub
End Class
End Namespace

之前我们介绍了如何插入和读取Excel文本批注,该文将介绍如何使用Spire.XLS for Java给Excel文档添加图片批注。
import com.spire.xls.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
public class Test {
public static void main(String[] args)throws IOException {
//加载示例文档
Workbook workbook = new Workbook();
workbook.loadFromFile("Sample.xlsx");
//获取第一个工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//设置字体
ExcelFont font = workbook.createFont();
font.setFontName("Arial");
font.setSize(11);
font.setKnownColor(ExcelColors.Orange);
CellRange range = sheet.getCellRange("D1");
//添加批注
ExcelComment comment = range.addComment();
//加载图片
BufferedImage bufferedImage = ImageIO.read(new File("Logo.jpg"));
//使用图片填充批注
comment.getFill().customPicture(bufferedImage, "Logo.jpg");
//设置批注高度和宽度
comment.setHeight(bufferedImage.getHeight());
comment.setWidth(bufferedImage.getWidth());
//设置批注可见
comment.setVisible(true);
//保存文档
workbook.saveToFile("output/setimageComment.xlsx", ExcelVersion.Version2013);
}
}
效果图:
