用JavaScript通过C++将所有工作表列适应在单个PDF页面上
Contents
[
Hide
]
有时候,您希望生成一个PDF文件,将工作表的所有列适合一页。PdfSaveOptions.allColumnsInOnePagePerSheet属性以一种非常易于使用的方式提供了这种功能。输出PDF的高度和宽度等复杂计算是在内部处理的,是基于工作表中的数据。
使工作表列适合单个PDF页面
PdfSaveOptions.allColumnsInOnePagePerSheet确保工作表中的所有列都呈现在单一PDF页面上,尽管行可能会根据工作表中的数据展开至多多页。
下面的示例代码显示了如何使用PdfSaveOptions.allColumnsInOnePagePerSheet属性呈现一个包含100列的大型工作表。
<!DOCTYPE html>
<html>
<head>
<title>Save Workbook to PDF Example</title>
</head>
<body>
<h1>Save Workbook to PDF Example</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx" />
<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, PdfSaveOptions, 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');
const resultDiv = document.getElementById('result');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Create and initialize an instance of Workbook
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Create and initialize an instance of PdfSaveOptions
const saveOptions = new PdfSaveOptions();
// Set AllColumnsInOnePagePerSheet to true (converted from setter to property)
saveOptions.allColumnsInOnePagePerSheet = true;
// Save Workbook to PDF format by passing the object of PdfSaveOptions
const outputData = workbook.save(SaveFormat.Pdf, saveOptions);
const blob = new Blob([outputData], { type: "application/pdf" });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.out.pdf';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download PDF File';
resultDiv.innerHTML = '<p style="color: green;">PDF created successfully! Click the download link to get the file.</p>';
});
</script>
</html>
当给定的工作表有很多列时,渲染的PDF文件可能以非常小的尺寸显示内容。在类似Acrobat Reader的查看应用程序中缩放后仍然可读。
如果您的电子表格包含公式,最好在将电子表格呈现为PDF格式之前调用 Workbook.calculateFormula()。这样做将确保重新计算依赖于公式的值,并在PDF中呈现正确的值。