Spire.PDF 8.2.2已发布。该版本支持从 PDF 表单域获取 JavaScript 字符串,并增强了PDF到图片的转换功能。此外还修复了使用 PDF-Exchange查看器打开受权限密码保护的文档时产生错误等已知问题。详情请阅读以下内容:
新功能:
Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
doc.LoadFromFile(@"input.pdf", FileFormat.PDF);
PdfFormWidget fw = doc.Form as PdfFormWidget;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < fw.FieldsWidget.Count; i++)
{
PdfField pdfField = fw.FieldsWidget[i];
if (pdfField is PdfCheckBoxWidgetFieldWidget)
{
PdfCheckBoxWidgetFieldWidget checkBoxField = pdfField as PdfCheckBoxWidgetFieldWidget;
PdfJavaScriptAction mousedown = (PdfJavaScriptAction)checkBoxField.Actions.MouseDown; sb.Append(mousedown.Script.ToString());
}
}
string result = @"js-output.txt";
File.WriteAllText(result, sb.ToString());
问题修复:
将 HTML 文件转换为图像文件格式(如 PNG 或 JPEG)在存档和长期存储方面具有显著优势。原始 HTML 由可编辑的代码组成,依赖于 CSS 和 JavaScript 等外部资源,而图像则不同,它提供了固定的、不可更改的内容表现形式。因此,图像是保存网页在特定时间点的准确外观的理想选择。
本文将介绍如何使用 Spire.Doc for Java 将 HTML 文件和字符串转换为图像文件。
首先,您需要在 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>
Spire.Doc for Java 提供高效的 HTML 转图片功能,只需三步即可完成转换:首先使用 Document.loadFromFile() 加载 HTML 文件,再通过 Document.saveToImages() 将其转换为 BufferedImage 对象数组,最后可遍历数组将图像按需保存为 PNG、JPG 或 BMP 等常见格式。
使用 Java 将 HTML 文件转换为图片的步骤:
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.ImageType;
import com.spire.doc.documents.XHTMLValidationType;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class ConvertHtmlFileToImage {
public static void main(String[] args) throws IOException {
// 创建一个 Document 对象
Document document = new Document();
// 加载一个 HTML 文件
document.loadFromFile("C:\\Users\\Administrator\\Desktop\\Html.html", FileFormat.Html, XHTMLValidationType.None);
// 获取第一个节
Section section = document.getSections().get(0);
// 设置页面边距
section.getPageSetup().getMargins().setAll(2);
// 将文档转换为 BufferedImage 数组
BufferedImage[] images = document.saveToImages(ImageType.Bitmap);
// 遍历图像
for (int index = 0; index < images.length; index++)
{
// 指定输出文件名
String fileName = String.format("C:\\Users\\Administrator\\Desktop\\Output\\image_%d.png", index);
// 将每个图像保存为 PNG 文件
File file= new File(fileName);
ImageIO.write(images[index], "PNG", file);
}
// 释放资源
document.dispose();
}
}

在动态应用场景中,您可能需要通过编程方式生成或修改 HTML 内容,例如根据用户输入或数据库查询动态创建 HTML 字符串。针对这一需求,Spire.Doc for Java 提供了强大的解决方案:通 Paragraph.appendHTML() 方法,您可以直接将 HTML 内容无缝插入文档段落,实现高效灵活的文档处理。
使用 Java 将 HTML 字符串转换为图片的步骤:
import com.spire.doc.Document;
import com.spire.doc.Section;
import com.spire.doc.documents.ImageType;
import com.spire.doc.interfaces.IParagraph;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ConvertHtmlStringToImage {
public static void main(String[] args) throws IOException {
// 创建一个 Document 对象
Document document = new Document();
// 添加一个节
Section section = document.addSection();
// 设置页面边距
section.getPageSetup().getMargins().setAll(2);
// 添加一个段落
IParagraph paragraph = section.addParagraph();
// 从文件中读取 HTML 字符串
String htmlFilePath = "C:\\Users\\Administrator\\Desktop\\Html.html";
String htmlString = new String(Files.readAllBytes(Paths.get(htmlFilePath)), "UTF-8");
// 将 HTML 字符串附加到段落中
paragraph.appendHTML(htmlString);
// 将文档转换为 BufferedImage 数组
BufferedImage[] images = document.saveToImages(ImageType.Bitmap);
// 遍历图像
for (int index = 0; index < images.length; index++)
{
// 指定输出文件名
String fileName = String.format("C:\\Users\\Administrator\\Desktop\\Output\\image_%d.png", index);
// 将每个图像保存为 PNG 文件
File file= new File(fileName);
ImageIO.write(images[index], "PNG", file);
}
// 释放资源
document.dispose();
}
}

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
Spire.Doc for Java 5.2.3已发布。该版本支持在段落中查找所有匹配的字符串,并增强了Word到PDF以及HTML到Word的转换功能。此外还修复了加载Word文件时程序抛出“Operation is not valid due to the current state of the object”等已知问题。详情请阅读以下内容:
新功能:
Document document = new Document();
document.loadFromFile("https://cdn.e-iceblue.cn/Sample.docx");
Section s = document.getSections().get(0);
Paragraph para = s.getParagraphs().get(0);
//第一种方式
TextSelection[] textSelections = para.findAllString("The", false, true);
for (TextSelection selection : textSelections)
{
//高亮
selection.getAsOneRange().getCharacterFormat().setHighlightColor(new Color(255, 255, 0));
}
//第二种方式
Pattern c = Pattern.compile("The");
TextSelection[] textSelections = para.findAllPattern(c);
for (TextSelection selection : textSelections)
{
TextRange[] results = selection.getAsRange();
for (TextRange result : results)
{
//高亮
result.getCharacterFormat().setHighlightColor(Color.yellow);
}
}
问题修复:
TIFF 是一种常用的高位彩色图像格式,广泛应用于各领域和行业。在 Spire.PDF for Java 的格式转换功能中,支持TIFF格式;可实现 PDF 转为 TIFF 以及将 TIFF 转为 PDF。本文将通过 Java 应用程序代码介绍实现 TIFF 转为 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>11.11.11</version>
</dependency>
</dependencies>
实现转换的步骤如下:
import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfImage;
public class TiffToPDF {
public static void main(String[] args) {
//创建PdfDocument类的对象
PdfDocument pdf = new PdfDocument();
//添加一页
PdfPageBase page = pdf.getPages().add();
//从TIFF图像创建一个PdfImage对象
PdfImage tiff = PdfImage.fromFile("sample.tiff");
//将图片绘制到PDF页面
page.getCanvas().drawImage(tiff, 0, 0, 580, 800);
//保存文档
pdf.saveToFile("TiffToPdf.pdf", FileFormat.PDF);
}
}转换结果:

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。 获取有效期 30 天的临时许可证。
当打开一个密码保护的 PDF 时,我们必须输入密码。但有时我们需要在打开 PDF 文件之前知道它是否有密码保护。 Spire.PDF for Java 提供了一个方法 PdfDocument.isPasswordProtected() 来验证 PDF 文档是否受密码保护。本文将通过 Java 应用程序来展示如何验证。
首先,您需要在 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>11.11.11</version>
</dependency>
</dependencies>
import com.spire.pdf.*;
public class DetectPDF {
public static void main(String[] args) {
//定义文件路径
String filePath ="test.pdf";
//检测PDF文档是否受密码保护
boolean result = PdfDocument.isPasswordProtected(filePath);
//输出判定结果
if(result)
{
System.out.println("文档已加密");
}
else
{
System.out.println("文档未加密");
}
}
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。 获取有效期 30 天的临时许可证。
Spire.PDF for Java 5.2.3已发布。该版本支持将TIFF文件转换为PDF以及将OFD文件转换为PDF,还增强了PDF到HTML,PDF到图片,PDF到PDFA1B,PDF到PDFA2A的转换功能。此外还修复了程序在查找PDF文本时抛出“NullPointerException”等已知问题。详情请阅读以下内容。
新功能:
PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.getPages().add();
PdfImage myTiff = PdfImage.fromFile("TiffToPdf.tiff");
page.getCanvas().drawImage(myTiff, 60, 60, 260, 150);
doc.saveToFile("output.pdf", FileFormat.PDF);
boolean isPasswordProtected = PdfDocument.isPasswordProtected(inputFile);
OfdConverter converter = new OfdConverter(inputFile);
converter.toPdf(outputFile);
问题修复:
多栏 PDF 常用于杂志、报纸、研究文章等。使用 Spire.PDF for Java,您可以轻松地通过代码创建多栏 PDF。本文将向您展示如何在 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>11.11.11</version>
</dependency>
</dependencies>
详细步骤如下:
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;
public class TwoColumnPDF {
public static void main(String[] args) throws Exception {
//创建一个pdf文档
PdfDocument doc = new PdfDocument();
//添加新页面
PdfPageBase page = doc.getPages().add();
//设置位置和宽度
float x = 0;
float y = 15;
float width = 600;
//创建pen
PdfPen pen = new PdfPen(new PdfRGBColor(Color.black), 1f);
//在PDF页面上画线
page.getCanvas().drawLine(pen, x, y, x + width, y);
//定义段落文本
String s1 = "Spire.PDF for Java是一款专门对PDF文档进行操作的Java类库。该类库的主要功能在于帮助开发人员在Java应用程序"
+ "(J2SE和J2EE)中生成PDF文档和操作现有PDF文档,并且运行环境无需安装Adobe Acrobat。同时兼容大部分国产操作系统,"
+ "能够在中标麒麟和中科方德等国产操作系统中正常运行";
String s2 = "Spire.PDF for Java支持的功能非常丰富,例如PDF文档安全性设置,提取文本和图片,合并和拆分PDF,画文本、图片、"
+ "形状、条形码到PDF,打印PDF,创建和填充表单,添加和删除层,重叠PDF页面,添加文本/图片水印,添加、更新、"
+ "删除PDF书签,添加表格,压缩PDF文档等。此外,Spire.PDF for Java还支持将PDF文档高质量地转换为XPS、图片、"
+ "Excel、SVG、Word、HTML、OFD和PDF/A格式,以及将SVG、XPS 和 HTML文档高质量地转换为PDF格式";
//获取页面的宽度和高度
double pageWidth = page.getClientSize().getWidth();
double pageHeight = page.getClientSize().getHeight();
//创建实体画笔对象
PdfSolidBrush brush = new PdfSolidBrush(new PdfRGBColor(Color.BLACK));
//创建true type字体对象
PdfTrueTypeFont font= new PdfTrueTypeFont(new Font("Times New Roman",Font.PLAIN,14));
//通过PdfStringFormat类设置文本对齐方式
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
//绘制文本
page.getCanvas().drawString(s1, font, brush, new Rectangle2D.Double(0, 20, pageWidth / 2 - 8f, pageHeight), format);
page.getCanvas().drawString(s2, font, brush, new Rectangle2D.Double(pageWidth / 2 + 8f, 20, pageWidth / 2 - 8f, pageHeight), format);
//保存文档
String output = "output/createTwoColumnPDF.pdf";
doc.saveToFile(output, FileFormat.PDF);
}
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。 获取有效期 30 天的临时许可证。
Spire.Doc 10. 1现已发布。此次更新增强了使用NewEngine转换Word到PDFA以及转换Word到图片的功能,同时还修复无法嵌入私有字体的问题。具体修复内容如下。
问题修复:
尊敬的新老客户:
2022年春节将至,成都冰蓝科技有限公司全体职工在此感谢您在过去一年里的鼎力支持与厚爱,向您与您的家人致以最诚挚的祝福和问候!为落实节日期间的合作事宜,我司就春节放假做出如下通知:
春节放假时间为 2022年1月31日 至 2022年2月6日,共7天。2月7日(农历正月初七)正常上班。
放假期间,QQ、电话将暂停服务。如有购买、申请临时许可证文件以及其它紧急事宜,请通过邮件联系我们,值班工作人员将尽快为您解决。
因假期给您造成的不便,敬请谅解。
恭祝:虎年吉祥,幸福安康!
页边距是 PDF 页面周围的白色区域。如果您需要更多空白来添加其他信息,您可以增加边距。或者在其它情况您可能还需要减少边距。本文演示了如何使用 Spire.PDF for 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>11.11.11</version>
</dependency>
</dependencies>
增加 PDF 文档页边距的方法是创建一个更大页面的新 PDF,然后在较大页面上的适当位置绘制原始页面。以下是使用 Spire.PDF for Java 增加现有 PDF 文档页边距的主要步骤。
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.graphics.PdfTemplate;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
public class IncreaseMargins {
public static void main(String[] args) {
//加载原始PDF文档
PdfDocument originalPdf = new PdfDocument("C:\\Users\\Administrator\\Desktop\\sample.pdf");
//获取第一页的内容
PdfPageBase firstPage = originalPdf.getPages().get(0);
//创建一个新的PdfDocument对象
PdfDocument newPdf = new PdfDocument();
//设置边距的增值
PdfMargins margins = newPdf.getPageSettings().getMargins();
margins.setTop(40);
margins.setBottom(40);
margins.setLeft(40);
margins.setRight(40);
//设置新PDF文档的大小
Dimension2D dimension2D = new Dimension();
dimension2D.setSize(firstPage.getSize().getWidth() + margins.getLeft() + margins.getRight(), firstPage.getSize().getHeight() + margins.getTop() + margins.getBottom());
//循环浏览原始文档中的页面
for (int i = 0; i < originalPdf.getPages().getCount(); i++) {
//根据源页面创建模板
PdfTemplate template = originalPdf.getPages().get(i).createTemplate();
//在新的PDF文档中添加页面
PdfPageBase page = newPdf.getPages().add(dimension2D);
//在页面上绘制模板
template.draw(page.getCanvas(), new Point2D.Float(0, 0));
}
//将新文档保存到文件
newPdf.saveToFile("output/IncreaseMargins.pdf", FileFormat.PDF);
}
}
同样,减少 PDF 边距的方法是创建一个更小页面的新 PDF,然后在较小页面的指定坐标位置绘制原始页面。以下是使用 Spire.PDF for Java 减少现有 PDF 文档页边距的主要步骤。
import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.PdfMargins;
import com.spire.pdf.graphics.PdfTemplate;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Point2D;
public class DecreaseMargins {
public static void main(String[] args) {
//加载原始PDF文档
PdfDocument originalPdf = new PdfDocument("C:\\Users\\Administrator\\Desktop\\sample.pdf");
//获取第一页的内容
PdfPageBase firstPage = originalPdf.getPages().get(0);
//创建一个新的PdfDocument对象
PdfDocument newPdf = new PdfDocument();
//设置减值
double left = -20;
double right = -20;
double top = -20;
double bottom = -20;
//设置新PDF文档的页面大小
Dimension2D dimension2D = new Dimension();
dimension2D.setSize(originalPdf.getPages().get(0).getSize().getWidth() + left + right, originalPdf.getPages().get(0).getSize().getHeight() + top + bottom);
//循环浏览原始文档中的页面
for (int i = 0; i < originalPdf.getPages().getCount(); i++) {
//根据源页面创建模板
PdfTemplate template = originalPdf.getPages().get(i).createTemplate();
//在新的PDF文档中添加页面
PdfPageBase page = newPdf.getPages().add(dimension2D, new PdfMargins(0));
//在页面上绘制模板
template.draw(page.getCanvas(), new Point2D.Float((float) left, (float) top));
}
//将新文档保存到文件
newPdf.saveToFile("output/DecreaseMargins.pdf", FileFormat.PDF);
}
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。 获取有效期 30 天的临时许可证。