Results 1 to 2 of 2

Thread: Print RichText and a scaled Picture

  1. #1
    Join Date
    Dec 2008
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Print RichText and a scaled Picture

    Hello,

    I want to print on a paper(or pdf...) a picture, scaled to the maximum of the page, according to the picture aspectRatio. Then I want to display under this picture some RichText.
    I have found three solutions, but each one has a serious drawback

    The fisrt one correctly resize the picture, but doesn't enable me to put RichText(only normal text). I made an example below
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5.  
    6. QApplication app(argc, argv);
    7. QPushButton bouton("Just a Button");
    8. bouton.show();
    9.  
    10.  
    11. QPrinter m_Printer(QPrinter::HighResolution);
    12. m_Printer.setOutputFormat(QPrinter::PdfFormat);
    13. m_Printer.setOutputFileName("baseprinted.pdf");
    14. QPainter painter (&m_Printer);
    15. QRect rect = painter.viewport();
    16. QRect OldWindow= painter.window();
    17. QPixmap m_Image("c:\\picture.jpg");
    18. QSize size = m_Image.size ();
    19. size.scale (rect.size (), Qt::KeepAspectRatio);
    20. painter.setViewport (0, 0, size.width (), size.height ());
    21. painter.setWindow (m_Image.rect ());
    22. painter.drawPixmap (m_Image.rect(), m_Image);
    23. painter.setViewport(rect);
    24. painter.setWindow(OldWindow);
    25. QRect tmp;
    26. QFont ComicMS("Comic Sans MS", 40);
    27. painter.setFont(ComicMS);
    28. tmp.setX(0);
    29. tmp.setY(size.height());
    30. tmp.setWidth(rect.width());
    31. tmp.setHeight(rect.height()-size.height());
    32. painter.drawText(tmp, Qt::AlignHCenter | Qt::AlignTop, "Texte <b>!</b>");
    33. return app.exec();
    34. }
    To copy to clipboard, switch view to plain text mode 

    The second one, enables me to put html, but the picture hasn't the good dimensions(it could be larger)
    Qt Code:
    1. m_Printer.setOutputFormat(QPrinter::PdfFormat);
    2. m_Printer.setOutputFileName("printed.pdf");
    3. m_Printer.setPageMargins(0,0,0,0,QPrinter::Point);
    4. Docu.addResource(QTextDocument::ImageResource, QUrl( "monImage.jpg" ), m_Image.scaled( m_Printer.paperRect(QPrinter::Point).size().toSize() ,Qt::KeepAspectRatio, Qt::SmoothTransformation) );
    5. Docu.setHtml("<img src=\"monImage.jpg\"/><br/><b>Hello</b> World");
    6. Docu.print(&m_Printer);
    To copy to clipboard, switch view to plain text mode 

    Then the last solution. It works well, but the text is to small(it's logic). And I dont know how to add other text at the rigtht size.
    Qt Code:
    1. m_Printer.setOutputFormat(QPrinter::PdfFormat);
    2. m_Printer.setOutputFileName("mixteprinted.pdf");
    3. QPainter painter (&m_Printer);
    4. QRect rect = painter.viewport();
    5. QRect OldWindow= painter.window();
    6. QSize size = m_Image.size ();
    7. size.scale (rect.size (), Qt::KeepAspectRatio);
    8. painter.setViewport (0, 0, size.width (), size.height ());
    9. painter.setWindow (m_Image.rect ());
    10. Docu.addResource(QTextDocument::ImageResource, QUrl( "monImage.jpg" ), m_Image );
    11. Docu.setHtml("<img src=\"monImage.jpg\"/><br/><b>Hello</b> World");
    12. Docu.drawContents(&painter);
    13. Docu.setHtml("<b>Hello</b> GUYS");
    14. painter.setViewport(rect);
    15. painter.setWindow(OldWindow);
    16. QFont ComicMS("Comic Sans MS", 40);
    17. Docu.setDefaultFont(ComicMS);
    18. Docu.drawContents(&painter);
    To copy to clipboard, switch view to plain text mode 


    Each solution has a drawback that I cant avoid. I'm focused on the last solution, where my goal is then just to add some text at the right size after the Docu.drawContents(&painter);
    But after many tryes, I can't find tsomething similar to painter.drawText() that put the text at the right size.

    If you have any Ideas?

    Thank you and Merry Christmas

  2. #2
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.