PDFPackage

Sample C# 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 Xamarin SDK.

1//
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3//
4using System;
5using pdftron;
6using pdftron.Common;
7using pdftron.Filters;
8using pdftron.SDF;
9using pdftron.PDF;
10
11using NUnit.Framework;
12
13namespace MiscellaneousSamples
14{
15 /// <summary>
16 /// This sample illustrates how to create, extract, and manipulate PDF Portfolios
17 /// (a.k.a. PDF Packages) using PDFNet SDK.
18 /// </summary>
19 [TestFixture]
20 public class PDFPackageTest
21 {
22
23 // Relative path to the folder containing test files.
24 const string input_path = "TestFiles/";
25
26 /// <summary>
27 /// The main entry point for the application.
28 /// </summary>
29 [Test]
30 public static void Sample()
31 {
32
33 // Create a PDF Package.
34 try
35 {
36 using (PDFDoc doc = new PDFDoc())
37 {
38 AddPackage(doc, Utils.GetAssetTempFile(input_path + "numbered.pdf"), "My File 1");
39 AddPackage(doc, Utils.GetAssetTempFile(input_path + "newsletter.pdf"), "My Newsletter...");
40 AddPackage(doc, Utils.GetAssetTempFile(input_path + "peppers.jpg"), "An image");
41 AddCovePage(doc);
42 doc.Save(Utils.CreateExternalFile("package.pdf"), SDFDoc.SaveOptions.e_linearized);
43 Console.WriteLine("Done.");
44 }
45 }
46 catch (PDFNetException e)
47 {
48 Console.WriteLine(e.Message);
49 Assert.True(false);
50 }
51
52 // Extract parts from a PDF Package.
53 try
54 {
55 using (PDFDoc doc = new PDFDoc(Utils.CreateExternalFile("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(Utils.CreateExternalFile(fname), false);
74 }
75 }
76 }
77 }
78
79 Console.WriteLine("Done.");
80 }
81 catch (PDFNetException e)
82 {
83 Console.WriteLine(e.Message);
84 Assert.True(false);
85 }
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