[Interest] QTextDocument, QPdfWriter, QSvgRender

Igor Mironchik igor.mironchik at gmail.com
Sat Feb 20 09:23:04 CET 2016


The answer to my own question...

voidprintDocument(constQTextDocument&doc,QPdfWriter&pdf,constQRectF&body 
){QTextBlockblock =doc.begin();QPainterp;p.begin(&pdf );qreal y 
=0.0;while(block.isValid()){QTextBlock::Iteratorit 
=block.begin();QTextImageFormatimageFormat;boolisObject 
=false;boolisImage =false;boolisBreak =false;for(;!it.atEnd();++it 
){constQStringtxt =it.fragment().text();isObject 
=txt.contains(QChar::ObjectReplacementCharacter);isImage =isObject 
&&it.fragment().charFormat().isImageFormat();isBreak =isObject 
&&(it.fragment().charFormat().objectType()==c_pageBreakType );if(isImage 
)imageFormat =it.fragment().charFormat().toImageFormat();}if(isBreak 
){pdf.newPage();y =0.0;}elseif(isImage 
){QSvgRenderersvg(imageFormat.name());QSizes 
=svg.viewBox().size();if(s.width()>body.size().width()||s.height()>body.size().height())s.scale(QSize(body.size().width(),body.size().height()),Qt::KeepAspectRatio);if((y 
+s.height())>body.height()){pdf.newPage();y 
=0.0;}p.save();p.translate((body.size().width()-s.width())/2,y 
);svg.render(&p,QRectF(0,0,s.width(),s.height()));y 
+=s.height();p.restore();}else{constQRectFr 
=block.layout()->boundingRect();block.layout()->setPosition(QPointF(0.0,0.0));if((y 
+r.height())>body.height()){pdf.newPage();y 
=0.0;}block.layout()->draw(&p,QPointF(0.0,y ));y +=r.height();}block 
=block.next();}p.end();}


On 19.02.2016 12:50, Igor Mironchik wrote:
> Hi,
>
> I want to print QTextDocument to PDF, it's simple. My question: is it 
> possible to say to QTextDocument to draw images as vector graphics if 
> they are SVG images?
>
> Thanks.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20160220/b6e28ec4/attachment.html>


More information about the Interest mailing list