IXLSRange接口下提供了getStyle() 方法和setStyle() 方法,用来获取或设置特定单元格(区域)的样式。要将格式从一个单元格复制到另一个单元格,请先获取样式,然后将其应用到另一个单元格。
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
public class CopyCellFormatting {
public static void main(String[] args) {
//创建Workbook对象
Workbook workbook = new Workbook();
//加载示例文档
workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");
//获取第一个工作表
Worksheet sheet = workbook.getWorksheets().get(0);
//获取被使用的行数
int rowCount = sheet.getRows().length;
//遍历行
for (int i = 1; i < rowCount + 1; i++)
{
//获取指定单元格的样式并应用到另一个单元格
sheet.getRange().get(String.format("C%d",i)).setStyle(sheet.getRange().get(String.format("A%d",i)).getStyle());
}
//保存文档
workbook.saveToFile("output/CopyFormatting.xlsx", ExcelVersion.Version2016);
}
}

经常有客户反馈,在Windows系统中,使用Spire组件转换文档时,结果文档正常,但是在Linux系统中,结果文档会出现乱码、方框或程序抛出字体缺失异常如:Cannot found font[SansSerif] installed on the system。
这个问题是由于Linux服务器缺少字体所导致。Spire组件转换文档时需要从系统中寻找对应的字体绘制字形,如果系统中没有安装原文档中所使用的字体,会先从系统已安装的字体中匹配能支持这个字形的字体,如果无法匹配出相应的字体,结果文档就会出现乱码或方框,或者程序会抛出字体缺失异常。若您遇到了这类问题,请先确认原文档中使用了哪些字体,然后将这些字体或Windows中的所有字体安装到您的Linux系统后,问题会得到解决。
以下是如何查看和安装字体的详细指令。
1、开启 root 权限
输入指令: su root 不行时,需要先输入指令: sudo passwd root
根据步骤输入密码,然后再重新输入指令: su root
2、将字体文件先放到某一个文件夹下
然后进入文件夹输入指令: cp simsun.ttc /usr/share/fonts/ ,依次输入下面的指令:
mkfontscale //字体扩展
mkfontdir //新增字体目录
fc-cache-fv //刷新缓存
3、查看安装的字体文件
查看系统中的字体:
指令:fc-list
查看系统中的中文字体:
指令:fc-list:lang=zh
如果按照上述步骤安装字体后,问题依然存在,请及时联系我们:该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。 或致电 028-81705109。
本文介绍如何使用Spire.XLS for Java来压缩Excel中的图片。
import com.spire.xls.*;
public class CompressImgs {
public static void main(String[] args) {
Workbook wb = new Workbook();
wb.loadFromFile("test.xlsx");//加载Excel测试文档
//遍历工作表
for(int s = 0; s < wb.getWorksheets().getCount(); s++)
{
Worksheet sheet = wb.getWorksheets().get(s);
//遍历工作表中的图片
for(int p = 0; p < sheet.getPictures().getCount(); p++)
{
ExcelPicture pic = sheet.getPictures().get(p);
pic.compress(40);//压缩图片
}
}
//保存文档
wb.saveToFile("CompressImgs.xlsx",FileFormat.Version2013);
wb.dispose();
}
}
压缩前后,可查看文档大小效果对比:

本文介绍如何使用Spire.Presentation for Java更改PowerPoint中文本的字体样式,包括字体名称,字体大小,字体颜色,粗体,斜体等。
import com.spire.presentation.*;
import com.spire.presentation.drawing.FillFormatType;
import java.awt.*;
public class ChangeFontStyles {
public static void main(String[] args) throws Exception {
//创建Presentation对象
Presentation presentation = new Presentation();
//加载示例文档
presentation.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.pptx");
//获取文本形状
IAutoShape shape = (IAutoShape) presentation.getSlides().get(0).getShapes().get(0);
//获取第一个段落并更改字体颜色
ParagraphEx paragraph = shape.getTextFrame().getParagraphs().get(0);
for (int i = 0; i < paragraph.getTextRanges().getCount(); i++) {
PortionEx textRange = paragraph.getTextRanges().get(i);
textRange.getFormat().getFill().setFillType(FillFormatType.SOLID);
textRange.getFormat().getFill().getSolidColor().setColor(Color.blue);
}
//获取第三个段落并将文字加粗,设置斜体和下划线
paragraph = shape.getTextFrame().getParagraphs().get(2);
for (int i = 0; i < paragraph.getTextRanges().getCount(); i++) {
PortionEx textRange = paragraph.getTextRanges().get(i);
textRange.getFormat().isBold(TriState.TRUE);
textRange.getFormat().isItalic(TriState.TRUE);
textRange.getFormat().setTextUnderlineType(TextUnderlineType.DASHED);
}
//获取第五个段落并更改字体名称及大小
paragraph = shape.getTextFrame().getParagraphs().get(4);
for (int i = 0; i < paragraph.getTextRanges().getCount(); i++) {
PortionEx textRange = paragraph.getTextRanges().get(i);
textRange.getFormat().setEastAsianFont(new TextFont("黑体"));
textRange.getFormat().setFontHeight(22f);
}
//保存文档
presentation.saveToFile("output/ChangeFontStyles.pptx", FileFormat.PPTX_2013);
}
}

Spire.PDFViewer 6.8.6已发布。本次更新带了些新功能,比如ASP.NET控件支持旋转页面,支持控件支持查找文本并高亮。同时,该版本还修复了PDF文档显示相关等问题。详情请阅读以下内容。
新功能:
input type=button id=PdfDocumentViewer1_btnRotate onfocus=this.blur() onclick=pdfdocumentviewer1.Rotate(pdfdocumentviewer1.RotateAngle + 270); value=rotate_270
input type=text id=PdfDocumentViewer1_txtFindText onkeydown=if(event.keyCode==13){pdfdocumentviewer1.SearchText(this.value,true);return false;} value=
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.PdfDocumentViewer1.FindTextHighLightColor = System.Drawing.Color.FromArgb(125, 255, 0, 0);
this.PdfDocumentViewer1.LoadFromFile(input.pdf);
}
}
问题修复:
本文将介绍如何使用Spire.Presentation for .NET在PowerPoint文档中使用正则表达式替换文本。
using Spire.Presentation;
using System.Text.RegularExpressions;
namespace ReplaceTextWithRegex
{
class Program
{
static void Main(string[] args)
{
//创建Presentation实例
Presentation ppt = new Presentation();
//加载示例文档
ppt.LoadFromFile("Sample.pptx");
//获取第一张幻灯片
ISlide slide = ppt.Slides[0];
//替换该幻灯片中所有“ABC”以及其后到行尾的内容为“ABC def”
Regex regex = new Regex("ABC.*");
string newvalue = "ABC def";
foreach (IShape shape in slide.Shapes)
{
shape.ReplaceTextWithRegex(regex, newvalue);
}
//保存结果文档
ppt.SaveToFile("ReplaceTextWithRegex.pptx", FileFormat.Pptx2013);
}
}
}
Imports Spire.Presentation
Imports System.Text.RegularExpressions
Namespace ReplaceTextWithRegex
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'创建Presentation实例
Dim ppt As Presentation = New Presentation()
'加载示例文档
ppt.LoadFromFile("Sample.pptx")
'获取第一张幻灯片
Dim slide As ISlide = ppt.Slides(0)
'替换该幻灯片中所有“ABC”以及其后到行尾的内容为“ABC def”
Dim regex As Regex = New Regex("ABC.*")
Dim newvalue As String = "ABC def"
For Each shape As IShape In slide.Shapes
shape.ReplaceTextWithRegex(regex, newvalue)
Next
'保存结果文档
ppt.SaveToFile("ReplaceTextWithRegex.pptx", FileFormat.Pptx2013)
End Sub
End Class
End Namespace
原PowerPoint文档:

结果文档:

OFD 即 Open Fixed-layout Document,是我国国家版式文档格式标准。OFD 文档具有便携性、开放性、扩展性的特点。OFD 文件小,可压缩比率大,且呈现效果与设备无关。在各种设备上阅读、打印或印刷时可达到版面固定、不跑版的效果。在电子商务、电子公务、信息发布等领域都有广泛应用。在本文中,您将学习如何在 C# 和 VB.NET 中使用 Spire.PDF for .NET 将 PDF 转换为 OFD 文档。
首先,您需要添加 Spire.PDF for .NET 包中包含的 DLL 文件作为 .NET 项目中的引用。DLL 文件可以从此链接下载或通过 NuGet 安装。
PM> Install-Package Spire.PDF详细步骤如下:
using Spire.Pdf;
namespace PDFtoOFD
{
class Program
{
static void Main(string[] args)
{
//创建PdfDocument 类的对象
PdfDocument pdf = new PdfDocument();
//加载PDF文档
pdf.LoadFromFile("成都大运会竞技赛公告.pdf");
//保存为OFD格式
pdf.SaveToFile("ToOFD.ofd", FileFormat.OFD);
}
}
}Imports Spire.Pdf
Namespace PDFtoOFD
Class Program
Private Shared Sub Main(ByVal args() As String)
'创建PdfDocument 类的对象
Dim pdf As PdfDocument = New PdfDocument
'加载PDF文档
pdf.LoadFromFile("成都大运会竞技赛公告.pdf")
'保存为OFD格式
pdf.SaveToFile("ToOFD.ofd", FileFormat.OFD)
End Sub
End Class
End Namespace
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
Spire.Doc 9.8.6已发布。该版本增强了转换Word/RTF到PDF的功能,以及转换HTML到Word的功能。此外,本次更新还修复了加载Word文档时出现的问题。详情请阅读以下内容。
问题修复:
Spire.Office 6.8.2已发布。本次更新带来了许多出色的新功能,比如:Spire.Presentation支持加载保存 DPS/DPT 格式的文档,支持设置PPT中图表的边框样式为直角,同时还支持设置图表坐标轴的刻度线间隔;Spire.PDF 支持转换 PDF 到 OFD,支持给数字签名添加有效性检查标记,以及支持添加不可见的数字签名的功能;Spire.XLS 支持转换 SmartArt/Shape 为图片;Spire.Barcode 支持获取条码在图片中的位置和条码类型等等。此外,该版本还修复了大量已知的问题。详情请阅读以下内容。
该版本涵盖了最新版的Spire.Doc, Spire.PDF, Spire.XLS, Spire.Presentation, Spire.Email, Spire.DocViewer, Spire.PDFViewer, Spire.Spreadsheet, Spire.OfficeViewer, Spire.DataExport, Spire.Barcode。
版本信息如下:
https://www.e-iceblue.cn/Downloads/Spire-Office-NET.html
问题修复:
新功能:
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(PdfFile);
doc.SaveToFile(OfdFile, FileFormat.OFD
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(inputPath);
PdfCertificate cert = new PdfCertificate(pfxPath, password);
PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(doc, cert);
signatureMaker.SetAcro6Layers(false);
signatureMaker.MakeSignature("signName", doc.Pages[0], 100, 100, 120, 60);
doc.SaveToFile(outputPath);
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(inputPath);
PdfCertificate cert = new PdfCertificate(pfxPath, password);
PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(doc, cert);
PdfSignature signature = signatureMaker.Signature;
signature.Name = "E-iceblue";
signature.ContactInfo = "028-81705109";
signature.Location = "Chengdu";
signature.Reason = "The certificate of this document";
PdfSignatureAppearance appearance = new PdfSignatureAppearance(signature);
appearance.NameLabel = "Signer: ";
appearance.ContactInfoLabel = "ContactInfo: ";
appearance.LocationLabel = "Location: ";
appearance.ReasonLabel = "Reaseon: ";
appearance.SignatureImage = PdfImage.FromFile(imagePath);
appearance.GraphicMode = GraphicMode.SignImageAndSignDetail;
signatureMaker.MakeSignature("signName", doc.Pages[0], 100, 600, 200, 100, appearance);
doc.SaveToFile(outputPath);
PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(doc, x509);
signatureMaker.MakeSignature("signName");
问题修复:
新功能:
ppt.SaveToFile(outputPath + filename + ".dps", FileFormat.Dps);
ppt.SaveToFile(outputPath + filename + ".dpt", FileFormat.Dpt);
IChart chart = ppt.Slides[0].Shapes[0] as IChart;
ITrendlines trendline = chart.Series[0].TrendLines[0] as ITrendlines;
foreach(TextParagraph para in trendline.TrendLineLabel.TextFrameProperties.Paragraphs)
{
para.DefaultCharacterProperties.FontHeight = 20;
foreach(TextRange range in para.TextRanges)
{
range.FontHeight = 20;
}
}
trendline.TrendLineLabel.OffsetX = -0.1f;
trendline.TrendLineLabel.OffsetY = 0.1f;
IChart chart = ppt.Slides[0].Shapes[0] as IChart;
chart.Line.FillFormat.FillType = FillFormatType.Solid;
chart.Line.FillFormat.SolidFillColor.Color = Color.Red;
chart.BorderRoundedCorners = false;
Regex regex = new Regex("^[A-Za-z]+$");
string newvalue = "new string";
foreach(IShape shape in ppt.Slides[0].Shapes)
{
shape.ReplaceTextWithRegex(regex, newvalue);
}
Presentation ppt = new Presentation();
ppt.LoadFromFile(intputFile);
IChart chart = ppt.Slides[0].Shapes[0] as IChart;
IChartAxis chartAxis = chart.PrimaryCategoryAxis;
chartAxis.TickMarkSpacing = 30;
ppt.SaveToFile(outputFile, FileFormat.Pptx2013);
FileStream from_stream = File.OpenRead(inputFile);
Presentation presentation = new Presentation();
presentation.LoadFromStream(from_stream,FileFormat.Auto, "12345");
presentation.SaveToFile(outputFile_px, FileFormat.Pptx2013);
问题修复:
新功能:
Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");
Worksheet sheet = workbook.Worksheets[0];
SaveShapeTypeOption shapelist = new SaveShapeTypeOption();
List images = sheet.SaveShapesToImage(shapelist);
int index = 0;
foreach (System.Drawing.Image img in images)
{
img.Save("toImage" + index + ".Png", ImageFormat.Png);
index++;
}
Workbook wb = new Workbook();
wb.LoadFromFile(etInputFile);
//wb.LoadFromFile(ettInputFile);
wb.SaveToFile(etOutputFile, FileFormat.ET);
//wb.SaveToFile(ettOutputFile, FileFormat.ETT);
问题修复:
新功能:
BarcodeInfo[] barcodeInfos = BarcodeScanner.ScanInfo(imageFile);
//BarcodeInfo[] barcodeInfos = BarcodeScanner.ScanInfo(imageFile, barCodeType);
for (int i = 0; i < barcodeInfos.Length; i++)
{
//获取条码位置
BarCodeReadType barCodeReadType = barcodeInfos[i].BarCodeReadType;
//获取条码四个顶点的坐标
Point[] vertexes = barcodeInfos[i].Vertexes;
}
问题修复:
问题修复:
Spire.PDF 7.8.8已发布。本次更新支持转换PDF到OFD,支持给数字签名添加有效性检查标记。此外,该版本还增强了转换PDF到SVG/图片/PDFA1B,以及XPS到PDF的功能。详情请阅读以下内容。
新功能:
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(PdfFile);
doc.SaveToFile(OfdFile, FileFormat.OFD
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(inputPath);
PdfCertificate cert = new PdfCertificate(pfxPath, password);
PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(doc, cert);
signatureMaker.SetAcro6Layers(false);
signatureMaker.MakeSignature("signName", doc.Pages[0], 100, 100, 120, 60);
doc.SaveToFile(outputPath);
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(inputPath);
PdfCertificate cert = new PdfCertificate(pfxPath, password);
PdfOrdinarySignatureMaker signatureMaker = new PdfOrdinarySignatureMaker(doc, cert);
PdfSignature signature = signatureMaker.Signature;
signature.Name = "E-iceblue";
signature.ContactInfo = "028-81705109";
signature.Location = "Chengdu";
signature.Reason = "The certificate of this document";
PdfSignatureAppearance appearance = new PdfSignatureAppearance(signature);
appearance.NameLabel = "Signer: ";
appearance.ContactInfoLabel = "ContactInfo: ";
appearance.LocationLabel = "Location: ";
appearance.ReasonLabel = "Reaseon: ";
appearance.SignatureImage = PdfImage.FromFile(imagePath);
appearance.GraphicMode = GraphicMode.SignImageAndSignDetail;
signatureMaker.MakeSignature("signName", doc.Pages[0], 100, 600, 200, 100, appearance);
doc.SaveToFile(outputPath);
问题修复: