Spire.PDF for Java 9.10.3 已发布。本次更新新增支持设置 AES 加密算法以及重置已有域的名字,同时还增强了 PDF 到 SVG、PPTX 和 PDFA2B 以及 OFD 到 PDF 的转换功能。此外,一些已知问题也在该版本中得到修复,如加载 PDF 时程序抛"NullPointerException"的问题。详情请阅读以下内容。
新功能:
PdfEncryptionAlgorithm.AES//创建密码安全策略
PdfSecurityPolicy securityPolicy = new PdfPasswordSecurityPolicy("", "123456");
//设置加密算法
securityPolicy.setEncryptionAlgorithm( PdfEncryptionAlgorithm.AES_256);
//设置文档权限(所有权),默认是ForbidAll
securityPolicy.setDocumentPrivilege(PdfDocumentPrivilege.getForbidAll());
securityPolicy.getDocumentPrivilege().setAllowDegradedPrinting(true);
securityPolicy.getDocumentPrivilege().setAllowModifyAnnotations(true);
securityPolicy.getDocumentPrivilege().setAllowAssembly(true);
securityPolicy.getDocumentPrivilege().setAllowModifyContents(true);
securityPolicy.getDocumentPrivilege().setAllowFillFormFields(true);
securityPolicy.getDocumentPrivilege().setAllowPrint(true);
pdf.encrypt(securityPolicy);PdfDocument pdf = new PdfDocument();
//传递打开密码打开PDF文档
pdf.loadFromFile(inputFile, "1234");
//解密
pdf.decrypt();
pdf.saveToFile(outputFile, FileFormat.PDF);
pdf.dispose();PdfDocument document=new PdfDocument();
document.loadFromFile("input.pdf");
PdfFormWidget formWidget = (PdfFormWidget)document.getForm();
for (int i = 0; i < formWidget.getFieldsWidget().getCount(); i++)
{
PdfField field = (PdfField)formWidget.getFieldsWidget().get(i);
for (PdfFieldWidget widget : (Iterable<? extends PdfFieldWidget>) formWidget.getFieldsWidget())
{
if (widget.getName() == "oldName")
{
widget.setName("NewName");
}
}
}
document.saveToFile("result.pdf",FileFormat.PDF);问题修复:
Spire.Presentation 8.10.2已发布。该版本新增支持获取形状内文本的位置信息的功能。同时也支持获取形状中文本的“Ascent”和“Descent”属性和将形状转换为SVG格式。此外,还修复了将PPT转换为SVG后,用浏览器打开SVG文件时出现空白的问题。详情请阅读以下内容。
新功能:
Presentation ppt = new Presentation();
ppt.LoadFromFile(inputFile);
StringBuilder sb = new StringBuilder();
ISlide slide = ppt.Slides[0];
for (int i = 0; i < slide.Shapes.Count; i++)
{
IShape shape = slide.Shapes[i];
if (shape is IAutoShape)
{
IAutoShape autoshape = slide.Shapes[i] as IAutoShape;
string text = autoshape.TextFrame.Text;
//obtain the text position information
PointF point = autoshape.TextFrame.GetTextLocation();
sb.AppendLine("Shape " + i + ":" + text + "\r\n" + "location:" + point.ToString());
}
}
File.AppendAllText(outputFile, sb.ToString());
ppt.Dispose();Presentation ppt = new Presentation();
ppt.LoadFromFile(inputFile);
ISlide slide = ppt.Slides[0];
IAutoShape autoshape = slide.Shapes[0] as IAutoShape;
IList<LineText> lines = autoshape.TextFrame.GetLayoutLines();
for (int i = 0; i < lines.Count; i++)
{
float ascent= lines[i].Ascent;
float descent = lines[i].Descent;
}Presentation ppt = new Presentation();
ppt.LoadFromFile("FreeForm.pptx");
ISlide slide = ppt.Slides[0];
int num = 0;
foreach (IShape shape in slide.Shapes)
{
byte[] svgByte = shape.SaveAsSvg();
FileStream fs = new FileStream("shape_" + num + ".svg",FileMode.Create);
fs.Write(svgByte, 0, svgByte.Length);
fs.Close();
num++;
}问题修复:
https://www.e-iceblue.cn/Downloads/Spire-Presentation-NET.html
Spire.XLS 13.10.1 已发布。本次更新提升了 Excel 到 PDF 的转换效率,同时新增支持自定义透视表字段的名称。此外,本次更新还增强了 Excel 到 HTML 的转换。一些已知问题也在该版本中得到修复,如被删除的注释没有从文档中移除成功的问题。详情请阅读以下内容。
新功能:
sheet.PivotTables[0].RowFields[0].CustomName = "rowName";
sheet.PivotTables[0].ColumnFields[0].CustomName = "colName";
sheet.PivotTables[0].PivotFields[0].CustomName = "fieldName1";
sheet.PivotTables[0].DataFields[0].CustomName = "dataName";问题修复:
当您需要在别的地方重复利用 PowerPoint 中的图片时,那么将其中的图片提取出来是很有必要的操作。提取后,您就可以灵活地在原始 PPT 文件之外使用这些图片,从而在不同的项目中最大限度地发挥它们的价值。本文将介绍如何使用 Spire.Presentation for Python 通过代码从 PowerPoint 文档中提取图片。
本教程需要用到 Spire.Presentation for Python 和 plum-dispatch v1.7.4。可以通过以下 pip 命令将它们轻松安装到 Windows 中。
pip install Spire.Presentation如果您不确定如何安装,请参考此教程:如何在 Windows 中安装 Spire.Presentation for Python
要从整个 PPT 演示文稿中提取图片,需要使用 Presentation.Images 属性获取文档中所有图片的集合。然后遍历集合中的元素,并调用 IImageData.Image.Save() 方法将每个元素保存为一个图片文件。具体步骤如下:
from spire.presentation.common import *
from spire.presentation import *
# 创建Presentation对象
ppt = Presentation()
# 加载PPT文档
ppt.LoadFromFile("示例.pptx")
# 遍历文档中所有图片
for i, image in enumerate(ppt.Images):
# 提取图片
ImageName = "提取图片/图_"+str(i)+".png"
image.Image.Save(ImageName)
ppt.Dispose()

要从指定幻灯片中提取图片,需要遍历幻灯片上的所有形状,并找到 SlidePicture 或 PictureShape 类型的形状,然后使用 SlidePicture.PictureFill.Picture.EmbedImage.Image.Save() 或 PictureShape.EmbedImage.Image.Save() 方法将其保存为图像文件。具体步骤如下:
from spire.presentation.common import *
from spire.presentation import *
# 创建Presentation对象
ppt = Presentation()
# 加载PPT文档
ppt.LoadFromFile("示例.pptx")
# 获取指定幻灯片
slide = ppt.Slides[1]
i = 0
#遍历该幻灯片中的所有形状
for s in slide.Shapes:
# 确定形状是否为SlidePicture类型
if isinstance(s, SlidePicture):
# 如果是,则将图片保存到指定路径
ps = s if isinstance(s, SlidePicture) else None
ps.PictureFill.Picture.EmbedImage.Image.Save("幻灯片图片/图_"+str(i)+".png")
i += 1
# 确定形状是否为PictureShape类型
if isinstance(s, PictureShape):
# 如果是,则将图片保存到指定路径
ps = s if isinstance(s, PictureShape) else None
ps.EmbedImage.Image.Save("幻灯片图片/图_"+str(i)+".png")
i += 1
ppt.Dispose()

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
设置适当的页边距是创建专业 Word 文档的重要步骤。页边距看似是一个小细节,但在提升文档外观方面却起着至关重要的作用。页边距控制了文档内容周围的空白空间,能够帮助保持内容布局的平衡,提升文档的展示效果。本文将介绍如何使用 Spire.Doc for Python 通过 Python 程序设置 Word 文档页边距。
本教程需要用到 Spire.Doc for Python 和 plum-dispatch v1.7.4。可以通过以下 pip 命令将它们轻松安装到 Windows 中。
pip install Spire.Doc如果您不确定如何安装,请参考此教程:如何在 Windows 中安装 Spire.Doc for Python
Spire.Doc for Python 在 Margins 类下提供了一些属性,可用于设置文档页面四边的边距。需要注意页边距是根据节设置的,如果需要使整个文档的页边距保持一致,就要遍历文档的每个节来设置相同的页边距。以下是设置页边距的详细操作步骤:
from spire.doc import *
from spire.doc.common import *
# 创建Document类的对象
doc = Document()
# 读取Word文档
doc.LoadFromFile("示例.docx")
# 遍历文档的每个节
for i in range(doc.Sections.Count):
# 获取节
section = doc.Sections.get_Item(i)
# 获取节的页边距
margins = section.PageSetup.Margins
# 设置上下左右页边距
margins.Top = 17.9
margins.Bottom = 17.9
margins.Left = 20.9
margins.Right = 20.9
# Margins.All = 17.9
# 保存文档
doc.SaveToFile("output/设置页边距.docx", FileFormat.Auto)

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
Spire.Doc 11.10.3已发布。该版本新增了转换Word到PDF时文本整形的功能且支持 .NET 4.6.2及以上、 .NET Core 和 .NET Standard平台。详情请阅读以下内容。
新功能:
Document doc = new Document();
doc.LoadFromFile(fileName);
doc.LayoutOptions.UseHarfBuzzTextShaper = true;
doc.SaveToFile(pdfFileName, Spire.Doc.FileFormat.PDF)Spire.PDF for Android via Java 9.10.1已发布。该版本支持在加密PDF文档时设置加密选项以及支持判断PDF文档流是否被加密。 此外,它还新增了支持将PDF转换为Word的新方法。详情请阅读以下内容。
新功能:
PdfDocument pdfdoc = new PdfDocument();
pdfdoc.loadFromFile(inputFile);
PdfSecurityPolicy securityPolicy = new PdfPasswordSecurityPolicy("123", "e-iceblue");
securityPolicy.setEncryptionAlgorithm(PdfEncryptionAlgorithm.AES_128);
securityPolicy.setEncryptMetadata(false);
pdfdoc.encrypt(securityPolicy);
pdfdoc.saveToFile(outputFile);FileInputStream stream_1 = new FileInputStream(new File(inputFile_1));
boolean isPasswordProtected_1 = PdfDocument.isPasswordProtected(stream_1);PdfToWordConverter convert = new PdfToWordConverter(inputFile);
convert.saveToDocx(outputFile);https://www.e-iceblue.cn/Downloads/pdf-for-android-via-java.html
Spire.PDF 9.10.2 已发布。本次更新新增支持按阅读顺序获取分栏文档的文本坐标、为 PdfStringFormat 设置 HorizontalScalingFactor 属性以及在转换有权限密码的PDF时设置是否允许转换。同时该版本还增强了 OFD 到 PDF 的转换。此外,一些已知问题也在该版本种得到修复,如提取的表格数据格式不正确的问题。详情请阅读以下内容。
新功能:
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(input);
PdfPageBase pdfPageBase = doc.Pages[0];
PdfTextFinder finder = new PdfTextFinder(pdfPageBase);
finder.Options.Strategy = PdfTextStrategy.Simple;PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();
string text = "Please add the code picture BehindText=true to set the picture behind text";
PdfSolidBrush solidBrush = new PdfSolidBrush (new PdfRGBColor(Color.Black));
PdfStringFormat format = new PdfStringFormat():
format.HorizontalScalingFactor = 80;
PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 14f, PdfFontStyle.Regular);
PdfGraphicsState state = page.Canvas.Save();
page.Canvas.DrawString(text, font, solidBrush, 0, 0, format);
page.Canvas.Restore(state):
doc.SaveToFile(outputFile);
doc.Close();PdfDocument doc = new PdfDocument();
doc.LoadFromFile(inputFile);
doc.ConvertOptions.ApplyPermissionsOptions(true);
StringBuilder sb = new StringBuilder();
foreach (FileFormat type in Enum.GetValues(typeof(FileFormat)))
{
try
{
if (type.ToString().Equals("PDF"))
{
doc.SaveToFile(outputFile_P, type);
}
else
{
doc.SaveToFile(outputFile, type);
}
}
catch (Exception ex)
{
sb.AppendLine("save to: "+ type +" :"+ ex.Message);
}
}
File.AppendAllText(outputFile,sb.ToString());
doc.Dispose();问题修复:
PDF 文档的比较功能能够帮助用户实现高效的文档管理。通过比较 PDF 文档,用户可以轻松识别文档内容的差异,对文档有更全面的了解,从而更方便地对文档内容进行修改和整合。本文将介绍如何使用 Spire.PDF for Java 通过 Java 程序比较 PDF 文档并找出文档内容的差异。
首先,您需要在 Java 程序中添加 Spire.Pdf.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.pdf</artifactId>
<version>12.9.0</version>
</dependency>
</dependencies>
将用于比较的两个 PDF 文档的示例:

Spire.PDF for Java 提供了 PdfComparer 类来表示 PDF 文档比较器。用户可以使用两个 PdfDocument 类对象创建 PdfComparer 类的对象,从而创建对两个文档进行比较的比较器。创建好 PdfComparer 类的对象后,用户可使用 PdfComparer.compare(String fileName) 方法对两个文档进行比较,并将比较结果保存到新的 PDF 文件。
生成的比较结果 PDF 文档左右会两边分别显示两个原始文档,删除的项目用红色显示在左边,添加的项目用黄色显示在右边。
比较两个 PDF 文档的详细操作步骤如下:
import com.spire.pdf.PdfDocument;
import com.spire.pdf.comparison.PdfComparer;
public class ComparePDF {
public static void main(String[] args) {
//创建一个PdfDocument类对象并加载一个PDF文档
PdfDocument pdf1 = new PdfDocument();
pdf1.loadFromFile("示例1.pdf");
//创建另一个PdfDocument类对象并加载另一个PDF文档
PdfDocument pdf2 = new PdfDocument();
pdf2.loadFromFile("示例2.pdf");
//创建一个PdfComparer类的对象
PdfComparer comparer = new PdfComparer(pdf1, pdf2);
//比较两个PDF 档并将比较结果保存到一个新文档中
comparer.compare("比较结果.pdf");
}
}

在对文档进行比较之前,还可以使用 PdfComparer.getOptions().setPageRanges() 方法来限制要比较的页面范围。具体操作步骤如下:
import com.spire.pdf.PdfDocument;
import com.spire.pdf.comparison.PdfComparer;
public class ComparePDFPageRange {
public static void main(String[] args) {
//创建一个PdfDocument类的对象并加载一个PDF文档
PdfDocument pdf1 = new PdfDocument();
pdf1.loadFromFile("示例1.pdf");
//创建另一个PdfDocument类的对象并加载另一个PDF文档
PdfDocument pdf2 = new PdfDocument();
pdf2.loadFromFile("示例2.pdf");
//创建一个PdfComparer类的对象
PdfComparer comparer = new PdfComparer(pdf1, pdf2);
//设置要比较的页面范围
comparer.getOptions().setPageRanges(1, 1, 1, 1);
//比较这两个PDF文档并将比较结果保存到一个新文档中
comparer.compare("比较结果.pdf");
}
}

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
对于许多使用 Python 编程语言的开发人员来说,创建、读取和编辑 Word 文档是一个常见需求。无论是生成报告、操作现有文档还是自动化文档创建,通过程序来处理 Word 文档可以极大地提高工作效率。本文将介绍如何使用 Spire.Doc for Python 在 Python 程序中创建、读取或编辑 Word 文档。
本教程需要用到 Spire.Doc for Python 和 plum-dispatch v1.7.4。可以通过以下 pip 命令将它们轻松安装到 Windows 中。
pip install Spire.Doc如果您不确定如何安装,请参考此教程:如何在 Windows 中安装 Spire.Doc for Python
Spire.Doc for Python 提供了 Document 类用于表示 Word 文档模型。文档必须至少包含一个节(由 Section 类表示),每个节都是各种元素(如段落、表格、图表和图像)的容器。此示例将展示如何使用 Spire.Doc for Python 创建一个包含多个段落的简单 Word 文档。
from spire.doc import *
from spire.doc.common import *
# 创建一个 Document 类的对象
doc = Document()
# 添加一个节
section = doc.AddSection()
# 设置页面边距
section.PageSetup.Margins.All = 40
# 添加一个标题
titleParagraph = section.AddParagraph()
titleParagraph.AppendText("Spire.Doc for Python 简介")
# 添加两个段落
bodyParagraph_1 = section.AddParagraph()
bodyParagraph_1.AppendText("Spire.Doc for Python 是专为开发人员设计的专业 Python 库,可在任何 Python 应用程序中创建、读取、写入、转换、比较和打印 Word 文档,处理速度快且质量高。")
bodyParagraph_2 = section.AddParagraph()
bodyParagraph_2.AppendText("作为独立的 Word Python API,Spire.Doc for Python 不需要在开发系统或目标系统上安装 Microsoft Word,而可以将 Microsoft Word 文档创建能力整合到任何 Python 应用程序中。")
# 应用标题1样式到标题段落
titleParagraph.ApplyStyle(BuiltinStyle.Heading1)
# 创建样式用于段落
style2 = ParagraphStyle(doc)
style2.Name = "paraStyle"
style2.CharacterFormat.FontName = "HarmonyOS Sans SC"
style2.CharacterFormat.FontSize = 13
doc.Styles.Add(style2)
bodyParagraph_1.ApplyStyle("paraStyle")
bodyParagraph_2.ApplyStyle("paraStyle")
# 设置段落的水平对齐方式
titleParagraph.Format.HorizontalAlignment = HorizontalAlignment.Center
bodyParagraph_1.Format.HorizontalAlignment = HorizontalAlignment.Left
bodyParagraph_2.Format.HorizontalAlignment = HorizontalAlignment.Left
# 设置段落之间的间距
titleParagraph.Format.AfterSpacing = 10
bodyParagraph_1.Format.AfterSpacing = 10
# 保存到文件
doc.SaveToFile("output/Word文档.docx", FileFormat.Docx2019)

使用 Document.GetText() 方法即可直接提取出 Word 文档中的所有文本内容。以下是详细操作步骤。
from spire.doc import *
from spire.doc.common import *
# 创建一个 Document 类的对象
doc = Document()
# 加载一个 Word 文件
doc.LoadFromFile("output/Word文档.docx")
# 从整个文档中获取文本
text = doc.GetText()
# 打印文本
print(text)

Spire.Doc for Python 提供的 Section.Paragraphs[index] 属性可以获取 Word 文档中的指定段落。如果要修改段落的文本,则可以通过 Paragraph.Text 属性为段落重新分配文本。以下是详细操作步骤。
from spire.doc import *
from spire.doc.common import *
# 创建一个 Document 对象
doc = Document()
# 加载一个 Word 文件
doc.LoadFromFile("output/Word文档.docx")
# 获取特定的章节
section = doc.Sections[0]
# 获取特定的段落
paragraph = section.Paragraphs[1]
# 更改段落的文本
paragraph.Text = "标题已更改"
# 保存到文件
doc.SaveToFile("output/编辑Word文档.docx", FileFormat.Docx2019)

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。