Working with PDF File Metadata in Node.js

Get PDF File Information

In case you want to get PDF file information, you can use AsposePdfGetInfo function. Please check the following code snippet in order to get PDF file information in Node.js environment.

CommonJS:

  1. Call require and import asposepdfnodejs module as AsposePdf variable.
  2. Specify the name of the PDF file from which the information will be extracted.
  3. Call AsposePdf as Promise and perform the operation for extracting info. Receive the object if successful.
  4. Call the function AsposePdfGetInfo.
  5. Extracted metadata is stored in the JSON object. Thus, if ‘json.errorCode’ is 0, the extracted metadata is displayed using console.log. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.

  const AsposePdf = require('asposepdfnodejs');
  const pdf_file = 'Aspose.pdf';
  AsposePdf().then(AsposePdfModule => {
      /*Get info (metadata) from a PDF-file*/
      const json = AsposePdfModule.AsposePdfGetInfo(pdf_file);
      /* JSON
        Title             : json.title
        Creator           : json.creator
        Author            : json.author
        Subject           : json.subject
        Keywords          : json.keywords
        Creation Date     : json.creation
        Modify Date       : json.mod
        PDF format        : json.format
        PDF version       : json.version
        PDF is PDF/A      : json.ispdfa
        PDF is PDF/UA     : json.ispdfua
        PDF is linearized : json.islinearized
        PDF is encrypted  : json.isencrypted
        PDF permission    : json.permission
        PDF page size     : json.size
        Page count        : json.pagecount
        Annotation count  : json.annotationcount
        Bookmark count    : json.bookmarkcount
        Attachment count  : json.attachmentcount
        Metadata count    : json.metadatacount
        JavaScript count  : json.javascriptcount
        Image count       : json.imagecount
      */
      console.log("AsposePdfGetInfo => %O", json.errorCode == 0 ? 'Title: ' + json.title : json.errorText);
  });

ECMAScript/ES6:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF file from which the information will be extracted.
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfGetInfo.
  5. Extracted metadata is stored in the JSON object. Thus, if ‘json.errorCode’ is 0, the extracted metadata is displayed using console.log. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.

    import AsposePdf from 'asposepdfnodejs';
    const AsposePdfModule = await AsposePdf();
    const pdf_file = 'Aspose.pdf';
    /*Get info (metadata) from a PDF-file*/
    const json = AsposePdfModule.AsposePdfGetInfo(pdf_file);
    /* JSON
      Title             : json.title
      Creator           : json.creator
      Author            : json.author
      Subject           : json.subject
      Keywords          : json.keywords
      Creation Date     : json.creation
      Modify Date       : json.mod
      PDF format        : json.format
      PDF version       : json.version
      PDF is PDF/A      : json.ispdfa
      PDF is PDF/UA     : json.ispdfua
      PDF is linearized : json.islinearized
      PDF is encrypted  : json.isencrypted
      PDF permission    : json.permission
      PDF page size     : json.size
      Page count        : json.pagecount
      Annotation count  : json.annotationcount
      Bookmark count    : json.bookmarkcount
      Attachment count  : json.attachmentcount
      Metadata count    : json.metadatacount
      JavaScript count  : json.javascriptcount
      Image count       : json.imagecount
    */
    console.log("AsposePdfGetInfo => %O", json.errorCode == 0 ? 'Title: ' + json.title : json.errorText);

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 fonts from a PDF file, you can use AsposePdfGetAllFonts function. Please check the following code snippet in order to get fonts from a PDF file in Node.js environment.

CommonJS:

  1. Call require and import asposepdfnodejs module as AsposePdf variable.
  2. Specify the name of the PDF file from which the fonts will be extracted.
  3. Call AsposePdf as Promise and perform the operation for extracting fonts. Receive the object if successful.
  4. Call the function AsposePdfGetAllFonts.
  5. Extracted fonts is stored in the JSON object. Thus, if ‘json.errorCode’ is 0, it displays an array of font details, including font name, whether it is embedded, and its accessibility statu using console.log. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.

  const AsposePdf = require('asposepdfnodejs');
  const pdf_file = 'Aspose.pdf';
  AsposePdf().then(AsposePdfModule => {
      /*Get list fonts from a PDF-file*/
      const json = AsposePdfModule.AsposePdfGetAllFonts(pdf_file);
      /*json.fonts - array of fonts: { fontName: <string>, isEmbedded: <boolean>, isAccessible: <boolean> }*/
      console.log("AsposePdfGetAllFonts => fonts: %O", json.errorCode == 0 ? json.fonts : json.errorText);
  });

ECMAScript/ES6:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF file from which the fonts will be extracted.
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfGetAllFonts.
  5. Extracted fonts is stored in the JSON object. Thus, if ‘json.errorCode’ is 0, it displays an array of font details, including font name, whether it is embedded, and its accessibility statu using console.log. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.

  import AsposePdf from 'asposepdfnodejs';
  const AsposePdfModule = await AsposePdf();
  const pdf_file = 'Aspose.pdf';
  /*Get list fonts from a PDF-file*/
  const json = AsposePdfModule.AsposePdfGetAllFonts(pdf_file);
  /*json.fonts - array of fonts: { fontName: <string>, isEmbedded: <boolean>, isAccessible: <boolean> }*/
  console.log("AsposePdfGetAllFonts => fonts: %O", json.errorCode == 0 ? json.fonts : json.errorText);

Set PDF File Information

Aspose.PDF for Node.js via C++ allows you to set file-specific information for a PDF, information like author, creation date, subject, and title. To set this information:

In case you want to set file-specific information, you can use AsposePdfSetInfo function. Please check the following code snippet in order to set file information in Node.js environment.

Possible to set:

  • title
  • creator
  • author
  • subject
  • list keywords
  • creation date
  • modify date
  • result file name

CommonJS:

  1. Call require and import asposepdfnodejs module as AsposePdf variable.
  2. Specify the name of the PDF file where the information will be set.
  3. Call AsposePdf as Promise and perform the operation. Receive the object if successful.
  4. Call the function AsposePdfSetInfo.
  5. Set PDF file information. Information such as title, creator, author, subject, keywords, creation date, and modification date are provided as parameters. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultSetInfo.pdf”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.

  const AsposePdf = require('asposepdfnodejs');
  const pdf_file = 'Aspose.pdf';
  AsposePdf().then(AsposePdfModule => {
      /*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 = AsposePdfModule.AsposePdfSetInfo(pdf_file, "Setting PDF Document Information", "", "Aspose", undefined, "Aspose.Pdf, DOM, API", undefined, "05/05/2023 11:55 PM", "ResultSetInfo.pdf");
      console.log("AsposePdfSetInfo => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
  });

ECMAScript/ES6:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF file where the information will be set.
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfSetInfo.
  5. Set PDF file information. Information such as title, creator, author, subject, keywords, creation date, and modification date are provided as parameters. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultSetInfo.pdf”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.

  import AsposePdf from 'asposepdfnodejs';
  const AsposePdfModule = await AsposePdf();
  const pdf_file = 'Aspose.pdf';
  /*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 = AsposePdfModule.AsposePdfSetInfo(pdf_file, "Setting PDF Document Information", "", "Aspose", undefined, "Aspose.Pdf, DOM, API", undefined, "05/05/2023 11:55 PM", "ResultSetInfo.pdf");
  console.log("AsposePdfSetInfo => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);

Remove PDF File Information

Aspose.PDF for Node.js via C++ allows you to remove PDF file Metadata:

In case you want to remove metadata from PDF, you can use AsposePdfRemoveMetadata function. Please check the following code snippet in order to remove metadata from PDF in Node.js environment.

CommonJS:

  1. Require the AsposePDFforNode.js module.
  2. Specify the name of the PDF file from which the information will be removed.
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfRemoveMetadata.
  5. Delete PDF file information. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfRemoveMetadata.pdf”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.

  const AsposePdf = require('asposepdfnodejs');
  const pdf_file = 'Aspose.pdf';
  AsposePdf().then(AsposePdfModule => {
      /*Remove metadata from a PDF-file and save the "ResultPdfRemoveMetadata.pdf"*/
      const json = AsposePdfModule.AsposePdfRemoveMetadata(pdf_file, "ResultPdfRemoveMetadata.pdf");
      console.log("AsposePdfRemoveMetadata => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);
  });

ECMAScript/ES6:

  1. Import the asposepdfnodejs module.
  2. Specify the name of the PDF file from which the information will be removed.
  3. Initialize the AsposePdf module. Receive the object if successful.
  4. Call the function AsposePdfRemoveMetadata.
  5. Delete PDF file information. Thus, if ‘json.errorCode’ is 0, the result of the operation is saved in “ResultPdfRemoveMetadata.pdf”. If the json.errorCode parameter is not 0 and, accordingly, an error appears in your file, the error information will be contained in ‘json.errorText’.

  import AsposePdf from 'asposepdfnodejs';
  const AsposePdfModule = await AsposePdf();
  const pdf_file = 'Aspose.pdf';
  /*Remove metadata from a PDF-file and save the "ResultPdfRemoveMetadata.pdf"*/
  const json = AsposePdfModule.AsposePdfRemoveMetadata(pdf_file, "ResultPdfRemoveMetadata.pdf");
  console.log("AsposePdfRemoveMetadata => %O", json.errorCode == 0 ? json.fileNameResult : json.errorText);