I am trying to set up encryption for this document so that it cannot be
edited after the pdf has been generated. I am new to c++ and have been using
the Hello World code, and other code as example code to build this document.
I cannot find any example code on how to set up the encryption or protection
of the PDF document. What I am trying to do is set up a digital signature
with encryption so that no one can edit the document for legal reasons. I am
sure there is an easy way to do it, but the documentation is going over my
head.
/*
* Include the standard headers for cout to write
* some output to the console.
*/
#include <iostream>
/*
* Now include all podofo header files, to have access
* to all functions of podofo and so that you do not have
* to care about the order of includes.
*
* You should always use podofo.h and not try to include
* the required headers on your own.
*/
#include <podofo/podofo.h>
/*
* All podofo classes are member of the PoDoFo namespace.
*/
using namespace PoDoFo;
#define FONT_SIZE 10
#define FONT_SIZE_TITLE 20
#define NUM_OF_COLUMN 15
#define SINGLE_ROW 1
#define NUM_OF_ROW 6
#define WIDTH_CELL 50
#define HEIGHT_CELL 25
#define BOARDER_SIZE 56.69 //Given by podofo
#define TITLE_VERT_OFFSET 90
#define SPLASH_VERT_OFFSET 150
#define TITLE_HORZ_OFFEST 200
#define TABLE_VERT_OFFEST 110
#define SECOND_COLUMN_OFFSET 250
#define THIRD_COLUMN_OFFSET 500
#define FIRST_ROW_OFFSET 250
#define SECOND_ROW_OFFSET 400
void PrintHelp()
{
std::cout << "This is a table made using PoDoFo." << std::endl
<< "It creates a small PDF file containing a table of data
(hardcoded)<" << std::endl
<< "Please see http://podofo.sf.net for more information" <<
std::endl << std::endl;
std::cout << "Usage:" << std::endl;
std::cout << " main [outputfile.pdf]" << std::endl << std::endl;
}
void PrintData( const char* pszFilename )
{
PdfStreamedDocument document( pszFilename);
/generate encryption???
PdfEncrypt *pEncrypt = PdfEncrypt::CreatePdfEncrypt("a", "a",
PdfEncrypt::ePdfPermissions_Copy, PdfEncrypt::ePdfEncryptAlgorithm_RC4V1,
PdfEncrypt::ePdfKeyLength_40);
/*
* This pointer will hold the page object later.
* PdfSimpleWriter can write several PdfPage's to a PDF file.
*/
PdfPage* pPage;
/*
* PdfPainter is the class which is able to draw text and graphics
* directly on a PdfPage object.
*/
PdfPainter painter;
/*
* A PdfFont object is required to draw text on a PdfPage using a
PdfPainter.
* PoDoFo will find the font using fontconfig on your system and embedd
truetype
* fonts automatically in the PDF file.
*/
PdfFont* pFont;
/*
* The PdfDocument object can be used to create PdfPage objects.
* The PdfPage object is owned by the PdfDocument will also be deleted
automatically
* by the PdfDocument object.
*
* You have to pass only one argument, i.e. the page size of the page to
create.
* There are predefined enums for some common page sizes.
* for last statement: true/landscape. Default is False/portrait
*/
pPage = document.CreatePage( PdfPage::CreateStandardPageSize(
ePdfPageSize_A4, PODOFO_TRUE ) );
/*
* If the page cannot be created because of an error (e.g.
ePdfError_OutOfMemory )
* a NULL pointer is returned.
* We check for a NULL pointer here and throw an exception using the
RAISE_ERROR macro.
* The raise error macro initializes a PdfError object with a given
error code and
* the location in the file in which the error ocurred and throws it as
an exception.
*/
if( !pPage )
{
PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
}
/*
* Set the page as drawing target for the PdfPainter.
* Before the painter can draw, a page has to be set first.
*/
painter.SetPage( pPage );
/*
* Create a PdfFont object using the font "font Here".
* The font is found on the system using fontconfig and embedded into
the
* PDF file. If Arial is not available, a default font will be used.
*
* The created PdfFont will be deleted by the PdfDocument.
*/
pFont = document.CreateFont( "Helvetica");
/*
* If the PdfFont object cannot be allocated return an error.
*/
if( !pFont )
{
PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
}
pFont->SetFontSize( FONT_SIZE );
//trouble creating text to be bold
//pFont->SetBold(1);
/*
* Set the font as default font for drawing.
* A font has to be set before you can draw text on
* a PdfPainter.
*/
painter.SetFont( pFont );
/* create a table
* TableTop and tableModeTop refer to the top purple row of the table
* tableBody and tableModeBody refer to the white body of the table
* creating a table is (y_col, x_row) form starting at 0
*/
//create a table (y_col, x_row) with word wrap
//TableTop is the purple first row of the table
PdfTable TableTop = PdfTable(NUM_OF_COLUMN, SINGLE_ROW);
PdfSimpleTableModel tableModeTop(NUM_OF_COLUMN, SINGLE_ROW);
//TableBody is the white rows of the table with the data
PdfTable TableBody = PdfTable(NUM_OF_COLUMN, NUM_OF_ROW);
PdfSimpleTableModel tableModeBody(NUM_OF_COLUMN, NUM_OF_ROW);
//enable word wrap and center text in the cells for top row
tableModeTop.SetWordWrapEnabled(PODOFO_TRUE);
tableModeTop.SetAlignment(PoDoFo::ePdfAlignment_Center);
tableModeTop.SetAlignment(PoDoFo::ePdfVerticalAlignment_Center);
//enable word wrap and center text in the cells of the body
tableModeBody.SetWordWrapEnabled(PODOFO_TRUE);
tableModeBody.SetAlignment(PoDoFo::ePdfAlignment_Center);
tableModeBody.SetAlignment(PoDoFo::ePdfVerticalAlignment_Center);
//set the font of the table to be default pFont
tableModeTop.SetFont(pFont);
tableModeBody.SetFont(pFont);
/* use to change colour in RGB
* You could set a different color than black to draw
* default is black or 0, 0, 0
* range of numbers is 0 to 1
*/
PdfColor white = PdfColor(1.0, 1.0, 1.0);
PdfColor purple = PdfColor(0.5, 0.0, 0.5);
PdfColor black = PdfColor(0.0, 0.0, 0.0);
//set first row of the table color to purple
tableModeTop.SetBackgroundEnabled(PODOFO_TRUE);
tableModeTop.SetBackgroundColor(purple);
//give color of text purple cells will have white text
//body text color is black
tableModeTop.SetForegroundColor(white);
tableModeBody.SetForegroundColor(black);
//first row, purple cell hardcoded names for information
tableModeTop.SetText(0, 0, "Sector ID");
tableModeTop.SetText(1, 0, "Ant Pos");
tableModeTop.SetText(2, 0, "Target Azimuth");
tableModeTop.SetText(3, 0, "Measured Azimuth");
tableModeTop.SetText(4, 0, "Azimuth Delta");
tableModeTop.SetText(5, 0, "Tilt");
tableModeTop.SetText(6, 0, "Roll");
tableModeTop.SetText(7, 0, "Height");
tableModeTop.SetText(8, 0, "ft/m");
tableModeTop.SetText(9, 0, "Height Type");
tableModeTop.SetText(10, 0, "Lat");
tableModeTop.SetText(11, 0, "Lon");
tableModeTop.SetText(12, 0, "Date");
tableModeTop.SetText(13, 0, "Time");
tableModeTop.SetText(14, 0, "Image Name");
//body, rows 1 to Num_OF_ROW and NUM OF COLUMN
//using just to show grid in printed tables
//in future, incoming data goes here
for(int i = 0; i < NUM_OF_COLUMN; i++){
for(int j = 0; j < NUM_OF_ROW; j++){
std::ostringstream stream;
stream << "(" << i << "," << j << ")";//hardcoded values that say
data
/*
//tableModeBody.SetText(0, 0, "Data");
//tableModeBody.SetText(1, 0, "Data");
//tableModeBody.SetText(2, 0, "Data");
//tableModeBody.SetText(3, 2, "Data");
//tableModeBody.SetText(4, 5, "Data");
//tableModeBody.SetText(5, 3, "Data");
//tableModeBody.SetText(6, 5, "Data");
//tableModeBody.SetText(7, 0, "Data");
//tableModeBody.SetText(8, 0, "Data");
//tableModeBody.SetText(9, 0, "Data");
//tableModeBody.SetText(10, 0, "Data");
//tableModeBody.SetText(11, 0, "Data");
//tableModeBody.SetText(12, 0, "Data");
//tableModeBody.SetText(13, 0, "Data");
//tableModeBody.SetText(14, 0, "Data");
*/
std::string data = stream.str();
tableModeBody.SetText(i, j, data);
}
}
//attach TableTop to tableModeTop.
//attaches the information of tableModeTop to TableTop
TableTop.SetModel(&tableModeTop);
TableBody.SetModel(&tableModeBody);
//size of each block, width should be the same as TableBody
TableTop.SetColumnWidth(WIDTH_CELL);
TableTop.SetRowHeight(HEIGHT_CELL);
//size of each cell, width should be the same as TableTop
TableBody.SetColumnWidth(WIDTH_CELL);
TableBody.SetRowHeight(HEIGHT_CELL);
/* load picture and place in Pdf
* load photo from diretory
*/
PdfImage splash( &document );
splash.LoadFromFile("./titleSplash1.jpg");
painter.DrawImage( 0.0, pPage->GetPageSize().GetHeight() -
SPLASH_VERT_OFFSET, &splash );
//title
std::string title = "Antenna Alignment Results for Site VZW1000 " ;
//find height of the page minus verical offset and size of boarder.
double height = pPage->GetPageSize().GetHeight() - TITLE_VERT_OFFSET -
BOARDER_SIZE;
//change font size for the title
pFont->SetFontSize( FONT_SIZE_TITLE );
//the bottom left corner of the PDF is the ( 0 , 0 ) xy coordinate
//DrawText(x coordinate, y coordinate, text being printed to pdf)
painter.DrawText(TITLE_HORZ_OFFEST, height, title);
//change the font size back to normal
pFont->SetFontSize( FONT_SIZE );
//draw table slightly below title
//value found by finding the hight of the table, minus vertical offset
and baorder size
//Draw(x coordinate, y coordinate, painter)
//slightly to the left (-10) fix later
TableTop.Draw(BOARDER_SIZE - 10 , pPage->GetPageSize().GetHeight() -
TABLE_VERT_OFFEST - BOARDER_SIZE, &painter);
//draw body of table directly underneath the TableTop row table
//slightly to the left (-10) fix later
TableBody.Draw(BOARDER_SIZE - 10, pPage->GetPageSize().GetHeight() -
TABLE_VERT_OFFEST - BOARDER_SIZE - HEIGHT_CELL, &painter);
//finish the document
painter.FinishPage();
//page 2
//print new page
pPage = document.CreatePage( PdfPage::CreateStandardPageSize(
ePdfPageSize_A4, PODOFO_TRUE ) );
//be able to edit new page
painter.SetPage( pPage );
//print photo splash
painter.DrawImage( 0.0, pPage->GetPageSize().GetHeight() -
SPLASH_VERT_OFFSET, &splash );
pFont->SetFontSize( FONT_SIZE_TITLE );
//DrawText(x coordinate, y coordinate, text being printed to pdf)
painter.DrawText(TITLE_HORZ_OFFEST, height, title);
//draw other photos on second page
//2 row by 3 columns
PdfImage azimuthPhoto( &document );
azimuthPhoto.LoadFromFile("./azimuth.jpg");
painter.DrawImage(BOARDER_SIZE,
pPage->GetPageSize().GetHeight() - BOARDER_SIZE - FIRST_ROW_OFFSET,
&azimuthPhoto);
painter.DrawImage(BOARDER_SIZE + SECOND_COLUMN_OFFSET ,
pPage->GetPageSize().GetHeight() - BOARDER_SIZE - FIRST_ROW_OFFSET,
&azimuthPhoto);
painter.DrawImage(BOARDER_SIZE + THIRD_COLUMN_OFFSET ,
pPage->GetPageSize().GetHeight() - BOARDER_SIZE - FIRST_ROW_OFFSET,
&azimuthPhoto);
painter.DrawImage(BOARDER_SIZE ,
pPage->GetPageSize().GetHeight() - BOARDER_SIZE - SECOND_ROW_OFFSET,
&azimuthPhoto);
painter.DrawImage(BOARDER_SIZE + SECOND_COLUMN_OFFSET ,
pPage->GetPageSize().GetHeight() - BOARDER_SIZE - SECOND_ROW_OFFSET,
&azimuthPhoto);
painter.DrawImage(BOARDER_SIZE + THIRD_COLUMN_OFFSET ,
pPage->GetPageSize().GetHeight() - BOARDER_SIZE - SECOND_ROW_OFFSET,
&azimuthPhoto);
painter.FinishPage();
/*digital signature
*
*/
/*
* Set some additional information on the PDF file.
*/
document.GetInfo()->SetCreator ( PdfString("CREATE A PDF") );
document.GetInfo()->SetAuthor ( PdfString("Victor Wiedemann") );
document.GetInfo()->SetTitle ( PdfString(" Results for Site") );
document.GetInfo()->SetSubject ( PdfString("create a table for PDE") );
document.GetInfo()->SetKeywords( PdfString("Test, PDE, Attenna;") );
/*
* The last step is to close the document.
*/
document.Close();
}
int main( int argc, char* argv[] )
{
/*
* Check if a filename was passed as commandline argument.
* If more than 1 argument or no argument is passed,
* a help message is displayed and the example application
* will quit.
return 0;
*/
if( argc != 2 )
{
PrintHelp();
return -1;
}
/*
* All podofo functions will throw an exception in case of an error.
*
* You should catch the exception to either fix it or report
* back to the user.
*
* All exceptions podofo throws are objects of the class PdfError.
* Thats why we simply catch PdfError objects.
*/
try {
/*
* Call the drawing routing which will create a PDF file
* with the filename of the output file as argument.
*/
PrintData( argv[1] );
} catch( const PdfError & eCode ) {
/*
* We have to check if an error has occurred.
* If yes, we return and print an error message
* to the commandline.
*/
eCode.PrintErrorMsg();
return eCode.GetError();
}
/*
* The PDF was created sucessfully.
*/
std::cout << std::endl
<< "Created a PDF file: " << argv[1] << std::endl <<
std::endl;
return 0;
}
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=267308311&iu=/4140
_______________________________________________
Podofo-users mailing list
Podofo-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/podofo-users