当我们需要对单个 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>
Spire.PDF for Java 提供了 PdfPageBase.createTemplate().draw() 方法来将一个 PDF 页面的内容绘制在一个新的 PDF 页面上。我们可以使用这个方法将原页面的内容分别绘制在多个页面上来达到拆分 PDF 页面的目的。以下是将一个 PDF 页面拆分成三个页面的详细操作步骤:
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.geom.Point2D;
public class splitPDFPage {
public static void main(String[] args) {
//创建PdfDocument的对象
PdfDocument pdf = new PdfDocument();
//载入PDF文档
pdf.loadFromFile("C:/四月.pdf");
//获取PDF文档的第一页
PdfPageBase page = pdf.getPages().get(0);
//创建一个新的PDF文档并移除页边距
PdfDocument newPdf = new PdfDocument();
newPdf.getPageSettings().getMargins().setAll(0);
//横向拆分
newPdf.getPageSettings().setWidth((float) page.getSize().getWidth());
newPdf.getPageSettings().setHeight((float) page.getSize().getHeight()/3);
//水平拆分
//newPdf.getPageSettings().setWidth((float) page.getSize().getWidth()/2);
//newPdf.getPageSettings().setHeight((float) page.getSize().getHeight());
//添加一个页面到新建的PDF文档
PdfPageBase newPage = newPdf.getPages().add();
//将PdfLayoutType设置为Paginate来使内容自动分页
PdfTextLayout layout = new PdfTextLayout();
layout.setBreak(PdfLayoutBreakType.Fit_Page);
layout.setLayout(PdfLayoutType.Paginate);
//将原文的页面的内容绘制在新文档页面上
page.createTemplate().draw(newPage, new Point2D.Float(0, 0), layout);
//保存文档
newPdf.saveToFile("拆分PDF页面.pdf");
newPdf.close();
}
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
Spire.XLS for Java 13.4.1 已发布。改版本增强了 Excel 到 HTML 和 PDF 的转换。此外,该版本还修复了一些已知问题,如插入图片后图片高度改变的问题。详情请阅读以下内容。
问题修复:
对于包含机密信息或敏感信息的 PDF 文档,使用密码对其进行加密可以确保只有指定人员能够获取其中的内容。而对加密的 PDF 文档进行解密处理,则可以方便公开发布文档,使任何人都能够随意获取其中的内容。本文将介绍如何使用 Spire.PDF for Java 通过程序加密 PDF 文档或解密 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 文档,必须输入密码才能查看有打开密码的 PDF 文档。而权限密码则可以限制对文档进行操作,如打印、复制、评论等。当一个 PDF 文档被同时用这两种密码加密时,可以使用其中任意一种来打开 PDF 文档。
Spire.PDF for Java 提供的 PdfDocument.encrypt()方法可以使用密码对PDF文档进行加密。 方法创建基于密码的安全策略, 方法可以同时用于设置打开密码和权限密码。以下是详细操作步骤:
import java.util.EnumSet;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;
public class encryptPDF {
public static void main(String[] args) {
//创建PdfDocument的对象
PdfDocument doc = new PdfDocument();
//载入PDF文件
doc.loadFromFile("C:/信用卡跟踪表.pdf");
// 创建基于密码的安全策略,包含打开密码和权限密码
PdfSecurityPolicy securityPolicy = new PdfPasswordSecurityPolicy("openPwd", "permissionPwd");
// 设置加密算法为AES 256位
securityPolicy.setEncryptionAlgorithm(PdfEncryptionAlgorithm.AES_256);
// 设置文档权限为禁止所有操作
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);
// 加密文档
doc.encrypt(securityPolicy);
// 将加密后的文档保存到输出文件路径
doc.saveToFile("加密.pdf",FileFormat.PDF);
// 释放文档资源
doc.dispose();
}
}
如需解密 PDF 文件,可以使用PdfDocument.decrypt()方法解密文档。 以下是详细操作步骤:
import com.spire.pdf.PdfDocument;
import com.spire.pdf.security.PdfEncryptionKeySize;
import com.spire.pdf.security.PdfPermissionsFlags;
public class decryptPDF {
public static void main(String[] args) {
//创建PdfDocument的对象
PdfDocument pdf = new PdfDocument();
//使用密码载入加密的PDF文档
pdf.loadFromFile("加密.pdf", "password");
//解密文档
pdf.decrypt();
//保存文件
pdf.saveToFile("解密.pdf");
pdf.close();
}
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
Spire.Spreadsheet 7.4.2 已发布。本次更新支持保存到PDF文档。详情请阅读以下内容。
新功能:
this.spreadsheet1.SaveToFile("1.pdf", FileFormatType.PDF);https://www.e-iceblue.cn/Downloads/Spire-Spreadsheet-NET.html
创建工作表时,MS Excel 使用的默认字体是“Calibri”,大小为 11,颜色为黑色。但是,有时您可能想要应用不同的字体样式(例如粗体、斜体)来优化文档的外观,或者设置独特的字体颜色以突出显示大量数据中的有用信息。在本文中,您将学习如何使用 Spire.XLS for C++ 以编程方式将多种字体样式应用于 Excel 单元格。
有两种方法可以将 Spire.XLS for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。
如何将 Spire.XLS for C++ 集成到 C++ 程序中
Spire.XLS for C++ 提供的 ExcelFont 类允许您轻松设置或更改单元格中的字体名称、颜色、大小和样式。以下是将不同字体应用于 Excel 中不同单元格的步骤。
#include "Spire.Xls.o.h";
using namespace Spire::Xls;
int main() {
//指定输出文件路径和名称
std::wstring outputPath = L"OUTPUT\\";
std::wstring outputFile = outputPath + L"应用字体到单元格.xlsx";
//创建工作簿对象
Workbook* workbook = new Workbook();
//获取第一个工作表
Worksheet* sheet = workbook->GetWorksheets()->Get(0);
//设置字体名称
sheet->GetRange(L"B1")->SetText(L"字体名称:宋体");
sheet->GetRange(L"B1")->GetStyle()->GetFont()->SetFontName(L"宋体");
//设置字体大小
sheet->GetRange(L"B2")->SetText(L"字体大小:25");
sheet->GetRange(L"B2")->GetStyle()->GetFont()->SetSize(25);
//将文本设置为粗体
sheet->GetRange(L"B3")->SetText(L"字体样式:加粗");
sheet->GetRange(L"B3")->GetStyle()->GetFont()->SetIsBold(true);
//给文本添加下划线
sheet->GetRange(L"B4")->SetText(L"字体样式:下划线");
sheet->GetRange(L"B4")->GetStyle()->GetFont()->SetUnderline(FontUnderlineType::Single);
//设置字体颜色
sheet->GetRange(L"B5")->SetText(L"字体颜色:蓝色");
sheet->GetRange(L"B5")->GetStyle()->GetFont()->SetColor(Spire::Common::Color::GetBlue());
//将文本设置为斜体
sheet->GetRange(L"B6")->SetText(L"字体样式:斜体");
sheet->GetRange(L"B6")->GetStyle()->GetFont()->SetIsItalic(true);
//在文本中添加删除线
sheet->GetRange(L"B7")->SetText(L"字体样式:删除线");
sheet->GetRange(L"B7")->GetStyle()->GetFont()->SetIsStrikethrough(true);
//保存结果文档
workbook->SaveToFile(outputFile.c_str(), ExcelVersion::Version2013);
workbook->Dispose();
}
在一个单元格中混合使用不同的字体可以帮助您强调某些特定的字符。以下是在 Excel 单元格中应用多种字体的步骤。
#include "Spire.Xls.o.h";
using namespace Spire::Xls;
int main() {
//指定输出文件路径和名称
std::wstring outputPath = L"OUTPUT\\";
std::wstring outputFile = outputPath + L"将多个字体应用于单元格.xlsx";
//创建工作簿对象
Workbook* workbook = new Workbook();
//获取第一个工作表
Worksheet* sheet = workbook->GetWorksheets()->Get(0);
//创建ExcelFont对象并设置其字体样式、颜色和大小
ExcelFont* font1 = workbook->CreateExcelFont();
font1->SetKnownColor(ExcelColors::Red);
font1->SetIsBold(true);
font1->SetIsItalic(true);
font1->SetSize(15);
//创建另一个ExcelFont对象并设置其字体样式、颜色和大小
ExcelFont* font2 = workbook->CreateExcelFont();
font2->SetKnownColor(ExcelColors::LightBlue);
font2->SetIsBold(true);
font2->SetFontName(L"宋体");
font2->SetSize(13);
//将RichText字符串写入单元格B5
RichText* richText = sheet->GetRange(L"B5")->GetRichText();
richText->SetText(L"Spire.XLS for C++是一款专业且独立的 C++ Excel 组件。");
//将两种字体应用于单元格B5中的文本
richText->SetFont(0, 16, font1);
richText->SetFont(17, 39, font2);
//保存结果文档
workbook->SaveToFile(outputFile.c_str(), ExcelVersion::Version2013);
workbook->Dispose();
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
Spire.Office 8.4.0 已发布。在该版本中,Spire.Doc 支持设置线条的类型(dashed/dotted),以及Docx2016和Docx2019文件格式;Spire.PDF 支持文本表单域的字体大小自动设置;Spire.Email 修复了读取的邮件日期信息不正确的问题。此外,该版本还成功修复了许多已知问题。详情请阅读以下内容。
该版本涵盖了最新版的 Spire.Doc,Spire.PDF,Spire.XLS,Spire.Email,Spire.DocViewer, Spire.PDFViewer,Spire.Presentation,Spire.Spreadsheet, Spire.OfficeViewer, Spire.DocViewer, Spire.Barcode, Spire.DataExport。
版本信息如下:
https://www.e-iceblue.cn/Downloads/Spire-Office-NET.html
新功能:
Document doc = new Document();
Section sec = doc.AddSection();
Paragraph para1 = sec.AddParagraph();
ShapeObject shape1 = para1.AppendShape(100, 100, ShapeType.Line);
shape1.FillColor = Color.Purple;
shape1.StrokeColor = Color.Black;
shape1.LineStyle = ShapeLineStyle.Single;
shape1.LineDashing = LineDashing.Dash;
doc.SaveToFile("InsertShapes.docx", Spire.Doc.FileFormat.Docx2013);Spire.Doc.FileFormat.Docx2016
Spire.Doc.FileFormat.Docx2019问题修复:
新功能:
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile(input);
PdfFormWidget formWidget = pdf.Form as PdfFormWidget;
foreach(PdfField field in formWidget.FieldsWidget.List)
{
if(field is PdfTextBoxFieldWidget)
{
PdfTextBoxFieldWidget textBoxField = field as PdfTextBoxFieldWidget;
if(textBoxField.Name == "Name of the Company")
{
textBoxField.Font = new PdfTrueTypeFont(new Font("Arial", 16), true);
textBoxField.FontSizeAuto = true;
textBoxField.Text = "e-iceblue";
}
}
}
pdf.SaveToFile(output);问题修复:
问题修复:
Spire.XLS 13.4.0 已发布。该版本新增支持 BITXOR 和 ISFUNCTION 函数。此外,该版本还增强了 Excel 到 HTML、图片、PDF,以及 HTML 到 Excel 的转换。许多已知问题也在该版本被成功修复,如在加载 .xlsm 文件时程序抛出“缺少根元素”的问题。详情请阅读以下内容。
新功能:
Workbook workbook = new Workbook();
workbook.Worksheets[0].Range["A1"].Formula = "BITXOR(12,58)";
workbook.CalculateAllValue();
workbook.SaveToFile("result.xlsx");Workbook workbook = new Workbook();
workbook.Worksheets[0].Range["A1"].Formula = "ISFORMULA(A1)";
workbook.CalculateAllValue();
workbook.SaveToFile("result.xlsx");问题修复:
Spire.Doc 11.4.0发布。本次更新支持了设置线条的类型(dashed/dotted),并支持Docx2016和Docx2019文件格式。同时,该版本还增强了 Word 到 PDF的转换。此外,许多已知问题也在该版本中成功被修复,如:修复了将文档A内容复制到文档B里面后表格样式错误的问题。详情请阅读以下内容。
新功能:
Document doc = new Document();
Section sec = doc.AddSection();
Paragraph para1 = sec.AddParagraph();
ShapeObject shape1 = para1.AppendShape(100, 100, ShapeType.Line);
shape1.FillColor = Color.Purple;
shape1.StrokeColor = Color.Black;
shape1.LineStyle = ShapeLineStyle.Single;
shape1.LineDashing = LineDashing.Dash;
doc.SaveToFile("InsertShapes.docx", Spire.Doc.FileFormat.Docx2013);Spire.Doc.FileFormat.Docx2016
Spire.Doc.FileFormat.Docx2019问题修复:
通过扫描创建的 PDF 文档可能会出现页面方向错误的问题(如上下颠倒),这样的情况会给阅读带来极大的不便。旋转 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 页面时,旋转角度以 90 度为增量。可设置的旋转角度为 0、90、180 或 270 度。以下是旋转指定 PDF 页面的详细操作步骤:
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageRotateAngle;
public class rotatePDFPage {
public static void main(String []args){
//创建PdfDocument的对象
PdfDocument pdf = new PdfDocument();
//载入PDF文档
pdf.loadFromFile("C:/预算概要.pdf");
//获取文档第二页
PdfPageBase page = pdf.getPages().get(1);
//获取页面的原旋转角度
int rotation = page.getRotation().getValue();
//在原旋转角度的基础上将页面顺时针旋转180度
rotation += PdfPageRotateAngle.Rotate_Angle_180.getValue();
page.setRotation(PdfPageRotateAngle.fromValue(rotation));
//保存文档
pdf.saveToFile("旋转页面.pdf");
}
}
以下是旋转 PDF 文档中所有页面的操作步骤:
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.PdfPageRotateAngle;
public class rotateAllPDFPages {
public static void main(String []args){
//创建PdfDocument的对象
PdfDocument pdf = new PdfDocument();
//载入PDF文档
pdf.loadFromFile("C:/预算概要.pdf");
//循环遍历文档中的页面
for(int i = 0; i < pdf.getPages().getCount(); i++) {
//获取页面
PdfPageBase page = pdf.getPages().get(i);
//获取页面的原旋转角度
int rotation = page.getRotation().getValue();
//在原旋转角度的基础上将页面顺时针旋转180度
rotation += PdfPageRotateAngle.Rotate_Angle_180.getValue();
page.setRotation(PdfPageRotateAngle.fromValue(rotation));
}
//保存文档
pdf.saveToFile("旋转所有页面.pdf");
}
}如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
Excel 和 CSV(字符分隔值)是各行业管理和存储表格数据的两种常用文件格式。Excel 以行和列的形式组织数据,并为用户提供广泛的数据操作、分析和可视化高级功能,而 CSV 则以纯文本格式存储数据,这种格式轻巧且与各种应用程序高度兼容。当用户需要在不同的程序中交换或导入 Excel 数据时,将 Excel 文件转换为 CSV 格式非常有用。相反,需要更高级的数据分析功能(如创建图表或应用公式)的用户可能会发现将 CSV 文件转换为 Excel 格式更为有效。在本文中,我们将演示如何使用 Spire.XLS for C++ 将 Excel 转换为 CSV 或将 CSV 转换为Excel。
有两种方法可以将 Spire.XLS for C++ 集成到您的应用程序中。一种方法是通过 NuGet 安装它,另一种方法是从我们的网站下载包并将库复制到您的程序中。通过 NuGet 安装更简单,更推荐使用。您可以通过访问以下链接找到更多详细信息。
如何将 Spire.XLS for C++ 集成到 C++ 程序中
Spire.XLS for C++ 提供了 XlsWorksheet->SaveToFile (LPCWSTR_S fileName, LPCWSTR_S separator, Spire::Common::Encoding* encoding) 方法,用于将 Excel 文件中的工作表转换为 CSV。具体步骤如下:
#include "Spire.Xls.o.h";
using namespace Spire::Xls;
int main()
{
//初始化Workbook类的实例
Workbook* workbook = new Workbook();
//加载Excel文件
workbook->LoadFromFile(L"示例文档.xlsx");
//获取指定工作表
Worksheet* sheet = workbook->GetWorksheets()->Get(0);
//将工作表保存到CSV文件
sheet->SaveToFile(L"ExcelToCsv.csv", L",", Encoding::GetUTF8());
workbook->Dispose();
delete workbook;
}
使用上述代码将 Excel 工作表转换为 CSV 时,所有数据,包括可见数据和隐藏数据,都将保存为 CSV。如果只想将工作表中的可见数据保存为 CSV,可以使用 XlsWorksheet->SaveToFile (LPCWSTR_S fileName, LPCWSTR_S separator, bool retainHiddenData) 方法。具体步骤如下:
#include "Spire.Xls.o.h";
using namespace Spire::Xls;
int main()
{
//初始化Workbook类的实例
Workbook* workbook = new Workbook();
//加载Excel文件
workbook->LoadFromFile(L"示例文档.xlsx");
//获取工作簿中的特定工作表
Worksheet* sheet = workbook->GetWorksheets()->Get(0);
//将工作表中的可见数据保存到CSV文件中
sheet->SaveToFile(L"ExcelToCsv1.csv", L",", false);
workbook->Dispose();
delete workbook;
}
要将 CSV 文件转换为 Excel,您需要使用 Workbook->LoadFromFile(LPCWSTR_S fileName, LPCWSTR_S separator) 方法加载 CSV 文件,然后使用 Workbook->SaveToFile (LPCWSTR_S fileName, ExcelVersion version) 方法将其保存到 Excel 文件。具体步骤如下:
#include "Spire.Xls.o.h";
using namespace Spire::Xls;
int main()
{
//初始化Workbook类的实例
Workbook* workbook = new Workbook();
//加载带有分隔符的CSV文件
workbook->LoadFromFile(L"ExcelToCSV.csv", L",");
//获取文件中的特定工作表
Worksheet* sheet = workbook->GetWorksheets()->Get(0);
//将忽略错误选项设置为将特定单元格区域中的数字保存为文本时忽略错误
sheet->GetRange(L"C2:C11")->SetIgnoreErrorOptions(IgnoreErrorType::NumberAsText);
//自动调整列宽
sheet->GetAllocatedRange()->AutoFitColumns();
//将文件保存到Excel XLSX文件
workbook->SaveToFile(L"CsvToExcel.xlsx", ExcelVersion::Version2013);
workbook->Dispose();
delete workbook;
}
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。