Sample UWP code to use Apryse SDK for creating, extracting, and manipulating PDF packages (also known as PDF portfolios).
Learn more about our full PDF Data Extraction SDK Capabilities.
To start your free trial, get stated with UWP SDK.
1//
2// Copyright (c) 2001-2020 by PDFTron Systems Inc. All Rights Reserved.
3//
4
5using System;
6using System.IO;
7using System.Threading.Tasks;
8using Windows.Foundation;
9
10using pdftron.Filters;
11using pdftron.PDF;
12using pdftron.SDF;
13
14using PDFNetUniversalSamples.ViewModels;
15
16namespace PDFNetSamples
17{
18 public sealed class PDFPackageTest : Sample
19 {
20 public PDFPackageTest() :
21 base("PDFPackage", "This sample illustrates how to create, extract, and manipulate PDF Portfolios (a.k.a. PDF Packages) using PDFNet SDK.")
22 {
23 }
24
25 public override IAsyncAction RunAsync()
26 {
27 return Task.Run(new System.Action(async () => {
28 WriteLine("--------------------------------");
29 WriteLine("Starting PDFPackage Test...");
30 WriteLine("--------------------------------\n");
31 try
32 {
33
34 PDFDoc doc = new PDFDoc();
35 AddPackage(doc, Path.Combine(InputPath, "numbered.pdf"), "My File 1");
36 AddPackage(doc, Path.Combine(InputPath, "newsletter.pdf"), "My Newsletter...");
37 AddPackage(doc, Path.Combine(InputPath, "peppers.jpg"), "An image");
38 AddCovePage(doc);
39 string output_file_path = Path.Combine(OutputPath, "package.pdf");
40 await doc.SaveAsync(output_file_path, SDFDocSaveOptions.e_linearized);
41 WriteLine(string.Format("PDFPackage created at: {0}{1}", output_file_path, Environment.NewLine));
42 await AddFileToOutputList(output_file_path).ConfigureAwait(false);
43
44 doc.Destroy();
45 }
46 catch (Exception e)
47 {
48 WriteLine(e.Message);
49 }
50
51 // Extract parts from a PDF Package.
52 {
53 PDFDoc doc = new PDFDoc(Path.Combine(OutputPath, "package.pdf"));
54 doc.InitSecurityHandler();
55
56 pdftron.SDF.NameTree files = NameTree.Find(doc.GetSDFDoc(), "EmbeddedFiles");
57 if(files.IsValid())
58 {
59
60 WriteLine(string.Format("Extracting files from {0}:", Path.Combine(OutputPath, "package.pdf")));
61
62 // Traverse the list of embedded files.
63 NameTreeIterator i = files.GetIterator();
64 for (int counter = 0; i.HasNext(); i.Next(), ++counter)
65 {
66 string entry_name = i.Key().GetAsPDFText();
67 WriteLine(string.Format("Part: {0}", entry_name));
68 FileSpec file_spec = new FileSpec(i.Value());
69 IFilter stm = file_spec.GetFileData();
70
71 if (stm!=null)
72 {
73 string fname = Path.Combine(OutputPath, "extract_" + counter.ToString() + entry_name.Substring(entry_name.Length - 4));
74 stm.WriteToFile(fname, false);
75 WriteLine(string.Format("File {0} extracted from package", fname));
76 await AddFileToOutputList(fname).ConfigureAwait(false);
77 }
78 }
79 }
80 }
81
82 WriteLine("\n--------------------------------");
83 WriteLine("Done Annotation Test.");
84 WriteLine("--------------------------------\n");
85 })).AsAsyncAction();
86 }
87
88 void AddPackage(PDFDoc doc, string file, string desc)
89 {
90 NameTree files = NameTree.Create(doc.GetSDFDoc(), "EmbeddedFiles");
91 FileSpec fs = FileSpec.Create(doc.GetSDFDoc(), file, true);
92 byte[] file1_name = System.Text.Encoding.UTF8.GetBytes(file);
93 files.Put(file1_name, fs.GetSDFObj());
94 fs.GetSDFObj().PutText("Desc", desc);
95
96 Obj collection = doc.GetRoot().FindObj("Collection");
97 if (collection == null) collection = doc.GetRoot().PutDict("Collection");
98
99 // You could here manipulate any entry in the Collection dictionary.
100 // For example, the following line sets the tile mode for initial view mode
101 // Please refer to section '2.3.5 Collections' in PDF Reference for details.
102 collection.PutName("View", "T");
103 }
104
105 void AddCovePage(PDFDoc doc)
106 {
107 // Here we dynamically generate cover page (please see ElementBuilder
108 // sample for more extensive coverage of PDF creation API).
109 pdftron.PDF.Page page = doc.PageCreate(new pdftron.PDF.Rect(0, 0, 200, 200));
110
111 ElementBuilder b = new ElementBuilder();
112 ElementWriter w = new ElementWriter();
113 w.Begin(page);
114 //Font font = Font.CreateTrueTypeFont(doc.GetSDFDoc(), new System.Drawing.Font("Comic Sans MS", 12), true, false);
115 Font font = Font.Create(doc, pdftron.PDF.FontStandardType1Font.e_times_roman);
116 w.WriteElement(b.CreateTextBegin(font, 12));
117 Element e = b.CreateTextRun("My PDF Collection");
118 e.SetTextMatrix(1, 0, 0, 1, 50, 96);
119 e.GetGState().SetFillColorSpace(ColorSpace.CreateDeviceRGB());
120 e.GetGState().SetFillColor(new ColorPt(1, 0, 0));
121 w.WriteElement(e);
122 w.WriteElement(b.CreateTextEnd());
123 w.End();
124 doc.PagePushBack(page);
125
126 // Alternatively we could import a PDF page from a template PDF document
127 // (for an example please see PDFPage sample project).
128 // ...
129 }
130 }
131}
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales