Specify how to cross a string in output PDF and image with JavaScript via C++
Possible Usage Scenarios
When a cell contains text that is larger than the width of the cell, the string overflows if the next cell in the subsequent column is null or empty. When you save your Excel file to PDF/Image, you can control this overflow by specifying the cross type using the TextCrossType enumeration. It has the following values:
-
TextCrossType.Default: Display text as in MS Excel, which depends on the next cell. If the next cell is empty, the string will cross; otherwise it will be truncated.
-
TextCrossType.CrossKeep: Display the string as MS Excel does when exporting to PDF/Image.
-
TextCrossType.CrossOverride: Display all the text by crossing other cells and overriding the text of the crossed cells.
-
TextCrossType.StrictInCell: Only display the string within the width of the cell.
Specify how to cross a string in output PDF/Image using TextCrossType
The following sample code loads the sample Excel file and saves it to PDF/Image format by specifying different TextCrossType. The sample Excel file and output files can be downloaded from the following links:
Sample Code
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Cross Text Type to PDF and Image</title>
</head>
<body>
<h1>Convert Excel to PDF and PNG (Text Cross Type)</h1>
<input type="file" id="fileInput" accept=".xls,.xlsx,.csv" />
<button id="runExample">Run Example</button>
<div>
<a id="downloadLinkPdf" style="display: none; margin-right: 10px;">Download PDF</a>
<a id="downloadLinkPng" style="display: none;">Download PNG</a>
</div>
<div id="result"></div>
</body>
<script src="aspose.cells.js.min.js"></script>
<script type="text/javascript">
const { Workbook, SaveFormat, PdfSaveOptions, ImageOrPrintOptions, SheetRender, TextCrossType, 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');
const downloadLinkPdf = document.getElementById('downloadLinkPdf');
const downloadLinkPng = document.getElementById('downloadLinkPng');
if (!fileInput.files.length) {
resultDiv.innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
// Clear previous links/messages
downloadLinkPdf.style.display = 'none';
downloadLinkPng.style.display = 'none';
resultDiv.innerHTML = '';
// Read file from input
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Initialize PDF save options
const pdfSaveOptions = new PdfSaveOptions();
// Set text cross type (converted setter → property)
pdfSaveOptions.textCrossType = TextCrossType.StrictInCell;
// Save PDF file data
const pdfData = workbook.save(SaveFormat.Pdf, pdfSaveOptions);
const pdfBlob = new Blob([pdfData], { type: 'application/pdf' });
downloadLinkPdf.href = URL.createObjectURL(pdfBlob);
downloadLinkPdf.download = 'outputCrossType.pdf';
downloadLinkPdf.style.display = 'inline-block';
downloadLinkPdf.textContent = 'Download PDF File';
// Initialize image or print options
const imageSaveOptions = new ImageOrPrintOptions();
// Set text cross type (converted setter → property)
imageSaveOptions.textCrossType = TextCrossType.StrictInCell;
// Initialize sheet renderer for first worksheet
const sheetRenderer = new SheetRender(workbook.worksheets.get(0), imageSaveOptions);
// Render the first page/image of the sheet to image data
const imageData = sheetRenderer.toImage(0);
const imageBlob = new Blob([imageData], { type: 'image/png' });
downloadLinkPng.href = URL.createObjectURL(imageBlob);
downloadLinkPng.download = 'outputCrossType.png';
downloadLinkPng.style.display = 'inline-block';
downloadLinkPng.textContent = 'Download PNG File';
resultDiv.innerHTML = '<p style="color: green;">Conversion completed! Download links are ready.</p>';
});
</script>
</html>