Skip to content

Commit 284934b

Browse files
authored
Merge pull request #115 from aspose-pdf-cloud/pdfapps-6884-added-use-cases-for-remove-metadata
PDFAPPS-6884: added use cases for remove metadata
2 parents c654ecb + c52924b commit 284934b

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// 1. Load your Application Secret and Key from the JSON file or set credentials in another way
2+
// 2. Create an object to connect to the Pdf.Cloud API
3+
// 3. Upload your document file
4+
// 4. Create a new XmpMetadataProperty with Your key and null as value for delete XmpMetadataProperty
5+
// 5. Delete XMpMetadataProperty in the document using postXmpMetadata() function
6+
// 6. Perform some action after successful addition
7+
// All values of variables starting with "YOUR_****" should be replaced by real user values
8+
9+
import credentials from "./credentials.json" with { type: "json" };
10+
import fs from 'node:fs/promises';
11+
import path from 'node:path';
12+
import { PdfApi } from "asposepdfcloud";
13+
import { XmpMetadata } from "asposepdfcloud/src/models/xmpMetadata.js";
14+
import { XmpMetadataProperty } from "asposepdfcloud/src/models/xmpMetadataProperty.js";
15+
16+
const configParams = {
17+
LOCAL_FOLDER: "C:\\Samples\\",
18+
PDF_DOCUMENT_NAME: "sample.pdf",
19+
LOCAL_RESULT_DOCUMENT_NAME: "output_sample.pdf",
20+
};
21+
22+
const pdfApi = new PdfApi(credentials.id, credentials.key);
23+
24+
const pdfMetadatas = {
25+
async uploadDocument() {
26+
const pdfFilePath = path.join(configParams.LOCAL_FOLDER, configParams.PDF_DOCUMENT_NAME);
27+
const pdfFileData = await fs.readFile(pdfFilePath);
28+
await pdfApi.uploadFile(configParams.PDF_DOCUMENT_NAME, pdfFileData);
29+
},
30+
31+
async downloadResult() {
32+
const changedPdfData = await pdfApi.downloadFile(configParams.PDF_DOCUMENT_NAME);
33+
const filePath = path.join(configParams.LOCAL_FOLDER, configParams.LOCAL_RESULT_DOCUMENT_NAME);
34+
await fs.writeFile(filePath, changedPdfData.body);
35+
console.log("Downloaded: " + filePath);
36+
},
37+
38+
async getMetadata () {
39+
const responseMetadata = await pdfApi.getXmpMetadataJson(configParams.PDF_DOCUMENT_NAME);
40+
41+
if (responseMetadata.response.status == 200)
42+
{
43+
const props = responseMetadata.body.properties;
44+
props.forEach((prop) =>{
45+
console.log(prop.key);
46+
});
47+
}
48+
},
49+
50+
async deleteMetadata () {
51+
const prop = new XmpMetadataProperty();
52+
prop.key = 'dc:creator';
53+
prop.value = null; // null value means delete property...
54+
prop.namespaceUri = 'http://purl.org/dc/elements/1.1/';
55+
56+
const metadata = new XmpMetadata();
57+
metadata.properties = [prop];
58+
59+
const response = await pdfApi.postXmpMetadata(configParams.PDF_DOCUMENT_NAME, metadata);
60+
61+
if (response.body.code == 200) {
62+
console.log("Dlete metadata '" + prop.key + "' successful!");
63+
return true;
64+
}
65+
},
66+
}
67+
68+
async function main() {
69+
try {
70+
await pdfMetadatas.uploadDocument();
71+
await pdfMetadatas.getMetadata();
72+
await pdfMetadatas.deleteMetadata();
73+
await pdfMetadatas.getMetadata();
74+
await pdfMetadatas.downloadResult();
75+
} catch (error) {
76+
console.error("Error:", error.message);
77+
}
78+
}
79+
80+
main();

0 commit comments

Comments
 (0)