スプレッドシートを画像にレンダリングする際のデフォルト フォントの設定
Contents
[
Hide
]
をご利用くださいImageOrPrintOptions.DefaultFontプロパティを使用して、スプレッドシートを画像にレンダリングする際のデフォルト フォントを設定します。このプロパティは、ワークブックの既定のフォントが文字をレンダリングできない場合にのみ有効です。で指定されたデフォルトのフォントImageOrPrintOptions.DefaultFontプロパティは、無効または存在しないフォントを持つすべてのセルに使用されます。
スプレッドシートを画像にレンダリングする際のデフォルト フォントの設定
次のサンプル コードは、ワークブックを作成し、最初のワークシートのセル A4 にテキストを追加し、そのフォントを無効または存在しないフォントに設定します。次に、ワークシートの 2 つの画像を取得します。最初の画像は、ImageOrPrintOptions.DefaultFontプロパティへクーリエ 新規 番目の画像は、ImageOrPrintOptions.DefaultFontプロパティへタイムズ ニュー ローマン.
これは、設定後の出力イメージです。ImageOrPrintOptions.DefaultFontプロパティへクーリエ 新規.
これは、設定後の出力イメージです。ImageOrPrintOptions.DefaultFontプロパティへタイムズ ニュー ローマン.
This file contains hidden or 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-Java | |
// Directory path where output HTML files are to be saved | |
String dataDir = Utils.getSharedDataDir(SetDefaultFontWhileRenderingSpreadsheetToImages.class) + "Conversion/"; | |
//Create workbook object. | |
Workbook wb = new Workbook(); | |
//Set default font of the workbook to none | |
Style s = wb.getDefaultStyle(); | |
s.getFont().setName(""); | |
wb.setDefaultStyle(s); | |
//Access first worksheet. | |
Worksheet ws = wb.getWorksheets().get(0); | |
//Access cell A4 and add some text inside it. | |
Cell cell = ws.getCells().get("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.getFont().setName("UnknownNotExist"); | |
st.getFont().setSize(20); | |
st.setTextWrapped(true); | |
cell.setStyle(st); | |
//Set first column width and fourth column height | |
ws.getCells().setColumnWidth(0, 80); | |
ws.getCells().setRowHeight(3, 60); | |
//Create image or print options. | |
ImageOrPrintOptions opts = new ImageOrPrintOptions(); | |
opts.setOnePagePerSheet(true); | |
opts.setImageFormat(ImageFormat.getPng()); | |
//Render worksheet image with Courier New as default font. | |
opts.setDefaultFont("Courier New"); | |
SheetRender sr = new SheetRender(ws, opts); | |
sr.toImage(0, dataDir + "out_courier_new.png"); | |
//Render worksheet image again with Times New Roman as default font. | |
opts.setDefaultFont("Times New Roman"); | |
sr = new SheetRender(ws, opts); | |
sr.toImage(0, dataDir + "out_times_new_roman.png"); |