Working with PDF File Metadata using JavaScript via C++
Contents
[
Hide
]
Get PDF File Information
- Create a ‘FileReader’.
- The AsposePdfGetInfo function is executed.
- PDF metadata that can be obtained:
- title - title
- creator - creator
- author - author
- subject - subject
- keywords - keywords
- creation - creation date
- mod - modify date
- Next, if the ‘json.errorCode’ is 0, then you can get list of PDF file Info. If the ‘json.errorCode’ parameter is not equal to 0 and, accordingly, there will be an error in your file, then information about such an error will be contained in the ‘json.errorText’ file.
var ffilePdfGetInfo = function (e) {
const file_reader = new FileReader();
file_reader.onload = (event) => {
/*Get info (metadata) from PDF file.*/
const json = AsposePdfGetInfo(event.target.result, e.target.files[0].name);
/* JSON
title - title
creator - creator
author - author
subject - subject
keywords - keywords
format - PDF format
version - PDF version
ispdfa - PDF is PDF/A
ispdfua - PDF is PDF/UA
islinearized - PDF is linearized
isencrypted - PDF is encrypted
permission - PDF permission
size - PDF page size
pagecount - Page count
сreation Date: json.creation
modify Date: json.mod
annotationcount - Annotation count
bookmarkcount - Bookmark count
attachmentcount - Attachment count
metadatacount - Metadata count
javascriptcount - JavaScript count
imagecount - Image count
*/
if (json.errorCode == 0) document.getElementById('output').textContent = "JSON:\n" + JSON.stringify(json, null, 4);
else document.getElementById('output').textContent = json.errorText;
};
file_reader.readAsArrayBuffer(e.target.files[0]);
};
Using Web Workers
/*Create Web Worker*/
const AsposePDFWebWorker = new Worker("AsposePDFforJS.js");
AsposePDFWebWorker.onerror = evt => console.log(`Error from Web Worker: ${evt.message}`);
AsposePDFWebWorker.onmessage = evt => document.getElementById('output').textContent =
(evt.data == 'ready') ? 'loaded!' :
(evt.data.json.errorCode == 0) ?
`info:\n${JSON.stringify(evt.data.json, null, 4)}` :
`Error: ${evt.data.json.errorText}`;
/*Event handler*/
const ffilePdfGetInfo = e => {
const file_reader = new FileReader();
file_reader.onload = event => {
/*Get info (metadata) from a PDF-file - Ask Web Worker*/
AsposePDFWebWorker.postMessage(
{ "operation": 'AsposePdfGetInfo', "params": [event.target.result, e.target.files[0].name] },
[event.target.result]
);
};
file_reader.readAsArrayBuffer(e.target.files[0]);
};
Get All Fonts
Getting fonts from a PDF file can be a useful way to reuse fonts in other documents or applications.
In case you want to get all fonts from a PDF document, you can use AsposePdfGetAllFonts. Please check following code snippet in order to get all fonts from an existing PDF document using JavaScript via C++.
- Create a ‘FileReader’.
- The AsposePdfGetAllFonts function is executed.
- Next, if the ‘json.errorCode’ is 0, then you can get list of fonts from PDF file. If the ‘json.errorCode’ parameter is not equal to 0 and, accordingly, there will be an error in your file, then information about such an error will be contained in the ‘json.errorText’ file.
var ffilePdfGetAllFonts = function (e) {
const file_reader = new FileReader();
file_reader.onload = (event) => {
/*get list of fonts from PDF file.*/
const json = AsposePdfGetAllFonts(event.target.result, e.target.files[0].name);
if (json.errorCode == 0) document.getElementById('output').textContent = "JSON:\n" + JSON.stringify(json, null, 4);
else document.getElementById('output').textContent = json.errorText;
};
file_reader.readAsArrayBuffer(e.target.files[0]);
};
Using Web Workers
/*Create Web Worker*/
const AsposePDFWebWorker = new Worker("AsposePDFforJS.js");
AsposePDFWebWorker.onerror = evt => console.log(`Error from Web Worker: ${evt.message}`);
AsposePDFWebWorker.onmessage = evt => document.getElementById('output').textContent =
(evt.data == 'ready') ? 'loaded!' :
(evt.data.json.errorCode == 0) ?
`fonts:\n${JSON.stringify(evt.data.json.fonts, null, 4)}` :
`Error: ${evt.data.json.errorText}`;
/*Event handler*/
const ffilePdfGetAllFonts = e => {
const file_reader = new FileReader();
file_reader.onload = event => {
/*Get list fonts a PDF-file - Ask Web Worker*/
AsposePDFWebWorker.postMessage(
{ "operation": 'AsposePdfGetAllFonts', "params": [event.target.result, e.target.files[0].name] },
[event.target.result]
);
};
file_reader.readAsArrayBuffer(e.target.files[0]);
};
Set PDF File Information
Aspose.PDF for JavaScript via C++ allows you to set file-specific information for a PDF, information like author, creation date, subject, and title. To set this information:
- Create a ‘FileReader’.
- If not need to set value, use undefined or "" (empty string).
- The AsposePdfSetInfo function is executed.
- The name of the resulting file is set, in this example “ResultSetInfo.pdf”.
- Next, if the ‘json.errorCode’ is 0, then your DownloadFile is given the name you specified earlier. If the ‘json.errorCode’ parameter is not equal to 0 and, accordingly, there will be an error in your file, then information about such an error will be contained in the ‘json.errorText’ file.
- As a result, the DownloadFile function generates a link and allows you to download the resulting file to the user’s operating system.
var ffilePdfSetInfo = function (e) {
const file_reader = new FileReader();
file_reader.onload = (event) => {
/*Set PDF info: title, creator, author, subject, keywords, creation (date), mod (date modify)*/
/*If not need to set value, use undefined or "" (empty string)*/
/*Set info (metadata) in a PDF-file and save the "ResultSetInfo.pdf"*/
const json = AsposePdfSetInfo(event.target.result, e.target.files[0].name, "Setting PDF Document Information", "", "Aspose", undefined, "Aspose.Pdf, DOM, API", undefined, "16/02/2023 11:55 PM", "ResultSetInfo.pdf");
if (json.errorCode == 0) document.getElementById('output').textContent = json.fileNameResult;
else document.getElementById('output').textContent = json.errorText;
/*Make a link to download the result file*/
DownloadFile(json.fileNameResult, "application/pdf");
};
file_reader.readAsArrayBuffer(e.target.files[0]);
};
Using Web Workers
/*Create Web Worker*/
const AsposePDFWebWorker = new Worker("AsposePDFforJS.js");
AsposePDFWebWorker.onerror = evt => console.log(`Error from Web Worker: ${evt.message}`);
AsposePDFWebWorker.onmessage = evt => document.getElementById('output').textContent =
(evt.data == 'ready') ? 'loaded!' :
(evt.data.json.errorCode == 0) ?
`Result:\n${DownloadFile(evt.data.json.fileNameResult, "application/pdf", evt.data.params[0])}` :
`Error: ${evt.data.json.errorText}`;
/*Event handler*/
const ffilePdfSetInfo = e => {
const file_reader = new FileReader();
file_reader.onload = event => {
/*PDF info: title, creator, author, subject, keywords, creation (date), mod (date modify)*/
const title = 'Setting PDF Document Information';
const creator = ''; /*if not need to set value, use: undefined or ""/'' (empty string)*/
const author = 'Aspose';
const subject = undefined;
const keywords = 'Aspose.Pdf, DOM, API';
const creation = undefined; /*create date*/
const mod = '16/02/2023 11:55 PM'; /*modify date*/
/*Set info (metadata) in a PDF-file and save the "ResultSetInfo.pdf" - Ask Web Worker*/
AsposePDFWebWorker.postMessage(
{ "operation": 'AsposePdfSetInfo',
"params": [event.target.result, e.target.files[0].name, title, creator, author, subject, keywords, creation, mod, "ResultSetInfo.pdf"] },
[event.target.result]
);
};
file_reader.readAsArrayBuffer(e.target.files[0]);
};
/*Make a link to download the result file*/
const DownloadFile = (filename, mime, content) => {
mime = mime || "application/octet-stream";
var link = document.createElement("a");
link.href = URL.createObjectURL(new Blob([content], {type: mime}));
link.download = filename;
link.innerHTML = "Click here to download the file " + filename;
document.body.appendChild(link);
document.body.appendChild(document.createElement("br"));
return filename;
}
Remove PDF File Information
Aspose.PDF for JavaScript via C++ allows you to remove PDF file Metadata:
- Create a ‘FileReader’.
- The AsposePdfRemoveMetadata function is executed.
- The name of the resulting file is set, in this example “ResultPdfRemoveMetadata.pdf”.
- Next, if the ‘json.errorCode’ is 0, then your DownloadFile is given the name you specified earlier. If the ‘json.errorCode’ parameter is not equal to 0 and, accordingly, there will be an error in your file, then information about such an error will be contained in the ‘json.errorText’ file.
- As a result, the DownloadFile function generates a link and allows you to download the resulting file to the user’s operating system.
var ffilePdfRemoveMetadata = function (e) {
const file_reader = new FileReader();
file_reader.onload = (event) => {
/*Remove metadata a PDF-file and save the "ResultPdfRemoveMetadata.pdf"*/
const json = AsposePdfRemoveMetadata(event.target.result, e.target.files[0].name, "ResultPdfRemoveMetadata.pdf");
if (json.errorCode == 0) document.getElementById('output').textContent = json.fileNameResult;
else document.getElementById('output').textContent = json.errorText;
/*Make a link to download the result file*/
DownloadFile(json.fileNameResult, "application/pdf");
};
file_reader.readAsArrayBuffer(e.target.files[0]);
};
Using Web Workers
/*Create Web Worker*/
const AsposePDFWebWorker = new Worker("AsposePDFforJS.js");
AsposePDFWebWorker.onerror = evt => console.log(`Error from Web Worker: ${evt.message}`);
AsposePDFWebWorker.onmessage = evt => document.getElementById('output').textContent =
(evt.data == 'ready') ? 'loaded!' :
(evt.data.json.errorCode == 0) ? `Result:\n${DownloadFile(evt.data.json.fileNameResult, "application/pdf", evt.data.params[0])}` : `Error: ${evt.data.json.errorText}`;
/*Event handler*/
const ffilePdfRemoveMetadata = e => {
const file_reader = new FileReader();
file_reader.onload = event => {
/*Remove metadata a PDF-file and save the "ResultPdfRemoveMetadata.pdf" - Ask Web Worker*/
AsposePDFWebWorker.postMessage({ "operation": 'AsposePdfRemoveMetadata', "params": [event.target.result, e.target.files[0].name, "ResultPdfRemoveMetadata.pdf"] }, [event.target.result]);
};
file_reader.readAsArrayBuffer(e.target.files[0]);
};
/// [Code snippet]
/*Make a link to download the result file*/
const DownloadFile = (filename, mime, content) => {
mime = mime || "application/octet-stream";
var link = document.createElement("a");
link.href = URL.createObjectURL(new Blob([content], {type: mime}));
link.download = filename;
link.innerHTML = "Click here to download the file " + filename;
document.body.appendChild(link);
document.body.appendChild(document.createElement("br"));
return filename;
}