开发者

QT利用QPdfWriter实现绘制PDF(支持表单输出)

之前打印PDF都是通过html形式来实现的,但是这次要做的东西,需要打印界面控件,所以需要使用QPdfWriter.

通过QPdfWriter来获取QPainter对象,就能实现在PDF上来画画啦.

代码生成的PDF如下所示:

QT利用QPdfWriter实现绘制PDF(支持表单输出)

代码效果如下所示:

QT利用QPdfWriter实现绘制PDF(支持表单输出)

代码如下所示:

//通过dialog来保存PDF路径
QString mainWindow::pdfSaveAs(QString fileName)
{
        QString file="";
 
        file = QFileDialog::getSaveFileName (
        this,                               //父组件
        "另存为",                              //标题
        QString("%1/%2").arg(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)).arg(""+fileName+".pdf"),
                                                            //设置路径, .表示当前路径,./表示更目录
        "PDF文件",     //过滤器,保存的类型
        Q_NULLPTR,
        QFileDialog::ShowDirsOnly);
 
        return file;
}
 
//y:表单的Y坐标
//horzBorder:水平外边距,写0表示没有边距
//row,column:行数和列数
//unitHeight:单元高度
//font:字体大小
//list:要写入的单元数据
void mainWindow::pdfDrawForm(QPainter* paint,int y,int horzBorder,int row,int column,int unitHeight,QFont &font,QStringList& list)
{
    paint->setFont(font);
 
    paint->setPen(QPen(QBrush(QColor(0,0,0)),2));
 
    int Width = paint->viewport().width()-horzBorder*2;
 
    int unitWidth = Width/column;
 
    for(int i=0;i<row;i++)
    {
        int x= horzBorder;
 
        for(int j=0;j<column;j++)
        {
            paint->drawText(QRect(x,y, unitWidth, unitHeight), Qt::AlignCenter,list[i*column+j]);
 
            paint->drawRect(QRect(x,y, unitWidth, unitHeight));
 
            x+=unitWidth;
        }
 
        y += unitHeiphpght;
    }
 
 
 
}
 
 
void mainWindow::pdfCreate(checkInfo &info)
{
 
//        //设置pdf保存的路径
//        QString file_path = pdfSaveAs(QString("表单%1").arg(QDateTime::currentDateTime().toString("MMdd-hh-mm")));
//        if(file_path.isEmpty())
//            return;
 
        QString file_path =QApplication::applicationDirPath()+"/test.pdf";  
        QFile pdfFile(file_path);
        pdfFile.open(QIODevice::WriteOnly);
        QPdfWriter* pWriter = new QPdfWriter(&pdfFile);
 
        //Init Page
        pWriter->setPageSize(QPagedPaintDevice::A4);
        pWriter->setResolution(300);    //设置dpi 每个平方英寸像素为300
        pWriter->setPageMargins(QMarginsF(30, 30, 30, 30));
 
 
        QPainter* pPainter = new QPainter(pWriter);
 
        //Init Font
        QFont font[5]={QFont("宋体",26,60),QFont("宋体",26,61),QFont("宋体",26,QFont::Normal),QFonjst("宋体",26,QFont::Normal),QFont("宋体",26,QFont::Normal)};
        font[0].setPixelSize(86);
        font[1].setPixelSize(61);
        font[2].setPixelSize(61);
        font[3].setPixelSize(42);
        font[4].setPixelSize(54);
 
        //Painter PDF
        qDebug()<<pPainter->viewport();
        int nPDFWidth = pPainter->viewport().width();
        int nPDFHeight = pPainter->viewport().height();
 
 
        //在10%的头部居中显示
        int y=10;
        pPainter->setFont(font[0]);
        pPainter->drawText(QRect(0,y, nPDFWidth, 100), Qt::AlignCenter,
        m_arg.printTitle);
        y+=140;
        pPainter->setPen(QPen(QBrush(QColor(0,0,0)),5));
        pPainter->drawLine(0,y,nPDFWidth,y);
        pPainter->drawLine(0,y+18,nPDFWidth,y+18);
 
        y+=60;
 
        pPainter->setFont(font[2]);
        pPainter->drawText(QRect(100,y, nPDFWidth/2, 70), Qt::AlignVCenter | Qt::AlignLeft,
        QString("器件型号: %1").arg(info.dev.name));
 
        pPainter->setFont(font[2]);
        pPainter->drawText(QRect(nPDFWidth/2+100,y, nPDFWidth/2-100, 70), Qt::AlignVCenter | Qt::AlignLeft,
        QString("器件编号: %1").arg(info.devNum));
 
        y+=90;
 
        pPainter->setFont(font[2]);
        pPainter->drawText(QRect(100,y, nPDFWidth/2, 70), Qt::AlignVCenter | Qt::AlignLeft,
        QString("装车车号: %1").arg(info.devCar));
 
        pPainter->setFont(font[2]);
        pPainter->drawText(QRect(nPDFWidth/2+100,y, nPDFWidth/2-100, 70), Qt::AlignVCenter | Qt::AlignLeft,
        QString("试验日期: %1").arg(info.checkDate));
 
        y+=110;
 
 
        pPainter->setFont(font[1]);
        pPainter->drawText(QRect(0,y, nPDFWidth, 80), Qt::AlignVCenter | Qt::AlignLeft,
        "减 振 器 标 准 参 数");
 
        y+=120;
 
 
        QStringList list;
        list<<"速度"<<"压力"<<"拉力"<<"阻尼系数"<<"不对称率"<<"正允差"<<"负允差";
        list<<info.dev.speed+"m/s"<<info.dev.yaLi+"kN"<<info.dev.laLi+"kN"<<\
              info编程客栈.dev.zhuNi+"kNs/m"<<info.dev.buDuiC+"%"<<info.dev.zYunCha+"%"<<info.dev.fYunCha开发者_JAVA+"%";
        pdfDrawForm(pPainter,y,0,2,7,100,font[3],list);
 
        y+=260;
 
 
        pPainter->setFont(font[1]);
        pPainter->drawText(QRect(0,y, nPDFWidth, 80), Qt::AlignVCenter | Qt::AlignLeft,
        "示 功 图");
        y+=100;
 
        //获取界面图片
        int imageBorder=150;        //设置图片水平边距为150
 
        QPixmap pixmap = QPixmap::grabWidget(m_checkChart, m_checkChart->rect());
 
        float x = (float)(nPDFWidth-imageBorder*2)/(float)pixmap.width();
 
        pixmap= pixmap.scaled(nPDFWidth-imageBorder*2, x*pixmap.height(),Qt::IgnoreASPectRatio);    //根据大小比例,来放大缩小图片
 
        pPainter->drawPixmap(imageBorder, y, pixmap);
 
        y+=pixmap.height()+90;
 
        pPainter->setFont(font[1]);
        pPainter->drawText(QRect(0,y, nPDFWidth, 80), Qt::AlignVCenter | Qt::AlignLeft,
        "减 振 器 测 试 结 果");
 
        y+=120;
 
        list.clear();
        list<<"拉伸行程:"<<QString::asprintf("%.2fmm",info.laSheng)<<"压缩行程:"<<QString::asprintf("%.2fmm",info.yaSuo)<<"最大速度:"<<QString::asprintf("%.3fm/s",info.speed)<<"阻尼系数:"<<QString::asprintf("%.2fkNs/m",info.speed)
             <<"最大压力:"<<QString::asprintf("%.2fkN",info.max)<<"压力偏差:"<<QString::asprintf("%.2f%",info.maxBaiFengb)<<"最大拉力:"<<cDmrVvQ;QString::asprintf("%.2fkN",info.min)<<"拉力偏差:"<<QString::asprintf("%.2f%",info.minBaiFengb)
              <<"不对称率:"<<QString::asprintf("%.2f%",info.buduic)<<"运行时间:"<<QString::asprintf("%dS",info.runDate)<<"试验结果:"<<info.result<<"曲线结论:"<<info.ChartResult;
        pdfDrawForm(pPainter,y,0,3,8,100,font[3],list);
 
        y+=400;
 
        pPainter->setFont(font[4]);
        pPainter->drawText(QRect(50,y, nPDFWidth/3, 80), Qt::AlignVCenter | Qt::AlignLeft,
        QString( "试验员: %1").arg(m_userLoginInfo->name));
 
        pPainter->drawText(QRect(nPDFWidth/3+50,y, nPDFWidth/3, 80), Qt::AlignVCenter | Qt::AlignLeft,
        QString( "检查员签字: "));
 
        pPainter->drawText(QRect(nPDFWidth/3*2,y, nPDFWidth/3, 80), Qt::AlignVCenter | Qt::AlignLeft,
        QString( "签字日期: "));
 
       // pWriter->newPage(); //写下一页
 
        //绘制完毕
        delete pPainter;
        delete pWriter;
        pdfFile.close();
 
 
        //通过其它PDF阅读器来打开PDF
        QDesktopServices::openUrl(QUrl::fromLocalFile(file_path));
}

到此这篇关于QT利用QPdfWriter实http://www.devze.com现绘制PDF(支持表单输出)的文章就介绍到这了,更多相关QT QPdfWriter绘制PDF内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜