Working with PDF Packages / PDF Portfolios

116 views
Skip to first unread message

Support

unread,
Sep 1, 2009, 4:30:58 PM9/1/09
to PDFTron PDFNet SDK
Q: I require to create an app that need to be able to create PDF
portfolio files ( a.k.a. pdf packages, a.k.a. pdf collections ), which
are pdf files that have other pdf files inside it.

Is your library pdftron (http://www.pdftron.com/pdfnet) capable of
creating this type of pdf files?

----------
A: You can use PDFNet to create, extract, or manage PDF Packages.
You may want to refer to the following article for an example of how
to generate, extract PDF Packages using PDFNet (using C#):

http://groups.google.com/group/pdfnet-sdk/browse_thread/thread/5fffca3cadaec0b7
http://groups.google.com/group/pdfnet-sdk/web/PDFPackageTest.zip

In case you are developing using C++ or Java you can find relevant
code samples in the attachment.

http://groups.google.com/group/pdfnet-sdk/web/PDFPackageTestCPPJava.zip

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2009 by PDFTron Systems Inc. All Rights
Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

#include <SDF/NameTree.h>
#include <PDF/PDFNet.h>
#include <PDF/PDFDoc.h>
#include <PDF/PDFDraw.h>
#include <PDF/ElementBuilder.h>
#include <PDF/ElementWriter.h>
#include <Filters/StdFile.h>

#include <iostream>

using namespace pdftron;
using namespace Common;
using namespace Filters;
using namespace SDF;
using namespace PDF;
using namespace std;

//-----------------------------------------------------------------------------------
/// This sample illustrates how to create, extract, and manipulate PDF
Portfolios
/// (a.k.a. PDF Packages) using PDFNet SDK.
//-----------------------------------------------------------------------------------

// Relative path to the folder containing test files.
static const string input_path = "../../TestFiles/";
static const string output_path = "../../TestFiles/Output/";

static void AddPackage(PDFDoc& doc, string file, string desc)
{
NameTree files = NameTree::Create(doc, "EmbeddedFiles");
FileSpec fs = FileSpec::Create(doc, file.c_str(), true);
files.Put((UChar*)file.c_str(), int(file.size()), fs.GetSDFObj());
fs.GetSDFObj().PutText("Desc", UString(desc.c_str()));

Obj collection = doc.GetRoot().FindObj("Collection");
if (!collection) collection = doc.GetRoot().PutDict("Collection");

// You could here manipulate any entry in the Collection dictionary.
// For example, the following line sets the tile mode for initial
view mode
// Please refer to section '2.3.5 Collections' in PDF Reference for
details.
collection.PutName("View", "T");
}

static void AddCovePage(PDFDoc& doc)
{
// Here we dynamically generate cover page (please see
ElementBuilder
// sample for more extensive coverage of PDF creation API).
Page page = doc.PageCreate(Rect(0, 0, 200, 200));

ElementBuilder b;
ElementWriter w;
w.Begin(page);
Font font = Font::Create(doc, Font::e_helvetica);
w.WriteElement(b.CreateTextBegin(font, 12));
Element e = b.CreateTextRun("My PDF Collection");
e.SetTextMatrix(1, 0, 0, 1, 50, 96);
e.GetGState().SetFillColorSpace(ColorSpace::CreateDeviceRGB());
e.GetGState().SetFillColor(ColorPt(1, 0, 0));
w.WriteElement(e);
w.WriteElement(b.CreateTextEnd());
w.End();
doc.PagePushBack(page);

// Alternatively we could import a PDF page from a template PDF
document
// (for an example please see PDFPage sample project).
// ...
}

int main(int argc, char *argv[])
{
int ret = 0;
PDFNet::Initialize();

// Create a PDF Package.
try
{
PDFDoc doc;
AddPackage(doc, input_path + "numbered.pdf", "My File 1");
AddPackage(doc, input_path + "newsletter.pdf", "My Newsletter...");
AddPackage(doc, input_path + "peppers.jpg", "An image");
AddCovePage(doc);
doc.Save((output_path + "package.pdf").c_str(),
SDFDoc::e_linearized, 0);
cout << "Done." << endl;
}
catch(Common::Exception& e)
{
cout << e << endl;
ret = 1;
}
catch(...)
{
cout << "Unknown Exception" << endl;
ret = 1;
}

// Extract parts from a PDF Package.
try
{
PDFDoc doc((output_path + "package.pdf").c_str());
doc.InitSecurityHandler();

NameTree files = NameTree::Find(doc, "EmbeddedFiles");
if(files.IsValid())
{
// Traverse the list of embedded files.
NameTreeIterator i = files.GetIterator();
for (int counter = 0; i.HasNext(); i.Next(), ++counter)
{
UString entry_name;
i.Key().GetAsPDFText(entry_name);
cout << "Part: " << entry_name.ConvertToAscii() << endl;
FileSpec file_spec(i.Value());
Filter stm(file_spec.GetFileData());
if (stm)
{
FilterReader reader(stm);
char tmpbuf[1024];
sprintf(tmpbuf, "%sextract_%d", output_path.c_str(), counter);
StdFile f(tmpbuf, StdFile::e_write_mode);
FilterWriter writer(f);
writer.WriteFilter(reader);
writer.Flush();
}
}
}

cout << "Done." << endl;
}
catch(Common::Exception& e)
{
cout << e << endl;
ret = 1;
}
catch(...)
{
cout << "Unknown Exception" << endl;
ret = 1;
}

PDFNet::Terminate();
return ret;
}

//------------------------------------
Java sample:

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2009 by PDFTron Systems Inc. All Rights
Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------

import pdftron.Common.Matrix2D;
import pdftron.Common.PDFNetException;
import pdftron.PDF.*;
import pdftron.Filters.*;
import pdftron.SDF.*;


//-----------------------------------------------------------------------------------
// This sample illustrates how to create, extract, and manipulate PDF
Portfolios
// (a.k.a. PDF Packages) using PDFNet SDK.
//-----------------------------------------------------------------------------------
public class PDFPackageTest
{
// Relative path to the folder containing test files.
static String input_path = "../../TestFiles/";
static String output_path = "../../TestFiles/Output/";

public static void main(String[] args)
{
PDFNet.initialize();

// Create a PDF Package.
try
{
PDFDoc doc = new PDFDoc();
addPackage(doc, input_path + "numbered.pdf", "My File 1");
addPackage(doc, input_path + "newsletter.pdf", "My Newsletter...");
addPackage(doc, input_path + "peppers.jpg", "An image");
addCovePage(doc);
doc.save(output_path + "package.pdf", SDFDoc.e_linearized, null);
doc.close();
}
catch(Exception e)
{
e.printStackTrace();
}

// Extract parts from a PDF Package.
try
{
PDFDoc doc = new PDFDoc(output_path + "package.pdf");
doc.initSecurityHandler();

pdftron.SDF.NameTree files = NameTree.find(doc.getSDFDoc(),
"EmbeddedFiles");
if(files.isValid())
{
// Traverse the list of embedded files.
NameTreeIterator i = files.getIterator();
for (int counter = 0; i.hasNext(); i.next(), ++counter)
{
String entry_name = i.key().getAsPDFText();
System.out.println("Part: " + entry_name);

FileSpec file_spec = new FileSpec(i.value());
Filter stm = file_spec.getFileData();
if (stm!=null)
{
FilterReader reader = new FilterReader(stm);
String fname = output_path + "extract_" + counter;
StdFile f = new StdFile(fname, StdFile.e_write_mode);
FilterWriter writer = new FilterWriter(f);
writer.writeFilter(reader);
writer.flush();
}
}
}
doc.close();
}
catch(Exception e)
{
e.printStackTrace();
}

PDFNet.terminate();
}

static void addPackage(PDFDoc doc, String file, String desc) throws
PDFNetException
{
NameTree files = NameTree.create(doc.getSDFDoc(), "EmbeddedFiles");
FileSpec fs = FileSpec.create(doc, file, true);
files.put(file.getBytes(), fs.getSDFObj());
fs.getSDFObj().putText("Desc", desc);

Obj collection = doc.getRoot().findObj("Collection");
if (collection == null) collection = doc.getRoot().putDict
("Collection");

// You could here manipulate any entry in the Collection
dictionary.
// For example, the following line sets the tile mode for initial
view mode
// Please refer to section '2.3.5 Collections' in PDF Reference for
details.
collection.putName("View", "T");
}

static void addCovePage(PDFDoc doc) throws PDFNetException
{
// Here we dynamically generate cover page (please see
ElementBuilder
// sample for more extensive coverage of PDF creation API).
Page page = doc.pageCreate(new Rect(0, 0, 200, 200));

ElementBuilder b = new ElementBuilder();
ElementWriter w = new ElementWriter();
w.begin(page);
Font font = Font.create(doc.getSDFDoc(), Font.e_helvetica);
w.writeElement(b.createTextBegin(font, 12));
Element e = b.createTextRun("My PDF Collection");
e.setTextMatrix(1, 0, 0, 1, 50, 96);
e.getGState().setFillColorSpace(ColorSpace.createDeviceRGB());
e.getGState().setFillColor(new ColorPt(1, 0, 0));
w.writeElement(e);
w.writeElement(b.createTextEnd());
w.end();
doc.pagePushBack(page);

// Alternatively we could import a PDF page from a template PDF
document
// (for an example please see PDFPage sample project).
// ...
}
}



Reply all
Reply to author
Forward
0 new messages