I am trying to use PODOFO in c++ to create a text in PDF. After clicking the text, it can move to a specific page/ area in the same PDF. My final goal is to create a table of content in PDF. The following code is what I am doing.
#include <iostream>
#include <string>
#include <podofo/podofo.h>
using namespace PoDoFo;
using namespace std;
int main( int argc, char* argv[] )
{
try {
string filename = std::string(argv[0]) + "testtesttesttest.pdf";
PdfStreamedDocument* document = new PdfStreamedDocument(filename.c_str());
PdfPage* pPage1;
PdfPainter painter;
PdfFont* pFont;
pPage1 = document->CreatePage( PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ) );
painter.SetPage(pPage1);
if( !pPage1 ) {
PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
}
pFont = document->CreateFont( "Arial" );
if( !pFont ) {
PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
}
pFont->SetFontSize( 18.0 );
painter.SetFont( pFont );
painter.DrawText( 56.69, pPage1->GetPageSize().GetHeight() - 56.69, "Hello World!" );
PdfPage* pPage2 = document->CreatePage( PdfPage::CreateStandardPageSize( ePdfPageSize_Letter ) );
painter.SetPage( pPage1 );
this->drawHyperLinkText(&painter, pPage1, pPage2, document );
//painter.SetPage( pPage2 );
painter.FinishPage();
document->GetInfo()->SetCreator ( PdfString("examplehelloworld - A PoDoFo test application") );
document->GetInfo()->SetAuthor ( PdfString("Dominik Seichter") );
document->GetInfo()->SetTitle ( PdfString("Hello World 222") );
document->GetInfo()->SetSubject ( PdfString("Testing the PoDoFo PDF Library") );
document->GetInfo()->SetKeywords( PdfString("Test;PDF;Hello World;") );
document->Close();
} catch( const PdfError & eCode ) {
eCode.PrintErrorMsg();
}
return 0;
}
void drawHyperLinkText( PdfPainter* pPainter, PdfPage* pPage1, PdfPage* pPage2, PdfStreamedDocument* pDocument )
{
PdfString sJap(reinterpret_cast<const pdf_utf8*>("test"));
PdfAnnotation* pAnnotation = pPage2->CreateAnnotation( ePdfAnnotation_Link, PdfRect( 400.0, 200.0, 20.0, 20.0 ) );
PdfString sGerman(reinterpret_cast<const pdf_utf8*>("Unicode Umlauts: ÄÖÜß"));
pAnnotation->SetTitle( sGerman );
pAnnotation->SetContents( sJap );
pAnnotation->SetOpen( true );
pAnnotation->SetDestination(pPage1);
}
I dun know I am doing right or not. I create two pages and want to add the text in page. When I click the text, it would move back to page 1. But it always crashes at the final line pAnnotation->SetDestination(pPage1);
.
May anyone give me some helps or an example to do that? Thanks