Alle Arbeitsblattspalten auf einer einzigen PDF Seite anzeigen mit JavaScript via C++
Contents
[
Hide
]
Manchmal möchten Sie eine PDF-Datei generieren, die alle Spalten eines Arbeitsblatts auf einer Seite passt. Die PdfSaveOptions.allColumnsInOnePagePerSheet-Eigenschaft bietet diese Funktion auf sehr benutzerfreundliche Weise. Komplexe Berechnungen wie die Höhe und Breite der Ausgabe-PDF werden intern behandelt und basieren auf den Daten im Arbeitsblatt.
Arbeitsblattsäulen auf eine einzelne PDF-Seite anpassen
PdfSaveOptions.allColumnsInOnePagePerSheet stellt sicher, dass alle Spalten in einem Arbeitsblatt auf einer einzigen PDF-Seite dargestellt werden, auch wenn Zeilen je nach Dateninhalt auf mehrere Seiten erweitert werden können.
Der folgende Beispielcode zeigt, wie die PdfSaveOptions.allColumnsInOnePagePerSheet-Eigenschaft verwendet wird, um ein großes Arbeitsblatt mit 100 Spalten zu rendern.
<!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>
Wenn ein bestimmtes Arbeitsblatt viele Spalten hat, kann die gerenderte PDF-Datei den Inhalt in sehr kleiner Größe anzeigen. Es ist immer noch lesbar, wenn es in einer Anzeige-Anwendung wie Acrobat Reader skaliert wird.
Wenn Ihre Tabelle Formeln enthält, ist es am besten, Workbook.calculateFormula() kurz vor dem Rendern der Tabelle im PDF-Format aufzurufen. Auf diese Weise wird sichergestellt, dass die von Formeln abhängigen Werte neu berechnet und die richtigen Werte im PDF gerendert werden.