QT生成PDF文件

QString temp="";
    //m_File_Content_Text_Edit->selectAll();
    QString t_File_Content = m_File_Content_Text_Edit->toPlainText();


// pdf文件生成
    QPrinter printer;
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setOutputFileName(filename);
    QPainter painter;
    if (! painter.begin(&printer))
    { // failed to open file
        QMessageBox::information(NULL, "提示", "PDF打开出错", QMessageBox::Yes, QMessageBox::Yes);
        return ;
    }
    int index = 0;
    int x=0;
    const int pageLine = 40;//PDF一页行数
   

    //我的PDF中做了表格,t_File_Content前五行是表头内容
    painter.setFont(QFont("Arial", 30));
    //painter.setPen(Qt::blue);
    painter.drawText(250,0,"组件接口检测报告");
    painter.setFont(QFont("Arial", 10));
    //painter.drawText(rect(), Qt::AlignCenter, "Qt");
    //QRectF biaoge(QPoint(100, 20), QSize(100, 100));
    //painter.drawRect(biaoge);


    //biaoge.setX();
    int biaotou_Count = 0;
    int y=40,size;

    for(index = 0;'\0' != t_File_Content[index];index++)
    {

        if('\n' == t_File_Content[index])
        {
            if((y+120)>=1000 && temp != "")
            {
                if (! printer.newPage())
                { // failed to open file
                    QMessageBox::information(NULL, "提示", "PDF新建页数出错", QMessageBox::Yes, QMessageBox::Yes);
                    return ;
                }
                y = 20;
            }
            if("" == temp)
            {
                temp == "";
                continue;
            }

            if(biaotou_Count < 5)//当前是表头数据
            {
                if(0 == biaotou_Count)
                {
                    painter.setPen(Qt::blue);
                    painter.drawRect(QRectF(100,40,100,40));
                    painter.setPen(Qt::black);
                    painter.drawText(QRectF(100,40,100,40), Qt::AlignCenter, tr("检测组件"));
                    painter.setPen(Qt::blue);
                    painter.drawRect(QRectF(200,40,200,40));
                    painter.setPen(Qt::black);
                    painter.drawText(QRectF(200,40,200,40), Qt::AlignCenter, tr(temp.toStdString().c_str()));
                }
                else if(1 == biaotou_Count)
                {
                    painter.setPen(Qt::blue);
                    painter.drawRect(QRectF(400,40,100,40));
                    painter.setPen(Qt::black);
                    painter.drawText(QRectF(400,40,100,40), Qt::AlignCenter, tr("检测日期"));
                    painter.setPen(Qt::blue);
                    painter.drawRect(QRectF(500,40,200,40));
                    painter.setPen(Qt::black);
                    painter.drawText(QRectF(500,40,200,40), Qt::AlignCenter, tr(temp.toStdString().c_str()));
                }
                else if(2 == biaotou_Count)
                {
                    painter.setPen(Qt::blue);
                    painter.drawRect(QRectF(100,80,100,40));
                    painter.setPen(Qt::black);
                    painter.drawText(QRectF(100,80,100,40), Qt::AlignCenter, tr("版本号"));
                    painter.setPen(Qt::blue);
                    painter.drawRect(QRectF(200,80,200,40));
                    painter.setPen(Qt::black);
                    painter.drawText(QRectF(200,80,200,40), Qt::AlignCenter, tr(temp.toStdString().c_str()));
                }
                else if(3 == biaotou_Count)
                {
                    painter.setPen(Qt::blue);
                    painter.drawRect(QRectF(400,80,100,40));
                    painter.setPen(Qt::black);
                    painter.drawText(QRectF(400,80,100,40), Qt::AlignCenter, tr("检测者"));
                    painter.setPen(Qt::blue);
                    painter.drawRect(QRectF(500,80,200,40));
                    painter.setPen(Qt::black);
                    painter.drawText(QRectF(500,80,200,40), Qt::AlignCenter, tr(temp.toStdString().c_str()));
                }
                else
                {
                    painter.setPen(Qt::blue);
                    painter.drawRect(QRectF(100,120,100,40));
                    painter.setPen(Qt::black);
                    painter.drawText(QRectF(100,120,100,40), Qt::AlignCenter, tr("组件描述"));
                    painter.setPen(Qt::blue);
                    painter.drawRect(QRectF(200,120,500,40));
                    painter.setPen(Qt::black);
                    painter.drawText(QRectF(200,120,500,40), Qt::AlignCenter, tr(temp.toStdString().c_str()));
                    painter.setPen(Qt::blue);
                    painter.drawRect(QRectF(100,160,600,40));
                    painter.setPen(Qt::black);
                    painter.drawText(QRectF(100,160,600,40), Qt::AlignCenter, tr("检测结果"));
                    y=200;
                    x=200;
                }
                temp = "";
                biaotou_Count++;
                continue;
            }

            //painter.drawText(0,25*x,temp);//行间距,25个像素
            //x++;
            size = temp.size();
            if(temp[size-1] == '.')
            {
                painter.setPen(Qt::blue);
                painter.drawRect(QRectF(100,y,600,120));
                painter.setPen(Qt::black);
                if(isError(temp))
                    painter.setPen(Qt::red);

                painter.drawText(QRectF(100,y,600,120), Qt::AlignCenter, tr(temp.toStdString().c_str()));
                //painter.setPen(Qt::blue);
                y+=120;

                temp = "";
                continue;
            }
            else
            {
                temp +='\n';
            }
        }
        else
        {
            temp += t_File_Content[index];
        }

    }

    painter.end();

 

转载于:https://www.cnblogs.com/LCCRNblog/p/4974152.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值