PDA

View Full Version : Print RichText and a scaled Picture



kray
24th December 2008, 11:22
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


#include <QtGui>

int main(int argc, char *argv[])
{

QApplication app(argc, argv);
QPushButton bouton("Just a Button");
bouton.show();


QPrinter m_Printer(QPrinter::HighResolution);
m_Printer.setOutputFormat(QPrinter::PdfFormat);
m_Printer.setOutputFileName("baseprinted.pdf");
QPainter painter (&m_Printer);
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);
QRect tmp;
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)


m_Printer.setOutputFormat(QPrinter::PdfFormat);
m_Printer.setOutputFileName("printed.pdf");
m_Printer.setPageMargins(0,0,0,0,QPrinter::Point);
QTextDocument Docu;
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.


m_Printer.setOutputFormat(QPrinter::PdfFormat);
m_Printer.setOutputFileName("mixteprinted.pdf");
QPainter painter (&m_Printer);
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 ());
QTextDocument Docu;
Docu.addResource(QTextDocument::ImageResource, QUrl( "monImage.jpg" ), m_Image );
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 ;)

tpf80
29th December 2008, 21:56
Here is how I worked out a similar issue for rich text PDF based off of Qwt example:


void PdfGenerate::generatePdf() {


QPrinter printer; //create a printer
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,



QTextDocument clause;
QPixmap logo("logo.jpg", "JPG");
clause.addResource(QTextDocument::ImageResource, QUrl("logo.jpg"), logo);
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:
QString sOldPath = QCoreApplication::applicationDirPath();
QDesktopServices::openUrl(QUrl::fromLocalFile(sOld Path + "/test.pdf"));



}

//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;


}

void PdfGenerate::drawrichtext(QPainter *painter, QRectF &rect, int flags, QTextDocument &text) {

text.setPageSize(QSize(rect.width(), QWIDGETSIZE_MAX));

QAbstractTextDocumentLayout* layout = text.documentLayout();

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;

QAbstractTextDocumentLayout::PaintContext context;
context.palette.setColor(QPalette::Text, painter->pen().color());

painter->save();

painter->translate(rect.x(), rect.y());
layout->draw(painter, context);

painter->restore();
}