PDFPackage

Sample C# code to use Apryse SDK for creating, extracting, and manipulating PDF packages (also known as PDF portfolios). Learn more about our Server SDK and PDF Data Extraction SDK Capabilities.

1//
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3//
4using System;
5using pdftron;
6using pdftron.Common;
7using pdftron.Filters;
8using pdftron.SDF;
9using pdftron.PDF;
10
11namespace PackageTestCS
12{
13 /// <summary>
14 /// This sample illustrates how to create, extract, and manipulate PDF Portfolios
15 /// (a.k.a. PDF Packages) using PDFNet SDK.
16 /// </summary>
17 class Class1
18 {
19 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
20 static Class1() {}
21
22 // Relative path to the folder containing test files.
23 const string input_path = "../../../../TestFiles/";
24 const string output_path = "../../../../TestFiles/Output/";
25
26 /// <summary>
27 /// The main entry point for the application.
28 /// </summary>
29 [STAThread]
30 static void Main(string[] args)
31 {
32 PDFNet.Initialize(PDFTronLicense.Key);
33
34 // Create a PDF Package.
35 try
36 {
37 using (PDFDoc doc = new PDFDoc())
38 {
39 AddPackage(doc, input_path + "numbered.pdf", "My File 1");
40 AddPackage(doc, input_path + "newsletter.pdf", "My Newsletter...");
41 AddPackage(doc, input_path + "peppers.jpg", "An image");
42 AddCovePage(doc);
43 doc.Save(output_path + "package.pdf", SDFDoc.SaveOptions.e_linearized);
44 Console.WriteLine("Done.");
45 }
46 }
47 catch (PDFNetException e)
48 {
49 Console.WriteLine(e.Message);
50 }
51
52 // Extract parts from a PDF Package.
53 try
54 {
55 using (PDFDoc doc = new PDFDoc(output_path + "package.pdf"))
56 {
57 doc.InitSecurityHandler();
58
59 pdftron.SDF.NameTree files = NameTree.Find(doc, "EmbeddedFiles");
60 if(files.IsValid())
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 Console.WriteLine("Part: {0}", entry_name);
68 FileSpec file_spec = new FileSpec(i.Value());
69 Filter stm = file_spec.GetFileData();
70 if (stm!=null)
71 {
72 string fname = "extract_" + counter.ToString() + System.IO.Path.GetExtension(entry_name);
73 stm.WriteToFile(output_path + fname, false);
74 }
75 }
76 }
77 }
78
79 Console.WriteLine("Done.");
80 }
81 catch (PDFNetException e)
82 {
83 Console.WriteLine(e.Message);
84 }
85 PDFNet.Terminate();
86 }
87
88 static void AddPackage(PDFDoc doc, string file, string desc)
89 {
90 NameTree files = NameTree.Create(doc, "EmbeddedFiles");
91 FileSpec fs = FileSpec.Create(doc, 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 static 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 Page page = doc.PageCreate(new Rect(0, 0, 200, 200));
110
111 using (ElementBuilder b = new ElementBuilder())
112 using (ElementWriter w = new ElementWriter())
113 {
114 w.Begin(page);
115 Font font = Font.Create(doc, Font.StandardType1Font.e_helvetica);
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
127 // Alternatively we could import a PDF page from a template PDF document
128 // (for an example please see PDFPage sample project).
129 // ...
130 }
131 }
132}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales