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
Code:
#include <QtGui>
int main(int argc, char *argv[])
{
bouton.show();
m_Printer.
setOutputFormat(QPrinter::PdfFormat);
m_Printer.setOutputFileName("baseprinted.pdf");
QRect rect
= painter.
viewport();
QRect OldWindow
= painter.
window();
QPixmap m_Image
("c:\\picture.jpg");
QSize size
= m_Image.
size ();
size.scale (rect.size (), Qt::KeepAspectRatio);
painter.setViewport (0, 0, size.width (), size.height ());
painter.setWindow (m_Image.rect ());
painter.drawPixmap (m_Image.rect(), m_Image);
painter.setViewport(rect);
painter.setWindow(OldWindow);
QFont ComicMS
("Comic Sans MS",
40);
painter.setFont(ComicMS);
tmp.setX(0);
tmp.setY(size.height());
tmp.setWidth(rect.width());
tmp.setHeight(rect.height()-size.height());
painter.drawText(tmp, Qt::AlignHCenter | Qt::AlignTop, "Texte <b>!</b>");
return app.exec();
}
The second one, enables me to put html, but the picture hasn't the good dimensions(it could be larger)
Code:
m_Printer.
setOutputFormat(QPrinter::PdfFormat);
m_Printer.setOutputFileName("printed.pdf");
m_Printer.
setPageMargins(0,
0,
0,
0,
QPrinter::Point);
Docu.
addResource(QTextDocument::ImageResource,
QUrl( "monImage.jpg" ), m_Image.
scaled( m_Printer.
paperRect(QPrinter::Point).
size().
toSize() ,Qt
::KeepAspectRatio, Qt
::SmoothTransformation) );
Docu.setHtml("<img src=\"monImage.jpg\"/><br/><b>Hello</b> World");
Docu.print(&m_Printer);
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.
Code:
m_Printer.
setOutputFormat(QPrinter::PdfFormat);
m_Printer.setOutputFileName("mixteprinted.pdf");
QRect rect
= painter.
viewport();
QRect OldWindow
= painter.
window();
QSize size
= m_Image.
size ();
size.scale (rect.size (), Qt::KeepAspectRatio);
painter.setViewport (0, 0, size.width (), size.height ());
painter.setWindow (m_Image.rect ());
Docu.setHtml("<img src=\"monImage.jpg\"/><br/><b>Hello</b> World");
Docu.drawContents(&painter);
Docu.setHtml("<b>Hello</b> GUYS");
painter.setViewport(rect);
painter.setWindow(OldWindow);
QFont ComicMS
("Comic Sans MS",
40);
Docu.setDefaultFont(ComicMS);
Docu.drawContents(&painter);
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 ;)
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:
Code:
void PdfGenerate::generatePdf() {
printer.
setOrientation(QPrinter::Portrait);
//set theorientation of the paper printer.
setOutputFormat(QPrinter::PdfFormat);
//make that printer as a PDF printer.setOutputFileName("test.pdf"); //set the PDF file name
printer.
setPaperSize(QPrinter::Letter);
//set paper size printer.setFullPage(true); //let our app use the full page (we handle margins ourself)
setupLetterPoints(); //set up reference points for our coordinate system
QPainter painter
(&printer
);
//make a painter, which uses this printer,
clause.setHtml("<img src=\"logo.jpg\"><br><b>clause:</b> Here is some text<br>Here is another line of text");
clause.
setDefaultFont(QFont(fontFamily, fontsize
));
QRectF r5
(leftmargin, topmargin,
500,
500);
//space our textdocument is allowed to take up and where it should be located drawrichtext(&painter, r5, 0, clause);
painter.end(); //done drawing, so save the PDF
//open the PDF for viewing:
}
//set up the point coordinates for a letter sheet of paper:
void PdfGenerate::setupLetterPoints() {
//(86 / inch) so it seems..
pagewidth = 731; //8.5 inch
pageheight = 946; //11 inch
margin = 43; // 0.5 inch margin
topmargin = margin;
bottommargin = pageheight - margin;
leftmargin = margin;
rightmargin = pagewidth - margin;
vertmarginwidth = pageheight - (margin * 2);
horizmarginwidth = pagewidth - (margin * 2);
//font sizes:
fontFamily = "Times New Roman";
fontsize = 15;
}
text.
setPageSize(QSize(rect.
width(), QWIDGETSIZE_MAX
));
const int height = qRound(layout->documentSize().height());
int y = rect.y();
if (flags & Qt::AlignBottom)
y += (rect.height() - height);
else if (flags & Qt::AlignVCenter)
y += (rect.height() - height)/2;
context.
palette.
setColor(QPalette::Text, painter
->pen
().
color());
painter->save();
painter->translate(rect.x(), rect.y());
layout->draw(painter, context);
painter->restore();
}