0

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

ng2b30
  • 351
  • 6
  • 18
  • Please show a [mre]. Is Qt even needed to make a small example? If not, don't include Qt. – Ted Lyngmo Jan 27 '22 at 11:33
  • removed the tag. Thanks – ng2b30 Jan 27 '22 at 11:38
  • Good. Now, if you could provide a minimal example of code that we can actually compile and that will reproduce the error, it would be great. I made a small example of your code but that doesn't cause any problems so we're lacking information. – Ted Lyngmo Jan 27 '22 at 11:47
  • @TedLyngmo Thanks you for your suggestion. Try my best to remove all the qt elements. – ng2b30 Jan 27 '22 at 12:25
  • Better - but you can't compile this, can you? Please verify that it compiles on your computer first and then update the question. – Ted Lyngmo Jan 27 '22 at 12:29
  • Fwiw: I corrected the errors in your code above to make it compile and I don't get any problems except memory leaks (since you don't `delete` what you've `new`ed). I had to remove the line `this->drawHyperLinkText(&painter, pPage1, pPage2, document);` though since there is no `this` in `main`. – Ted Lyngmo Jan 27 '22 at 12:35
  • I has posted all my code. The problem is that I would crash in this line 'pAnnotation->SetDestination(pPage1); ' I dun know that is it a right way to do the link to anchor on current page? – ng2b30 Jan 27 '22 at 12:40
  • Ok, now it became worse. With all that Qt stuff in there, it's not _minimal_ anymore. We can't even compile it since we don't have `"ui_mainwindow.h"` so it's not _reproducible_ either. Please remove the Qt dependencies. – Ted Lyngmo Jan 27 '22 at 12:43
  • Removed all the Qt dependencies. Please have a look. Thanks – ng2b30 Jan 27 '22 at 12:49
  • Better again :-) Though I'm surprised that your compiler lets you compile the line `string filename = argv[0] + "testtesttesttest.pdf";` - mine don't. You must `#include ` and you can't concatenate C strings like that. It should be `std::string filename = std::string(argv[0]) + "testtesttesttest.pdf";` - And the line `this->drawHyperLinkText(&painter, pPage1, pPage2, document);` should definitely not compile. The only thing left are memory leaks, nothing else that I can see. – Ted Lyngmo Jan 27 '22 at 12:53
  • I changed this part wrongly. I tried to debug in qt and it crashed in this line. pAnnotation->SetDestination(pPage1); but I dun know the reason as I have stored the page1 and page2. – ng2b30 Jan 27 '22 at 13:02
  • If I change it from pPage1 to pPage2(annotation page), then it works – ng2b30 Jan 27 '22 at 13:10
  • I only fixed the memory leak and compiled the program with `-g -fsanitize=address,undefined,leak` and it reports no problems. I also used `valgrind` - still no problems reported. You still have the line `this->drawHyperLinkText(&painter, pPage1, pPage2, document );` in the code. What is that suppsoed to do? It can't compile with that line in there. – Ted Lyngmo Jan 27 '22 at 13:11
  • @TedLyngmo I want to build the table of content. When I click the text in the table of contents page, it would route to that page. Also I want to create a text in page 10 and click ,it would move to a specific page like page 2. Can you do such example for me please? thanks. – ng2b30 Jan 27 '22 at 14:22
  • If you can you make a reproducible example I will have a look. – Ted Lyngmo Jan 27 '22 at 15:28

0 Answers0