MCPcopy Create free account
hub / github.com/LibPDF-js/core / main

Function main

examples/06-metadata/document-dates.ts:13–144  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

11import { formatBytes, loadFixture, saveOutput } from "../utils";
12
13async function main() {
14 console.log("Working with document dates...\n");
15
16 // === Reading dates from existing PDFs ===
17 console.log("=== Reading Dates from Existing PDF ===\n");
18
19 const existingBytes = await loadFixture("basic", "rot0.pdf");
20 const existingPdf = await PDF.load(existingBytes);
21
22 const existingCreation = existingPdf.getCreationDate();
23 const existingModification = existingPdf.getModificationDate();
24
25 console.log("Existing PDF dates:");
26
27 if (existingCreation) {
28 console.log(` Creation Date: ${existingCreation.toISOString()}`);
29 console.log(` Local: ${existingCreation.toLocaleString()}`);
30 console.log(` Timestamp: ${existingCreation.getTime()}`);
31 } else {
32 console.log(" Creation Date: (not set)");
33 }
34
35 if (existingModification) {
36 console.log(` Modification Date: ${existingModification.toISOString()}`);
37 console.log(` Local: ${existingModification.toLocaleString()}`);
38 } else {
39 console.log(" Modification Date: (not set)");
40 }
41
42 // === Setting dates on a new PDF ===
43 console.log("\n=== Setting Dates on New PDF ===\n");
44
45 const pdf = PDF.create();
46 pdf.addPage({ size: "letter" });
47
48 const page = pdf.getPage(0);
49 if (page) {
50 page.drawText("Document Date Examples", {
51 x: 180,
52 y: page.height - 50,
53 size: 20,
54 color: black,
55 });
56 }
57
58 // Set creation date to a specific date/time
59 const creationDate = new Date("2024-01-15T14:30:00-05:00"); // EST timezone
60 pdf.setCreationDate(creationDate);
61 console.log(`Set creation date: ${creationDate.toISOString()}`);
62
63 // Set modification date to now
64 const modificationDate = new Date();
65 pdf.setModificationDate(modificationDate);
66 console.log(`Set modification date: ${modificationDate.toISOString()}`);
67
68 // === Different ways to create dates ===
69 console.log("\n=== Different Date Creation Methods ===\n");
70

Callers 1

document-dates.tsFile · 0.70

Calls 14

loadFixtureFunction · 0.90
saveOutputFunction · 0.90
formatBytesFunction · 0.90
rgbFunction · 0.85
getCreationDateMethod · 0.80
getModificationDateMethod · 0.80
addPageMethod · 0.80
drawTextMethod · 0.80
saveMethod · 0.80
createMethod · 0.65
loadMethod · 0.45
getPageMethod · 0.45

Tested by

no test coverage detected