TIFF 因其高质量和支持多种色彩空间而成为扫描和存档中常用的图像格式。另一方面,PDF 在压缩文件大小的同时保留了文档的布局和格式,因此被广泛用于文档交换。这些格式之间的转换可用于各种目的,如存档、编辑或共享文档。
在本文中,您将学习如何使用 Spire.PDF for Python 在 Python 程序中将 PDF 转换为 TIFF 和将 TIFF 转换为 PDF。
本教程需要用到 Spire.PDF for Python 和 plum-dispatch v1.7.4。可以通过以下 pip 命令将它们轻松安装到 Windows 中。
pip install Spire.PDF
如果您不确定如何安装,请参考此教程:如何在 Windows 中安装 Spire.PDF for Python
要完成 PDF 到 TIFF 的转换,首先需要加载 PDF 文档,并使用 Spire.PDF 将单个页面转换为图像流。然后,使用 PIL 库的功能将这些图像流合并在一起,得到一个合并的 TIFF 图像。
以下是使用 Python 将 PDF 转换为 TIFF 的步骤。
from spire.pdf.common import *
from spire.pdf import *
from PIL import Image
from io import BytesIO
# 创建一个PdfDocument对象
doc = PdfDocument()
# 加载PDF文档
doc.LoadFromFile("输入文档.pdf")
# 创建一个空列表,用于存储从PDF中提取的图片
images = []
# 遍历PDF文档中的每一页
for i in range(doc.Pages.Count):
# 使用SaveAsImage方法将PDF的当前页保存为图片
with doc.SaveAsImage(i) as imageData:
# 将imageData中的图片数据转换为字节流,然后使用PIL库中的Image.open方法打开这个字节流作为图片
img = Image.open(BytesIO(imageData.ToArray()))
# 将打开的图片添加到images列表中
images.append(img)
# 将所有图片保存为一个多页的TIFF文件
images[0].save("ToTIFF.tiff", save_all=True, append_images=images[1:])
# 释放PdfDocument对象占用的资源
doc.Dispose()

在 PIL 库的帮助下,您可以加载 TIFF 文件并将每一帧转换为不同的 PNG 文件。然后,您可以使用 Spire.PDF 将这些 PNG 文件绘制到 PDF 文档的页面上。
要使用 Python 将 TIFF 图像转换为 PDF 文档,请按照以下步骤操作。
from spire.pdf import *
from spire.pdf.common import *
from PIL import Image
import io
# 创建一个 PdfDocument 对象
doc = PdfDocument()
# 设置页边距为 0
doc.PageSettings.SetMargins(0.0)
# 加载 TIFF 图像
tiff_image = Image.open("TIFF.tiff")
# 遍历其中的frame
for i in range(getattr(tiff_image, 'n_frames', 1)):
# 转到当前frame
tiff_image.seek(i)
# 提取当前frame的图像
frame_image = tiff_image.copy()
# 将图像保存为 PNG 文件
frame_image.save(f"temp/output_frame_{i}.png")
# 将图像文件加载到 PdfImage 中
image = PdfImage.FromFile(f"temp/output_frame_{i}.png")
# 获取图像宽度和高度
width = image.PhysicalDimension.Width
height = image.PhysicalDimension.Height
# 向文档添加页面
page = doc.Pages.Add(SizeF(width, height))
# 在页面的 (0, 0) 处绘制图像
page.Canvas.DrawImage(image, 0.0, 0.0, width, height)
# 将文档保存为 PDF 文件
doc.SaveToFile("结果文件.pdf",FileFormat.PDF)
doc.Dispose()

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
复制行、列或单元格是 Excel 中非常有用的功能,它能帮助你快捷高效地对数据进行重复利用。在创建具有相似架构的表格,或需要在工作簿的多个地方使用相同内容时,这项功能尤为实用。通过直接复制行、列或单元格,你可以大幅提升工作效率,同时也能减少人工数据录入可能产生的错误。这篇文章将介绍如何使用 Spire.XLS for Python 和 Python 在 Excel 中复制行、列和单元格。
本方案需要 Spire.XLS for Python 和 plum-dispatch v1.7.4。您可以通过以下 pip 命令将它轻松安装到 Windows 中。
pip install Spire.XLS
如果您不清楚如何安装,请参考此教程: 如何在 Windows 中安装 Spire.XLS for Python
你可以使用 Spire.XLS for Python 提供的 Worksheet.CopyRow(sourceRow, destSheet, destRowIndex, copyOptions) 方法轻松地在同一个工作表内或不同的工作表之间复制行。具体步骤如下:
from spire.xls import *
from spire.xls.common import *
# 创建Workbook对象
workbook = Workbook()
# 加载Excel文件
workbook.LoadFromFile("示例.xlsx")
# 获取源工作表
sheet1 = workbook.Worksheets[0]
# 获取目标工作表
sheet2 = workbook.Worksheets[1]
# 获取要复制的行
row = sheet1.Rows[0]
# 将源工作表中的行复制到目标工作表的第一行
sheet1.CopyRow(row, sheet2, 1, CopyRangeOptions.All)
columns = sheet1.Columns.Length
# 将源行中的单元格宽度复制到目标行相应的单元格
for i in range(columns):
column_width = row.Columns[i].ColumnWidth
sheet2.Rows[0].Columns[i].ColumnWidth = column_width
# 将工作簿保存到文件
workbook.SaveToFile("复制行.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

要复制工作表中的列,可以使用 Worksheet.CopyColumn(sourceColumn, destSheet, destColIndex, copyOptions) 方法。具体步骤如下:
from spire.xls import *
from spire.xls.common import *
# 创建Workbook对象
workbook = Workbook()
# 加载Excel文件
workbook.LoadFromFile("示例.xlsx")
# 获取源工作表
sheet1 = workbook.Worksheets[0]
# 获取目标工作表
sheet2 = workbook.Worksheets[1]
# 获取要复制的列
column = sheet1.Columns[0]
# 将源工作表中的列复制到目标工作表的第一列
sheet1.CopyColumn(column, sheet2, 1, CopyRangeOptions.All)
rows = column.Rows.Length
# 将源列中单元格的行高复制到目标列的对应单元格
for i in range(rows):
row_height = column.Rows[i].RowHeight
sheet2.Columns[0].Rows[i].RowHeight = row_height
# 将工作簿保存到文件
workbook.SaveToFile("复制列.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

除了复制整行和整列,你还可以使用 CellRange.Copy(destRange, copyOptions) 方法复制单个单元格或单元格区域。具体步骤如下:
from spire.xls import *
from spire.xls.common import *
# 创建Workbook对象
workbook = Workbook()
# 加载Excel文件
workbook.LoadFromFile("示例.xlsx")
# 获取源工作表
sheet1 = workbook.Worksheets[0]
# 获取目标工作表
sheet2 = workbook.Worksheets[1]
# 获取源单元格区域
range1 = sheet1.Range["A1:F7"]
# 获取目标单元格区域
range2 = sheet2.Range["A1:F7"]
# 将源单元格区域从源工作表复制到目标工作表的目标单元格区域
range1.Copy(range2, CopyRangeOptions.All)
# 将源单元格区域的行高和列宽复制到目标单元格区域
for i, row in enumerate(range1.Rows):
for j, column in enumerate(row.Columns):
range2.Rows[i].Columns[j].ColumnWidth = column.ColumnWidth
range2.Rows[i].RowHeight = row.RowHeight
# 将工作簿保存到文件
workbook.SaveToFile("复制单元格.xlsx", ExcelVersion.Version2016)
workbook.Dispose()

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
添加和删除 PDF 文档中的水印在文档管理、版权保护、信息安全等领域扮演着重要角色。水印可以是一种视觉标记,如公司 Logo、版权信息或“机密”字样,用于表明文档的来源、状态或所有权。添加水印可以增强文档的版权保护,防止未经授权的复制和分发。同时,在某些情况下,如当文档被合法购买或授权使用后,可能需要删除水印以提供更清晰的阅读体验或进一步的编辑权限。本文将介绍如何使用 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>
Spire.PDF 提供了一个通过 PdfWatermarkAnnotation 对象向 PDF 页面添加水印的方法。值得注意的是,采用此方法添加的水印,日后可以被轻易地移除。以下是主要的关键步骤:
import com.spire.pdf.*;
import com.spire.pdf.annotations.*;
import com.spire.pdf.annotations.appearance.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;
public class AddWatermarkInPDF {
public static void main(String[] args) {
// 创建PdfDocument对象
PdfDocument pdfDocument = new PdfDocument();
// 从文件加载PDF文档
pdfDocument.loadFromFile("示例.pdf");
// 设置字体样式,这里选择"微软雅黑",字号为22
Font font = new Font("微软雅黑", java.awt.Font.PLAIN, 22);
// 创建PdfTrueTypeFont对象用于后续的文本渲染
PdfTrueTypeFont trueTypeFont = new PdfTrueTypeFont(font);
// 获取页面对象
PdfPageBase page;
// 定义水印文本
String watermarkAnnotationText = "ID_0";
// 创建尺寸对象
Dimension2D dimension2D = new Dimension();
// 创建矩形对象
Rectangle2D loRect = new Rectangle2D.Float();
// 遍历PDF中的每一页
for (int i = 0; i < pdfDocument.getPages().getCount(); i++) {
// 获取当前页
page = pdfDocument.getPages().get(i);
// 设置尺寸对象大小为当前页的大小
dimension2D.setSize(page.getClientSize().getWidth(), page.getClientSize().getHeight());
// 设置矩形对象框架,即整个页面的范围
loRect.setFrame(new Point2D.Float(0, 0), dimension2D);
// 创建PdfTemplate对象,用于绘制水印
PdfTemplate template = new PdfTemplate(page.getClientSize().getWidth(),page.getClientSize().getHeight());
// 插入水印
insertWatermark(template,trueTypeFont,"禁止修改");
// 创建PdfWatermarkAnnotation对象,定义水印位置
PdfWatermarkAnnotation watermarkAnnotation=new PdfWatermarkAnnotation(loRect);
// 创建PdfAppearance对象,设置水印外观
PdfAppearance appearance=new PdfAppearance(watermarkAnnotation);
// 设置水印的正常状态模板
appearance.setNormal(template);
// 将外观设置到水印对象中
watermarkAnnotation.setAppearance(appearance);
// 设置水印文本
watermarkAnnotation.setText(watermarkAnnotationText);
// 设置水印打印矩阵,用于控制水印的位置和大小
watermarkAnnotation.getFixedPrint().setMatrix(new float[]{1,0,0,1,0,0});
// 设置水平偏移量
watermarkAnnotation.getFixedPrint().setHorizontalTranslation(0.5f);
// 设置垂直偏移量
watermarkAnnotation.getFixedPrint().setVerticalTranslation(0.5f);
// 将水印添加到页面的注释小部件中
page.getAnnotationsWidget().add(watermarkAnnotation);
}
// 保存PDF文档至文件
pdfDocument.saveToFile("添加水印.pdf");
// 关闭并释放PDF文档资源
pdfDocument.dispose();
}
// 定义一个静态方法用于向PDF模板中插入水印
static void insertWatermark(PdfTemplate template, PdfTrueTypeFont font, String watermark) {
// 创建一个Dimension2D对象,用于设置水印刷子的大小
Dimension2D dimension2D = new Dimension();
// 设置水印刷子的大小为模板宽度和高度的一半和三分之一
dimension2D.setSize(template.getWidth() / 2, template.getHeight() / 3);
// 创建一个PdfTilingBrush对象,用于重复填充水印图案
PdfTilingBrush brush = new PdfTilingBrush(dimension2D);
// 设置水印的透明度为0.3
brush.getGraphics().setTransparency(0.3F);
// 开始一组图形状态保存
brush.getGraphics().save();
// 平移图形上下文,使其中心与水印刷子的中心对齐
brush.getGraphics().translateTransform((float) brush.getSize().getWidth() / 2, (float) brush.getSize().getHeight() / 2);
// 旋转图形上下文,使水印倾斜45度
brush.getGraphics().rotateTransform(-45);
// 在图形上下文中绘制水印文字,使用指定的字体、颜色和居中对齐方式
brush.getGraphics().drawString(watermark, font, PdfBrushes.getViolet(), 0, 0, new PdfStringFormat(PdfTextAlignment.Center));
// 结束一组图形状态保存并恢复
brush.getGraphics().restore();
// 重置水印透明度为1,即完全不透明
brush.getGraphics().setTransparency(1);
// 创建一个Rectangle2D对象,用于定义水印的填充区域
Rectangle2D loRect = new Rectangle2D.Float();
// 设置水印的填充区域为整个模板的大小
loRect.setFrame(new Point2D.Float(0, 0), template.getSize());
// 使用水印刷子在模板上绘制水印
template.getGraphics().drawRectangle(brush, loRect);
}
}

Spire.PDF 可以移除通过 PdfWatermarkAnnotation 对象向 PDF 页面添加的水印。以下是详细的步骤:
import com.spire.pdf.*;
import com.spire.pdf.annotations.*;
public class RemoveWatermarkFromPDF {
public static void main(String[] args) {
// 创建PdfDocument对象
PdfDocument pdfDocument = new PdfDocument();
// 从文件加载PDF文档
pdfDocument.loadFromFile("水印.pdf");
// 定义一个字符串id,用于匹配和删除特定的水印
String id = "ID_0";
// 遍历PDF文档中的每一页
for (int i = 0; i < pdfDocument.getPages().getCount(); i++) {
// 获取当前页面的所有注释
PdfAnnotationCollection annotationWidget = pdfDocument.getPages().get(i).getAnnotationsWidget();
// 遍历当前页面的所有注释
for (int j = 0; j < annotationWidget.getCount(); j++) {
// 检查当前注释是否为水印注释
if (annotationWidget.get(j) instanceof PdfWatermarkAnnotationWidget) {
// 如果水印文本与id相等,则删除该水印
if (annotationWidget.get(j).getText().equals(id)) {
annotationWidget.remove(annotationWidget.get(j));
}
}
}
}
// 将修改后的PDF文档保存到新文件
pdfDocument.saveToFile("移除水印.pdf");
// 释放PdfDocument对象资源
pdfDocument.dispose();
}
}

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
Spire.Doc for Java 12.6.2 现已正式发布。该版本支持在 Word 转换过程中找不到相应字体时,给出提示消息的功能。同时还修复了一些在 Word 转 PDF 和 HTML 转 Word 时出现的问题。详情请查阅以下内容。
新功能:
Document doc = ConvertUtil.GetNewEngineDocument();
doc.loadFromFile(input);
HandleDocumentSubstitutionWarnings substitutionWarningHandler = new HandleDocumentSubstitutionWarnings();
doc.setWarningCallback(substitutionWarningHandler);
doc.saveToFile(output_1);
StringBuilder sb = new StringBuilder();
Iterator iterator = substitutionWarningHandler.FontWarnings.iterator();
while(iterator.hasNext()){
System.out.println(((WarningInfo)iterator.next()).getDescription());
}
String s = substitutionWarningHandler.FontWarnings.get(0).getDescription();
WarningSource warningSource = substitutionWarningHandler.FontWarnings.get(0).getSource();
substitutionWarningHandler.FontWarnings.clear();
class HandleDocumentSubstitutionWarnings implements IWarningCallback
{
public void warning(WarningInfo info) {
if(info.getWarningType() == WarningType.Font_Substitution)
FontWarnings.warning(info);
}
public WarningInfoCollection FontWarnings = new WarningInfoCollection();
}问题修复:
Spire.XLS for Java 14.6.2 已发布。本次更新增强了 Excel 到图片的转换功能。此外,一些已知问题也在该版本中得到修复,如设置 "autoFitColumns()" 效果不正确的问题。详情请阅读以下内容。
问题修复:
表格是 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 提供了 Table.TableFormat.Borders 属性,支持获取表格的边框集合。获取后,你可以从集合中访问特定的边框(如顶部边框、底部边框、左边框、右边框、水平边框和垂直边框),然后修改它们的线条样式、宽度和颜色。具体步骤如下:
from spire.doc import *
from spire.doc.common import *
# 初始化一个 Document 类的实例
document = Document()
# 加载Word文档
document.LoadFromFile("表格.docx")
# 获取第一个节
section = document.Sections.get_Item(0)
# 获取第一个表格
table = section.Tables[0] if isinstance(section.Tables[0], Table) else None
# 获取表格边框集合
borders = table.TableFormat.Borders
# 获取顶部边框并更改边框样式、线宽和颜色
topBorder = borders.Top
topBorder.BorderType = BorderStyle.Single
topBorder.LineWidth = 1.0
topBorder.Color = Color.get_YellowGreen()
# 获取左侧边框并更改边框样式、线宽和颜色
leftBorder = borders.Left
leftBorder.BorderType = BorderStyle.Single
leftBorder.LineWidth = 1.0
leftBorder.Color = Color.get_YellowGreen()
# 获取右侧边框并更改边框样式、线宽和颜色
rightBorder = borders.Right
rightBorder.BorderType = BorderStyle.Single
rightBorder.LineWidth = 1.0
rightBorder.Color = Color.get_YellowGreen()
# 获取底部边框并更改边框样式、线宽和颜色
bottomBorder = borders.Bottom
bottomBorder.BorderType = BorderStyle.Single
bottomBorder.LineWidth = 1.0
bottomBorder.Color = Color.get_YellowGreen()
# 获取水平边框并更改边框样式、线宽和颜色
horizontalBorder = borders.Horizontal
horizontalBorder.BorderType = BorderStyle.Dot
horizontalBorder.LineWidth = 1.0
horizontalBorder.Color = Color.get_Orange()
# 获取垂直边框并更改边框样式、线宽和颜色
verticalBorder = borders.Vertical
verticalBorder.BorderType = BorderStyle.Dot
verticalBorder.LineWidth = 1.0
verticalBorder.Color = Color.get_CornflowerBlue()
# 保存结果文档
document.SaveToFile("修改边框.docx", FileFormat.Docx2013)
document.Close()

要删除表格的边框,需要将边框的 BorderType 属性设置为 BorderStyle.none。具体步骤如下:
from spire.doc import *
from spire.doc.common import *
# 初始化一个 Document 类的实例
document = Document()
document.LoadFromFile("修改边框.docx")
# 添加一个段落到文档
section = document.Sections.get_Item(0)
# 获取第一个表格
table = section.Tables.get_Item(0) if isinstance(section.Tables[0], Table) else None
# 获取表格边框集合
borders = table.TableFormat.Borders
# 删除顶部边框
topBorder = borders.Top
topBorder.BorderType = BorderStyle.none
# 删除左侧边框
leftBorder = borders.Left
leftBorder.BorderType = BorderStyle.none
# 删除右侧边框
rightBorder = borders.Right
rightBorder.BorderType = BorderStyle.none
# 删除底部边框
bottomBorder = borders.Bottom
bottomBorder.BorderType = BorderStyle.none
# 删除水平边框
horizontalBorder = borders.Horizontal
horizontalBorder.BorderType = BorderStyle.none
# 删除垂直边框
verticalBorder = borders.Vertical
verticalBorder.BorderType = BorderStyle.none
# 保存结果文档
document.SaveToFile("删除边框.docx", FileFormat.Docx2013)
document.Close()

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
在 Word 文档中,灵活地添加、修改和移除表格边框能够显著提升文档的可读性和专业度。首先,定制边框样式可以让重要信息更加突出,帮助读者快速定位关键数据或段落,增强视觉效果。其次,通过调整边框线的粗细、颜色和样式,可以实现更精细的设计控制,使文档风格统一且美观。最后,移除不必要的边框有助于减少视觉杂乱,让页面布局更加简洁明了,提高阅读体验。本文将介绍如何使用 Spire.Doc for Java 在 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>14.9.5</version>
</dependency>
</dependencies>
为了给整个表格中的所有单元格统一添加边框,需要逐一访问每个单元格,并对其边框属性进行可视化设置。以下是详细的步骤:
import com.spire.doc.*;
import com.spire.doc.documents.*;
import java.awt.*;
public class AddBorder {
public static void main(String[] args) {
// 创建一个新的Document对象
Document doc = new Document();
// 从文件加载文档
doc.loadFromFile("表格示例1.docx");
// 获取文档的第一个节
Section section = doc.getSections().get(0);
// 获取该节中第一个表格
Table table = section.getTables().get(0);
// 声明TableRow和TableCell变量,以便在循环中使用
TableRow tableRow;
TableCell tableCell;
// 遍历表格的所有行
for (int i = 0; i < table.getRows().getCount(); i++) {
// 获取当前行
tableRow = table.getRows().get(i);
// 遍历当前行的所有单元格
for (int j = 0; j < tableRow.getCells().getCount(); j++) {
// 获取当前单元格
tableCell = tableRow.getCells().get(j);
// 设置当前单元格的边框样式为单线
tableCell.getCellFormat().getBorders().setBorderType(BorderStyle.Single);
// 设置边框的宽度
tableCell.getCellFormat().getBorders().setLineWidth(1f);
// 设置边框的颜色
tableCell.getCellFormat().getBorders().setColor(Color.black);
}
}
// 将修改后的文档保存为新的文件
doc.saveToFile("添加单元格边框.docx", FileFormat.Docx_2016);
// 关闭文档,释放资源
doc.close();
}
}

Spire.Doc 赋予用户丰富的边框自定义选项,通过调整如 TableCell.getCellFormat().getBorders().getBottom().setBorderType() 来选择边框样式,TableCell.getCellFormat().getBorders().getBottom().setLineWidth() 以设定边框厚度,以及 TableCell.getCellFormat().getBorders().getBottom().setColor() 来指定边框色彩,可以根据个人或项目需求,精细化设计文档中表格的边框细节。以下是详细的步骤:
import com.spire.doc.*;
import com.spire.doc.documents.*;
import java.awt.*;
public class ModifyBorder {
public static void main(String[] args) {
// 创建一个新的Document对象
Document doc = new Document();
// 从文件加载文档
doc.loadFromFile("表格示例2.docx");
// 获取文档的第一个节
Section section = doc.getSections().get(0);
// 获取该节中第一个表格
Table table = section.getTables().get(0);
// 声明TableRow,以便在循环中使用
TableRow tableRow;
// 遍历表格的所有行
for (int i = 1; i < table.getRows().getCount() - 1; i++) {
tableRow = table.getRows().get(i);
// 设置当前单元格的边框颜色
tableRow.getCells().get(1).getCellFormat().getBorders().getBottom().setColor(Color.ORANGE);
// 设置当前单元格的边框样式为点线
tableRow.getCells().get(1).getCellFormat().getBorders().getBottom().setBorderType(BorderStyle.Dot_Dash);
// 设置边框的宽度
tableRow.getCells().get(1).getCellFormat().getBorders().getBottom().setLineWidth(2);
}
// 将修改后的文档保存为新的文件
doc.saveToFile("修改单元格边框.docx", FileFormat.Docx_2016);
// 关闭文档,释放资源
doc.close();
}
}

在编辑 Word 文档时,边框设计的灵活性不仅限于整个表格层面,更延伸至每个独立单元格,允许进行细致入微的个性化调整。若目标为全面消除表格内外的所有边框痕迹,推荐采用分阶段实施的方法:第一步,从宏观角度出发,清除表格整体的边框样式;紧接着,进入微观操作阶段,遍历表格中的每一个单元格,逐一撤销其特有的边框设置。以下是详细的步骤:
import com.spire.doc.*;
import com.spire.doc.documents.*;
public class RemoveBorder {
public static void main(String[] args) {
// 创建一个新的Document对象
Document doc = new Document();
// 从文件加载文档
doc.loadFromFile("表格示例2.docx");
// 获取文档的第一个节
Section section = doc.getSections().get(0);
// 获取该节中第一个表格
Table table = section.getTables().get(0);
// 移除表格设置的边框
table.getFormat().getBorders().setBorderType(BorderStyle.None);
// 声明TableRow,以便在循环中使用
TableRow tableRow;
// 遍历表格的所有行
for (int i = 0; i < table.getRows().getCount(); i++) {
tableRow = table.getRows().get(i);
for (int j = 0; j < tableRow.getCells().getCount(); j++) {
// 移除单元格设置的所有边框
tableRow.getCells().get(j).getCellFormat().getBorders().setBorderType(BorderStyle.None);
}
}
// 将修改后的文档保存为新的文件
doc.saveToFile("移除单元格边框.docx", FileFormat.Docx_2016);
// 关闭文档,释放资源
doc.close();
}
}

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
Spire.PDF for Python 10.6.1 现已正式发布。该版本支持对比两个 PDF 文档,并修复了一些已知问题,如获取图片坐标时程序抛异常。详情请查阅以下内容。
新功能:
pdf1 = PdfDocument()
pdf1.LoadFromFile(inputFile_1)
pdf2 = PdfDocument()
pdf2.LoadFromFile(inputFile_2)
comparer = PdfComparer(pdf1, pdf2)
comparer.Compare(outputFile)问题修复:
Spire.PDF 10.6.7 已发布。本次更新新增支持转换 PDF 文档到 Markdown 格式。详情请阅读以下内容。
新功能:
PdfDocument doc = new PdfDocument("input.pdf");
doc.SaveToFile("output.md", FileFormat.Markdown);
设置 Word 表格列宽是优化文档可读性和美观性的关键。合理的列宽避免了长行文字影响阅读,尤其在文本密集的表格中。Word 提供了两种方法:百分比和固定值。用百分比值的设置可以让表格适应不同屏幕,保持内容整齐,提升阅读体验。用固定值的设置则可以精确地实现了表格结构控制,确保一致性与专业性,适合严格规定数据对齐或复杂布局的设计。本文将介绍如何使用 Spire.Doc for .NET 在 C# 项目中基于百分比与固定值的设置方法来设置 Word 表格列宽。
首先,您需要将 Spire.Doc for.NET 包含的 DLL 文件作为引用添加到您的 .NET 项目中。DLL 文件可以从此链接下载,也可以通过 NuGet 安装。
PM> Install-Package Spire.Doc
使用百分比值设置 Word 表格列宽时需先对表格设置下百分比宽度类型,Table.PreferredWidth = new PreferredWidth(WidthType.Percentage, (short)100),然后依次遍历每个列设置相同或不同的百分比值的宽度。以下是详细的步骤:
using Spire.Doc;
namespace SpireDocDemo
{
internal class Program
{
static void Main(string[] args)
{
// 创建一个新的Document对象
Document doc = new Document();
// 加载名为"示例.docx"的文档
doc.LoadFromFile("示例.docx");
// 获取文档的第一个Section
Section section = doc.Sections[0];
// 将Section中的第一个Table转换为Table类型
Table table = (Table)section.Tables[0];
// 创建一个PreferredWidth对象,设置宽度类型为百分比,并设置宽度值为100%
PreferredWidth percentageWidth = new PreferredWidth(WidthType.Percentage, (short)100);
// 设置Table的首选宽度为上面创建的PreferredWidth对象
table.PreferredWidth = percentageWidth;
// 定义一个TableRow类型的变量
TableRow tableRow;
// 遍历Table的所有行
for (int i = 0; i < table.Rows.Count; i++)
{
// 获取当前行
tableRow = table.Rows[i];
// 设置第一列单元格的宽度为34%,类型为百分比
tableRow.Cells[0].SetCellWidth(34, CellWidthType.Percentage);
// 设置第二列单元格的宽度为33%,类型为百分比
tableRow.Cells[1].SetCellWidth(33, CellWidthType.Percentage);
// 设置第三列单元格的宽度为33%,类型为百分比
tableRow.Cells[2].SetCellWidth(33, CellWidthType.Percentage);
}
// 保存修改后的文档,并指定文件格式为Docx2016
doc.SaveToFile("以百分比值设置列宽.docx", FileFormat.Docx2016);
// 关闭文档
doc.Close();
}
}
}

使用固定值设置 Word 表格列宽时需先对表格设置固定布局,Table.TableFormat.LayoutType = LayoutType.Fixed,然后依次遍历每个列设置相同或不同的固定值的宽度。以下是详细的步骤:
using Spire.Doc;
namespace SpireDocDemo
{
internal class Program
{
static void Main(string[] args)
{
// 创建一个新的Document对象
Document doc = new Document();
// 加载名为"示例.docx"的文档
doc.LoadFromFile("示例.docx");
// 获取文档的第一个Section
Section section = doc.Sections[0];
// 将Section中的第一个Table转换为Table类型
Table table = (Table)section.Tables[0];
// 设置表格布局类型为固定
table.TableFormat.LayoutType = LayoutType.Fixed;
// 设置表格大小调整方式为非自动
table.TableFormat.IsAutoResized = false;
// 获取左页边距
float leftMargin = section.PageSetup.Margins.Left;
// 获取右页边距
float rightMargin = section.PageSetup.Margins.Right;
// 计算页面宽度减去左右边距
double pageWidth = section.PageSetup.PageSize.Width - leftMargin - rightMargin;
// 定义一个TableRow类型的变量
TableRow tableRow;
// 遍历Table的所有行
for (int i = 0; i < table.Rows.Count; i++)
{
// 获取当前行
tableRow = table.Rows[i];
// 设置第一列单元格宽度为页面宽度的34%
tableRow.Cells[0].SetCellWidth((float)(pageWidth * 0.34), CellWidthType.Point);
// 设置第二列单元格宽度为页面宽度的33%
tableRow.Cells[1].SetCellWidth((float)(pageWidth * 0.33), CellWidthType.Point);
// 设置第三列单元格宽度为页面宽度的33%
tableRow.Cells[2].SetCellWidth((float)(pageWidth * 0.33), CellWidthType.Point);
}
// 保存修改后的文档,并指定文件格式为Docx2016
doc.SaveToFile("以固定值设置列宽.docx", FileFormat.Docx2016);
// 关闭文档
doc.Close();
}
}
}

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