C++経由でJavaScriptを使ってPDFに添付ファイルを埋め込む
Contents
[
Hide
]
Excelでは、ソースデータを持つOleオブジェクトを挿入できます(embedded-attachments-example.xlsx)。Oleオブジェクトをダブルクリックすると、埋め込まれたファイルが開きます。
一般的に、PDFに変換する際、OLEオブジェクトはアイコンやサムネイルとして表示され、OLEオブジェクトのソースデータは表示されません。オプションPdfSaveOptions.embedAttachments()を使用すると、OLEオブジェクトのソースデータを添付ファイルとしてPDFに埋め込むことができます。PDF内のアイコンまたはサムネイルをダブルクリックすると、OLEオブジェクトのソースファイルを開くことができます。
<!DOCTYPE html>
<html>
<head>
<title>Aspose.Cells Example - Embed Attachments to PDF</title>
</head>
<body>
<h1>Embed Attachments 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');
if (!fileInput.files.length) {
document.getElementById('result').innerHTML = '<p style="color: red;">Please select an Excel file.</p>';
return;
}
const file = fileInput.files[0];
const arrayBuffer = await file.arrayBuffer();
// Instantiating a Workbook object from the uploaded file
const workbook = new Workbook(new Uint8Array(arrayBuffer));
// Set to embed Ole Object attachment.
const pdfSaveOptions = new PdfSaveOptions();
pdfSaveOptions.embedAttachments = true;
// Save the pdf file with PdfSaveOptions
const outputData = workbook.save(SaveFormat.Pdf, pdfSaveOptions);
const blob = new Blob([outputData], { type: 'application/pdf' });
const downloadLink = document.getElementById('downloadLink');
downloadLink.href = URL.createObjectURL(blob);
downloadLink.download = 'output.pdf';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download PDF File';
document.getElementById('result').innerHTML = '<p style="color: green;">PDF generated successfully! Click the download link to get the file.</p>';
});
</script>
</html>
