Imposta la proprietà DefaultFont di PdfSaveOptions e ImageOrPrintOptions per avere priorità con JavaScript via C++
Possibili Scenari di Utilizzo
Mentre si imposta la proprietà DefaultFont di PdfSaveOptions e ImageOrPrintOptions, potresti aspettarti che il salvataggio in PDF o immagine imposti quel DefaultFont a tutto il testo in un foglio di lavoro che ha un carattere mancante (non installato).
Generalmente, quando si salva in PDF o immagine, Aspose.Cells for JavaScript via C++ cercherà prima di impostare il font predefinito del Workbook (cioè, Workbook.DefaultStyle.Font). Se il font predefinito del workbook ancora non può mostrare/renderizzare correttamente il testo, allora Aspose.Cells proverà a rendere con il font menzionato contro l’attributo DefaultFont in PdfSaveOptions/ImageOrPrintOptions.
Per far fronte alle tue aspettative, abbiamo una proprietà booleana chiamata “CheckWorkbookDefaultFont” in PdfSaveOptions/ImageOrPrintOptions. Puoi impostarla su false per disabilitare il tentativo del carattere predefinito del foglio di lavoro o lasciare che l’impostazione DefaultFont in PdfSaveOptions/ImageOrPrintOptions abbia la priorità.
Impostare la proprietà DefaultFont di PdfSaveOptions/ImageOrPrintOptions
Il seguente esempio di codice apre un file Excel. La cella A1 (nel primo foglio di lavoro) contiene il testo “Christmas Time Font text”. Il nome del font è “Christmas Time Personal Use” che non è installato sulla macchina. Impostiamo l’attributo DefaultFont di PdfSaveOptions/ImageOrPrintOptions su “Times New Roman”. Impostiamo anche la proprietà booleana CheckWorkbookDefaultFont su “false” che garantisce che il testo della cella A1 venga reso con il font “Times New Roman” e non utilizzi il font di default del workbook (“Calibri” in questo caso). Il codice rende il primo foglio di lavoro in formati immagine PNG e TIFF. Infine, viene esportato come file PDF.
Questa è la schermata del file di modello utilizzato nel codice di esempio.

Questa è l’immagine PNG di output dopo aver impostato la proprietà ImageOrPrintOptions.defaultFont su “Times New Roman”.

Guarda l’immagine TIFF di output dopo aver impostato la proprietà ImageOrPrintOptions.defaultFont su “Times New Roman”.
Guarda il file PDF di output dopo aver impostato la proprietà PdfSaveOptions.defaultFont su “Times New Roman”.
Codice di Esempio
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example</title>
</head>
<body>
<h1>Set Default Font for Export (PNG, TIFF, PDF)</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<div>
<a id="downloadPng" style="display: none;">Download PNG</a><br/>
<a id="downloadTiff" style="display: none;">Download TIFF</a><br/>
<a id="downloadPdf" style="display: none;">Download PDF</a>
</div>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, ImageOrPrintOptions, ImageType, SheetRender, WorkbookRender, PdfSaveOptions } = AsposeCells;
const readyPromise = 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 resultEl = document.getElementById('result');
const fileInput = document.getElementById('fileInput');
if (!fileInput.files.length) {
resultEl.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
await readyPromise;
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Rendering to PNG while setting checkWorkbookDefaultFont = false and defaultFont to Times New Roman
const imgOpt = new ImageOrPrintOptions();
imgOpt.imageType = ImageType.Png;
imgOpt.checkWorkbookDefaultFont = false;
imgOpt.defaultFont = "Times New Roman";
const sr = new SheetRender(workbook.worksheets.get(0), imgOpt);
const pngData = sr.toImage(0);
const pngBlob = new Blob([pngData], { type: 'image/png' });
const downloadPng = document.getElementById('downloadPng');
downloadPng.href = URL.createObjectURL(pngBlob);
downloadPng.download = 'out1_imagePNG.png';
downloadPng.style.display = 'inline-block';
downloadPng.textContent = 'Download PNG';
// Rendering to TIFF while setting checkWorkbookDefaultFont = false and defaultFont to Times New Roman
imgOpt.imageType = ImageType.Tiff;
const wr = new WorkbookRender(workbook, imgOpt);
const tiffData = wr.toImage();
const tiffBlob = new Blob([tiffData], { type: 'image/tiff' });
const downloadTiff = document.getElementById('downloadTiff');
downloadTiff.href = URL.createObjectURL(tiffBlob);
downloadTiff.download = 'out1_imageTIFF.tiff';
downloadTiff.style.display = 'inline-block';
downloadTiff.textContent = 'Download TIFF';
// Rendering to PDF while setting the default font and checkWorkbookDefaultFont
const saveOptions = new PdfSaveOptions();
saveOptions.defaultFont = "Times New Roman";
saveOptions.checkWorkbookDefaultFont = false;
const pdfData = workbook.save(SaveFormat.Pdf, saveOptions);
const pdfBlob = new Blob([pdfData], { type: 'application/pdf' });
const downloadPdf = document.getElementById('downloadPdf');
downloadPdf.href = URL.createObjectURL(pdfBlob);
downloadPdf.download = 'out1_pdf.pdf';
downloadPdf.style.display = 'inline-block';
downloadPdf.textContent = 'Download PDF';
resultEl.innerHTML = '<p style="color: green;">Export completed. Click the links above to download the generated files.</p>';
});
</script>
</html>