画像にレンダリングする前にデータから余分なスペースを削除する
Contents
[
Hide
]
時折、ワークシート画像をアプリケーションやWebページに表示する必要があります。たとえば、Wordドキュメント、PDFファイル、PowerPointプレゼンテーションなどに画像を挿入する必要があるかもしれません。基本的には、ワークシートを画像としてレンダリングし、他のアプリケーションに貼り付けられるようにしたいと思うでしょう。Aspose.CellsのAPIでは、Microsoft Excelワークシートを画像に変換することができます。
SheetRenderクラスは、画像形式、ページ化されたシートなどを含む任意の属性でワークシートを画像ファイルに変換する機能を備えています。BMP、GIF、JPG、TIFF、およびEMFなどのさまざまな画像形式がサポートされています。
シートを画像に変換する機能を使用すると、出力画像にはデフォルトでホワイト/ブランクスペース、つまり周囲にボーダーがあります。このボーダーを削除できます。ソースワークシートの上部、左部、下部、右部のページ設定マージンを0に設定し、それに応じてImageOrPrintOptions属性を指定します。
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-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(RemoveWhitespaceAroundData.class) + "TechnicalArticles/"; | |
// Instantiate a workbook | |
// Open the template file | |
Workbook book = new Workbook(dataDir + "book1.xlsx"); | |
// Get the first worksheet | |
Worksheet sheet = book.getWorksheets().get(0); | |
// Specify your print area if you want | |
// sheet.PageSetup.PrintArea = "A1:H8"; | |
// To remove the white border around the image. | |
sheet.getPageSetup().setLeftMargin(0); | |
sheet.getPageSetup().setRightMargin(0); | |
sheet.getPageSetup().setTopMargin(0); | |
sheet.getPageSetup().setBottomMargin(0); | |
// Define ImageOrPrintOptions | |
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions(); | |
imgOptions.setImageType(ImageType.EMF); | |
// Set only one page would be rendered for the image | |
imgOptions.setOnePagePerSheet(true); | |
imgOptions.setPrintingPage(PrintingPageType.IGNORE_BLANK); | |
// Create the SheetRender object based on the sheet with its | |
// ImageOrPrintOptions attributes | |
SheetRender render = new SheetRender(sheet, imgOptions); | |
// Convert the image | |
render.toImage(0, dataDir + "RWhitespaceAroundData_out.emf"); |