-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
165 lines (137 loc) · 6.9 KB
/
Program.cs
File metadata and controls
165 lines (137 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using DevExpress.Pdf;
using DevExpress.Pdf.Xmp;
using System.IO;
namespace XmpMetadataExamples
{
class Program
{
static void Main(string[] args)
{
EmbedLoadedMetadata();
EmbedEditedMetadata();
EmbedGeneratedMetadata();
}
static void EmbedEditedMetadata()
{
using (PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor())
{
// Load a document:
pdfDocumentProcessor.LoadDocument("Documents//Invoice.pdf");
PdfDocument document = pdfDocumentProcessor.Document;
// Retrieve metadata:
XmpDocument editedMetadata = XmpDocument.FromString(document.Metadata.Data);
// Add items to the Creator array:
XmpArray creators = editedMetadata.GetArray("dc:creator");
if (creators != null)
{
creators.Add("PDF Document API");
creators.Add("Office File API");
}
// Change the CreatorTool node value:
XmpSimpleNode creatorTool = editedMetadata.GetSimpleValue("xmp:CreatorTool");
creatorTool.SetValue("PDF Document API");
// Add MaxPageSize structure:
XmpName structureName = XmpName.Get("MaxPageSize", "http://ns.adobe.com/xap/1.0/t/pg/");
XmpStructure dimensions = editedMetadata.CreateStructure(structureName);
editedMetadata.RegisterNamespace("http://ns.adobe.com/xap/1.0/sType/Dimensions#", "stDim");
dimensions.Add("stDim:h", 11);
dimensions.Add("stDim:w", 8.5f);
dimensions.Add("stDim:Unit", "inch");
editedMetadata.Remove("dc:subject");
// Embed modified metadata to the document:
document.SetMetadata(editedMetadata);
// Save the result:
pdfDocumentProcessor.SaveDocument("Invoice_Upd.pdf");
}
}
static void EmbedGeneratedMetadata()
{
using (PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor())
{
// Load a document:
pdfDocumentProcessor.LoadDocument("Documents//Invoice_blank.pdf");
PdfDocument document = pdfDocumentProcessor.Document;
XmpDocument generatedMetadata = GenerateXmpMetadata();
// Embed modified metadata to the document:
document.SetMetadata(generatedMetadata);
// Save the result:
pdfDocumentProcessor.SaveDocument("Invoice_new.pdf");
}
}
private static XmpDocument GenerateXmpMetadata()
{
XmpDocument document = new XmpDocument();
// Register namespaces:
document.RegisterNamespace("http://ns.adobe.com/xap/1.0/", "xmp");
document.RegisterNamespace("http://ns.adobe.com/xap/1.0/sType/Dimensions#", "stDim");
document.RegisterNamespace("http://ns.adobe.com/pdf/1.3/", "pdf");
document.RegisterNamespace("http://ns.adobe.com/xap/1.0/t/pg/", "xmpTPg");
// Add items with "xmp" prefix:
XmpArray array = document.CreateArray("xmp:Identifier", XmpArrayType.Unordered);
array.Add("identifier1");
array.Add("identifier2");
array.Add("identifier3");
document.CreateSimpleValue("xmp:Label", "Demo");
// Add items with the "pdf" prefix:
document.Add("pdf:Keywords", "Invoice,Northwind,PDF,XMP");
document.Add("pdf:Producer", "Developer Express Inc.DXperience(tm)");
document.CreateSimpleValue("pdf:PDFVersion", "1.3");
// Add items with the "xmpTPg" prefix:
XmpStructure dimensions = document.CreateStructure("xmpTPg:MaxPageSize");
dimensions.Add("stDim:h", 750);
dimensions.Add("stDim:w", 500);
dimensions.Add("stDim:Unit", "pixel");
document.Add("xmpTPg:NPages", 6);
return document;
}
static void EmbedLoadedMetadata()
{
ExportSchemas();
using (PdfDocumentProcessor pdfDocumentProcessor = new PdfDocumentProcessor())
{
// Set metadata loaded from a stream:
pdfDocumentProcessor.LoadDocument("Documents//Invoice_signed.pdf");
XmpDocument metadata;
using (FileStream xmlStream = new FileStream("Documents//metadata_new.xml", FileMode.Open, FileAccess.Read))
{
metadata = XmpDocument.FromStream(xmlStream);
pdfDocumentProcessor.Document.SetMetadata(metadata);
}
pdfDocumentProcessor.SaveDocument("Invoice_metadata-from_file.pdf");
}
}
private static void ExportSchemas()
{
XmpDocument metadata = new XmpDocument();
// Add items from the XMP basic schema:
XmpProperties basicSchema = metadata.XmpProperties;
basicSchema.CreatorTool = "PDF Document API";
basicSchema.Label = "Sample";
basicSchema.Identifier.Add("Id");
basicSchema.Rating = "0";
// Add items from the Dublin Core schema:
DublinCoreProperties dublinCoreProperties = metadata.DublinCoreProperties;
dublinCoreProperties.Creator.Add("DevExpress");
dublinCoreProperties.Description.AddString("This document has emdedded XMP metadata", "en-us");
dublinCoreProperties.Title.AddString("Invoice", "x-default");
dublinCoreProperties.Type.Add("PDF");
dublinCoreProperties.Publisher.Add("PDF Document API");
// Add items from the Adobe PDF schema:
AdobePdfProperties adobePdfProperties = metadata.PdfProperties;
adobePdfProperties.Keywords = "Invoice, Northwind, PDF, XMP";
adobePdfProperties.PDFVersion = "1.3";
adobePdfProperties.Producer = "PDF Document API";
adobePdfProperties.Trapped = false;
// Add items from the Rights Management schema:
XmpRightsManagementProperties rightsManagementSchema = metadata.RightsManagementProperties;
rightsManagementSchema.Certificate = "https://www.devexpress.com/";
rightsManagementSchema.Owner.Add("DevExpress");
rightsManagementSchema.Marked = true;
rightsManagementSchema.WebStatement = "https://www.devexpress.com/support/eulas/";
rightsManagementSchema.UsageTerms.AddString("Copyright(C) 2021 DevExpress.All Rights Reserved.", "x-default");
// Export generated metadata to the file:
using (var fileStream = new FileStream("Documents//metadata_new.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite))
metadata.Serialize(fileStream);
}
}
}