Some test text!

platform
search

Quick start

Contents

Step-by-step
Code Summary

The PDFTron SDK is quite simple to get started with. Just three steps actually:

That's it! Yes, really. You are ready with your first sample application using the PDFTron SDK. If you would like to dig a little deeper on your own, the entire samples collection is made available as part of the SDK, as well as online.

linkStep-by-step (Your first application)

The best way to get started is to dive right in. Let us start with a simple task - convert the first page of a PDF document to an image. In the example code we will touch upon a few basics - how to initialize PDFNet, how to open a PDF document, how to get to pages within a document. We will also touch upon some of the advanced features like PDFDraw, that are covered in detail in other samples.

  1. Extract the SDK to a local folder on your machine.
  2. Open your favourite editor and create a new cpp file - say pdf2image.cpp in the Samples directory.
  3. Include the following headers in your code file.
#include <iostream>
#include <PDF/PDFNet.h>
#include <PDF/PDFDoc.h>
#include <PDF/PDFDraw.h>
  1. Add using namespace statements.
using namespace pdftron;
using namespace PDF;
  1. Inside your main function, add the PDFNet::Initialize() call. This needs to be called before any other calls to PDFNet and should only be called once in your main() function.
PDFNet::Initialize();
  1. Load an existing PDFDoc and initialize the default security handler. To open password protected PDFs see the Encryption sample.
PDFDoc doc(pdf_filepath);
doc.InitSecurityHandler();
  1. Export PDF to image.
PDFDraw draw;
draw.Export(doc.GetPage(1), image_filepath);
  1. Wrap everything in try / catch block., and add PDFTron::common::Exception catch.
try
{
    PDFNet::Initialize();
    PDFDoc doc(pdf_filepath);
    doc.InitSecurityHandler();
    PDFDraw draw;
    draw.Export(doc.GetPage(1), image_filepath);
}
catch(pdftron::Common::Exception& ex)
{
    std::cout << ex.GetMessage() << std::endl;
}
  1. Create a folder named output and cd into it. The expected folder structure above output is as shown below.
PDFNetC
  |-Doc
  |-Headers
  |-Lib
  |-output (the output is placed here in this example)
  |-Resources
  |-Samples (the code file is placed here in this example)
  1. Compile everything using g++.
g++ ../Samples/pdf2image.cpp -I../Headers -L../Lib -lPDFNetC -lstdc++ -lpthread -lm -lc -Wl,-rpath,../Lib -Wl,-rpath$ORIGIN -o pdf2image
  1. Run the program, with the first command line argument as a PDF file, and the second as the name of the image file to be generated. For example, running ./pdf2image input.pdf output.png will result in the first page of input.pdf being converted to a PNG file named output.png.

linkCode Summary

#include <iostream>
#include <PDF/PDFNet.h>
#include <PDF/PDFDoc.h>
#include <PDF/PDFDraw.h>

using namespace pdftron;
using namespace PDF;

int main(int argc, char** argv)
{
    try
    {
        PDFNet::Initialize();

        UString pdf_filepath(argv[1]);
        UString image_filepath(argv[2]);

        PDFDoc doc(pdf_filepath);
        doc.InitSecurityHandler();

        PDFDraw draw;
        draw.Export(doc.GetPage(1), image_filepath);
    }
    catch(pdftron::Common::Exception& ex)
    {
        std::cout << ex.GetMessage() << std::endl;
    }
    return 0;
}

Get the anwsers you need: Support

Contents

Step-by-step
Code Summary