Results 1 to 2 of 2

Thread: Print RichText and a scaled Picture

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2006
    Location
    Hawaii
    Posts
    130
    Thanks
    48
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Print RichText and a scaled Picture

    Here is how I worked out a similar issue for rich text PDF based off of Qwt example:
    Qt Code:
    1. void PdfGenerate::generatePdf() {
    2.  
    3.  
    4. QPrinter printer; //create a printer
    5. printer.setOrientation(QPrinter::Portrait); //set theorientation of the paper
    6. printer.setOutputFormat(QPrinter::PdfFormat); //make that printer as a PDF
    7. printer.setOutputFileName("test.pdf"); //set the PDF file name
    8. printer.setPaperSize(QPrinter::Letter); //set paper size
    9. printer.setFullPage(true); //let our app use the full page (we handle margins ourself)
    10.  
    11. setupLetterPoints(); //set up reference points for our coordinate system
    12.  
    13. QPainter painter(&printer); //make a painter, which uses this printer,
    14.  
    15.  
    16.  
    17. QTextDocument clause;
    18. QPixmap logo("logo.jpg", "JPG");
    19. clause.addResource(QTextDocument::ImageResource, QUrl("logo.jpg"), logo);
    20. clause.setHtml("<img src=\"logo.jpg\"><br><b>clause:</b> Here is some text<br>Here is another line of text");
    21. clause.setDefaultFont(QFont(fontFamily, fontsize));
    22. QRectF r5(leftmargin, topmargin, 500, 500); //space our textdocument is allowed to take up and where it should be located
    23. drawrichtext(&painter, r5, 0, clause);
    24.  
    25. painter.end(); //done drawing, so save the PDF
    26. //open the PDF for viewing:
    27. QString sOldPath = QCoreApplication::applicationDirPath();
    28. QDesktopServices::openUrl(QUrl::fromLocalFile(sOldPath + "/test.pdf"));
    29.  
    30.  
    31.  
    32. }
    33.  
    34. //set up the point coordinates for a letter sheet of paper:
    35. void PdfGenerate::setupLetterPoints() {
    36. //(86 / inch) so it seems..
    37. pagewidth = 731; //8.5 inch
    38. pageheight = 946; //11 inch
    39.  
    40. margin = 43; // 0.5 inch margin
    41.  
    42. topmargin = margin;
    43. bottommargin = pageheight - margin;
    44. leftmargin = margin;
    45. rightmargin = pagewidth - margin;
    46.  
    47. vertmarginwidth = pageheight - (margin * 2);
    48. horizmarginwidth = pagewidth - (margin * 2);
    49.  
    50. //font sizes:
    51. fontFamily = "Times New Roman";
    52. fontsize = 15;
    53.  
    54.  
    55. }
    56.  
    57. void PdfGenerate::drawrichtext(QPainter *painter, QRectF &rect, int flags, QTextDocument &text) {
    58.  
    59. text.setPageSize(QSize(rect.width(), QWIDGETSIZE_MAX));
    60.  
    61. QAbstractTextDocumentLayout* layout = text.documentLayout();
    62.  
    63. const int height = qRound(layout->documentSize().height());
    64. int y = rect.y();
    65. if (flags & Qt::AlignBottom)
    66. y += (rect.height() - height);
    67. else if (flags & Qt::AlignVCenter)
    68. y += (rect.height() - height)/2;
    69.  
    70. QAbstractTextDocumentLayout::PaintContext context;
    71. context.palette.setColor(QPalette::Text, painter->pen().color());
    72.  
    73. painter->save();
    74.  
    75. painter->translate(rect.x(), rect.y());
    76. layout->draw(painter, context);
    77.  
    78. painter->restore();
    79. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by tpf80; 29th December 2008 at 21:41.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.