QTextDocument mix of headline picture and HTML formated text



  • My program has following informations in a list:

    • QTextLine for headline
    • QTextEdit for some informations (will be translated into html code for some format)
    • QImage which was rendered in same application with drawing widget

    I want to put these informations into a QTextDocument and print it as PDF in the following order per page:

    Headline

    Image

    Information

    I would like to know how i can put them into this QTextDocument. Except for QTextDocument::setHtml(...) and ::setPlainText(...) there is no way to add images. Can you help me?

    lg Enforcer


  • Lifetime Qt Champion

    Hi,

    You might be interested by the "Rich Text Processing":http://qt-project.org/doc/qt-4.8/richtext.html documentation

    Hope it helps



  • Thanks, now i can have Headline, Image and Information in my PDF, but now i have another problem.

    I have a list of many items which consists of headline, image and information, and I want to have each item on a single page each (item[0] at first page, item[1] at second page etc.). How can I add a new page?

    Please show me an example code, I read many forums which discuss the QTextDocument and new page problem, and neither of them explains how to manually make a new page.


  • Lifetime Qt Champion

    IIRC you have to add a QTextBlockFormat and set it's page break policy to QTextFormat::PageBreak_AlwaysAfter



  • This is what I got so far for my "printPdf" function:

    • create QPrinter and set the necessary settings like output format and file name
    • create QTextDocument instance
    • create QTextCursor and use instance of QTextDocument
    • for each item in the list:
      ** cursor.insertHtml(<item's headline>);
      ** cursor.insertImage(<item's image>);
      ** cursor.insertHtml(<item's info>);
      ** document.print(&printer);
      ** make new page (this part is not working)

    I want to use html because it's easier (for me) for text format. From an example I found this code:

    @QTextEdit myEdit;
    QTextDocument* myDocument = new QTextDocument(&myEdit);
    myEdit.setDocument(myDocument);
    QTextCursor* myCursor = new QTextCursor(myDocument);

    QTextBlockFormat format;
    format.setBackground(Qt::red);
    myCursor->setBlockFormat(format);

    myCursor->insertText("the ");

    format.setBackground(Qt::green);
    myCursor->insertBlock(format);
    myCursor->insertText("fish ");

    format.setBackground(Qt::yellow);
    myCursor->insertBlock(format);
    myCursor->insertText("are ");

    format.setBackground(Qt::red);
    myCursor->insertBlock(format);
    myCursor->insertText("coming!");

    format.setBackground(Qt::green);
    myCursor->insertBlock(format);
    myCursor->insertText(QString("%1 blocks").arg(myDocument->blockCount()));
    myEdit.show();@

    I hope I get it in the right direction:

    @QTextBlockFormat format;
    format.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysAfter);

    foreach(Item item, itemlist)
    {
    cursor.insertBlock(format);
    cursor.insertHtml("<item's headline>");
    cursor.insertImage("<item's image>");
    cursor.insertHtml("<item's info>");
    }
    document.print(&printer);@

    Do you think this will work?


  • Lifetime Qt Champion

    AFAICT, it seems all right. The best is to run it.



  • It seems QTextDocument can't insert QImage properly. The resolution is bad. When calling QImage::save(QString) there is no problem with resolution, but the same image in PDF file created by QPrinter the resolution sucks. I also tried to set QPrinter(QPrinter::HighResolution), but no changes in image quality.


  • Lifetime Qt Champion

    Did you try to include the image through html ?



  • No, through QTextCursor::insertImage(QImage&) because I draw the image within the application via QPainter. But I already solved the problem to rise its resolution:

    "QTextCursor source code":https://code.google.com/p/phantomjs/source/browse/src/qt/src/gui/text/qtextcursor.cpp?r=08fc50d149745b078c9fd73a213c8c83307beaa2

    If you look at line 2311, then you see that the code transforms QImage into QTextImage format which would occure to this pixelized image in PDF. Afterwards it calls insertImage(QTextImageFormat&)

    Instead of inserting a QImage I transformed it into an QTextImageFormat myself:

    @QImage img(item->getImage(1920,1080));
    // I wrote class Item

    document.addResource(QTextDocument::ImageResource,
    QUrl(item->getName()), img);

    // Create QTextImageFormat and change its width, height alters according to image's width and height rate

    QTextImageFormat imgFormat;
    imgFormat.setName(item->getName());
    imgFormat.setWidth(480);
    cursor.insertImage(imgFormat);@

    The Image in pdf has a high resolution (of course pdf has a bigger size as well).

    QTextImageFormat keeps its width and height according to the loaded image (either QImage or from a PNG/JPEG), but the resolution drops. with QTextImage::setWidth(int) you can change width and height until it has the width you want. Resolution looks better because each pixel is compressed.


  • Lifetime Qt Champion

    Nice catch !

    I wonder if this behavior should not be changed to use the size of the image when creating the QTextImageFormat...



  • When you scale QImage BEFORE you make a QTextImageFormat from it then you would have the same pixelized problem as before. When you use MS Word you also compress the inserted picture make the size smaller.

    Last part of this "Export to PDF" is to make a text parser to transform item's information into html code with bold and italics. I thought of something like the parser of this text editor where words within asterisks become bold and words within underlines become italic. I also want to add (un)ordered lists.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.