[Interest] QTextDocument | QPdfWriter | Internal Links

Igor Mironchik igor.mironchik at gmail.com
Sat Feb 20 08:01:38 CET 2016


Hi,

I'm trying to understand how to work with internal links in PDF.

Here is the simple code:

#include <QTextDocument>
#include <QPdfWriter>
#include <QApplication>
#include <QTextCharFormat>
#include <QTextCursor>


int main( int argc, char ** argv )
{
     QApplication app( argc, argv );

     QPdfWriter pdf( "/home/igor/test.pdf" );

     QTextDocument doc;

     QTextCharFormat fmt;
     fmt.setAnchorName( "title" );
     fmt.setAnchor( true );

     QTextCursor c( &doc );
     c.insertText( 
"Title\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", fmt );

     QTextCharFormat link;
     link.setAnchorHref( "title" );
     link.setAnchor( true );

     c.insertText( "Go to title", link );

     doc.print( &pdf );

     return 0;
}

But when I click on "Go to title" in PDF, viewer tries to open file 
"title" instead of navigating through the PDF.

How to correctly set anchors and links in QTextDocument?



More information about the Interest mailing list