Repository: Hopding/pdf-lib (ts)

Issue:
[Feature request] Add PDFDocument.setLanguage method
This method should set the language on the PDF document as described in the following specification: https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf

More specifically, it should add an entry named `Lang` in the catalog.

Pull request description:
resolves #258 add PDFDocument.setLanguage method

Patch:
diff --git a/src/api/PDFDocument.ts b/src/api/PDFDocument.ts
index 19a35c6..e2ec917 100644
--- a/src/api/PDFDocument.ts
+++ b/src/api/PDFDocument.ts
@@ -294,6 +294,25 @@ export default class PDFDocument {
     this.getInfoDict().set(key, PDFHexString.fromText(producer));
   }
 
+  /**
+   * Set this document's language metadata. The language will appear in the
+   * "Document Properties" section of most PDF readers. For example:
+   * ```js
+   * pdfDoc.setLanguage('en-us')
+   * ```
+   *
+   * A language identifier shall either be the empty text string,
+   * to indicate that the language is unknown,
+   * or a Language-Tag as defined in RFC 3066.
+   *
+   * @param language The language of this document.
+   */
+  setLanguage(language: string): void {
+    assertIs(language, 'language', ['string']);
+    const key = PDFName.of('Lang');
+    this.catalog.set(key, PDFString.of(language));
+  }
+
   /**
    * Set this document's creation date metadata. The creation date will appear
    * in the "Document Properties" section of most PDF readers. For example:


Test patch:
diff --git a/tests/api/PDFDocument.spec.ts b/tests/api/PDFDocument.spec.ts
index 88bd999..30a3e42 100644
--- a/tests/api/PDFDocument.spec.ts
+++ b/tests/api/PDFDocument.spec.ts
@@ -3,6 +3,7 @@ import {
   EncryptedPDFError,
   ParseSpeeds,
   PDFDocument,
+  PDFName,
   PDFPage,
 } from 'src/index';
 
@@ -120,6 +121,24 @@ describe(`PDFDocument`, () => {
     });
   });
 
+  describe(`setLanguage() method`, () => {
+    let pdfDoc: PDFDocument;
+    beforeAll(async () => {
+      const parseSpeed = ParseSpeeds.Fastest;
+      pdfDoc = await PDFDocument.load(unencryptedPdfBytes, { parseSpeed });
+    });
+
+    it(`sets the language of the document`, () => {
+      expect(pdfDoc.catalog.get(PDFName.of('Lang'))).toBe(undefined);
+      pdfDoc.setLanguage('fr-FR');
+      expect(String(pdfDoc.catalog.get(PDFName.of('Lang')))).toBe('(fr-FR)');
+      pdfDoc.setLanguage('en');
+      expect(String(pdfDoc.catalog.get(PDFName.of('Lang')))).toBe('(en)');
+      pdfDoc.setLanguage('');
+      expect(String(pdfDoc.catalog.get(PDFName.of('Lang')))).toBe('()');
+    });
+  });
+
   describe(`getPageCount() method`, () => {
     let pdfDoc: PDFDocument;
     beforeAll(async () => {