使用JavaScript通过C++实现工作表的自定义纸张大小以进行渲染
Contents
[
Hide
]
可能的使用场景
MS Excel 没有直接创建自定义纸张大小的选项,但在将 Excel 文件渲染为 PDF 时,可以设置自定义纸张大小。本文介绍如何使用 Aspose.Cells API 设置工作表的自定义纸张大小。
实现工作表的自定义纸张大小以进行渲染
Aspose.Cells允许您实现工作表的自定义纸张大小。您可以使用PageSetup类的PageSetup.customPaperSize(number, number)方法指定自定义页面大小。以下示例代码演示了如何为工作簿中的第一个工作表指定自定义纸张大小。请同时参考用以下代码生成的输出PDF。
屏幕截图

示例代码
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Custom PDF Paper Size Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<a id="downloadLink" style="display: none;">Download Result</a>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, Utils } = AsposeCells;
AsposeCells.onReady({
license: "/lic/aspose.cells.enc",
fontPath: "/fonts/",
fontList: [
"arial.ttf",
"NotoSansSC-Regular.ttf"
]
}).then(() => {
console.log("Aspose.Cells initialized");
});
document.getElementById('runExample').addEventListener('click', async () => {
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
// If no file provided, create a new workbook as in the original JavaScript example
const wb = new Workbook();
// Access first worksheet
const ws = wb.worksheets.get(0);
// Set custom paper size in unit of inches
ws.pageSetup.customPaperSize(6, 4);
// Access cell B4
const b4 = ws.cells.get("B4");
// Add the message in cell B4
b4.value = "Pdf Page Dimensions: 6.00 x 4.00 in";
// Save the workbook in pdf format
const outputData = wb.save(SaveFormat.Pdf);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'outputCustomPaperSize.pdf';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download PDF File';
document.getElementById('result').innerHTML = '<p style="color: green;">PDF created successfully! Click the download link to get the file.</p>';
return;
}
// If a file is provided, open it and apply the same operations
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiate workbook from uploaded file
const wb = new Workbook(new Uint8Array(arrayBuffer));
// Access first worksheet
const ws = wb.worksheets.get(0);
// Set custom paper size in unit of inches
ws.pageSetup.customPaperSize(6, 4);
// Access cell B4
const b4 = ws.cells.get("B4");
// Add the message in cell B4
b4.value = "Pdf Page Dimensions: 6.00 x 4.00 in";
// Save the workbook in pdf format
const outputData = wb.save(SaveFormat.Pdf);
const blob = new Blob([outputData]);
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'outputCustomPaperSize.pdf';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download PDF File';
document.getElementById('result').innerHTML = '<p style="color: green;">PDF created successfully! Click the download link to get the file.</p>';
});
</script>
</html>