()
| 11 | import { formatBytes, loadFixture, saveOutput } from "../utils"; |
| 12 | |
| 13 | async 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 |
no test coverage detected