在将电子表格呈现为图像时设置默认字体
Contents
[
Hide
]
请使用ImageOrPrintOptions.DefaultFont属性将默认字体设置为渲染电子表格为图像时的默认字体。仅当工作簿的默认字体无法呈现您的字符时,才会使用ImageOrPrintOptions.DefaultFont属性指定的默认字体。指定的默认字体将用于所有缺少或不存在的字体的单元格。
渲染电子表格为图像时设置默认字体
下面的示例代码创建一个工作簿,在第一个工作表的A4单元格中添加一些文本,并将其字体设置为无效或不存在的字体。然后,它会获取工作表的两个图像。第一张图片是通过将ImageOrPrintOptions.DefaultFont属性设置为Courier New获取的,第二张图片是通过将ImageOrPrintOptions.DefaultFont属性设置为Times New Roman获取的。
将ImageOrPrintOptions.DefaultFont属性设置为Courier New后的输出图像。
将ImageOrPrintOptions.DefaultFont属性设置为Times New Roman后的输出图像。
示例代码
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create workbook object. | |
Workbook wb = new Workbook(); | |
// Set default font of the workbook to none | |
Style s = wb.DefaultStyle; | |
s.Font.Name = ""; | |
wb.DefaultStyle = s; | |
// Access first worksheet. | |
Worksheet ws = wb.Worksheets[0]; | |
// Access cell A4 and add some text inside it. | |
Cell cell = ws.Cells["A4"]; | |
cell.PutValue("This text has some unknown or invalid font which does not exist."); | |
// Set the font of cell A4 which is unknown. | |
Style st = cell.GetStyle(); | |
st.Font.Name = "UnknownNotExist"; | |
st.Font.Size = 20; | |
st.IsTextWrapped = true; | |
cell.SetStyle(st); | |
// Set first column width and fourth column height | |
ws.Cells.SetColumnWidth(0, 80); | |
ws.Cells.SetRowHeight(3, 60); | |
// Create image or print options. | |
ImageOrPrintOptions opts = new ImageOrPrintOptions(); | |
opts.OnePagePerSheet = true; | |
opts.ImageType = Drawing.ImageType.Png; | |
// Render worksheet image with Courier New as default font. | |
opts.DefaultFont = "Courier New"; | |
SheetRender sr = new SheetRender(ws, opts); | |
sr.ToImage(0, "out_courier_new_out.png"); | |
// Render worksheet image again with Times New Roman as default font. | |
opts.DefaultFont = "Times New Roman"; | |
sr = new SheetRender(ws, opts); | |
sr.ToImage(0, "times_new_roman_out.png"); |