加载指定打印纸张大小的工作簿
Contents
[
Hide
]
您可以使用 LoadOptions.SetPaperSize() 方法在加载工作簿时指定所需的打印纸张大小。请注意,如果你在MS Excel中创建一个新文件,你会发现纸张大小与你的机器上默认打印机的设置相同。
以下示例代码说明了 LoadOptions.SetPaperSize() 方法的用法。它首先创建一个工作簿,然后以XLSX格式保存到内存流中。然后在A5纸张大小下加载它并以PDF格式保存。然后再以A3纸张大小加载它,并再次以PDF格式保存。如果你打开输出的PDF并检查它们的纸张大小,你会发现它们是不同的。一个是A5,另一个是A3。请下载代码生成的 A5输出PDF 和 A3输出PDF 供参考。
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 a sample workbook and add some data inside the first worksheet | |
Workbook workbook = new Workbook(); | |
Worksheet worksheet = workbook.Worksheets[0]; | |
worksheet.Cells["P30"].PutValue("This is sample data."); | |
// Save the workbook in memory stream | |
MemoryStream ms = new MemoryStream(); | |
workbook.Save(ms, SaveFormat.Xlsx); | |
ms.Position = 0; | |
// Now load the workbook from memory stream with A5 paper size | |
LoadOptions opts = new LoadOptions(LoadFormat.Xlsx); | |
opts.SetPaperSize(PaperSizeType.PaperA5); | |
workbook = new Workbook(ms, opts); | |
// Save the workbook in pdf format | |
workbook.Save(dataDir + "LoadWorkbookWithPrinterSize-a5_out.pdf"); | |
// Now load the workbook again from memory stream with A3 paper size | |
ms.Position = 0; | |
opts = new LoadOptions(LoadFormat.Xlsx); | |
opts.SetPaperSize(PaperSizeType.PaperA3); | |
workbook = new Workbook(ms, opts); | |
// Save the workbook in pdf format | |
workbook.Save(dataDir + "LoadWorkbookWithPrinterSize-a3_out.pdf"); |