• Qt导出PDF文件:使用QPdfWriter绘制PDF和使用QPrintPreviewDialog为绘制的PDF文件提供预览之后输出PDF


    一、使用QPdfWriter绘制PDF

     1 void MainWindow::exportPdf()
     2 {
     3     //一、选择保存pdf文件路径
     4     QString sPath = QFileDialog::getSaveFileName(this, tr("另存为"), "/", tr("Text Files (*.pdf)"));
     5     if(sPath.isEmpty())
     6     {
     7         return;
     8     }
     9     qDebug() << sPath;
    10 
    11     //二、创建pdf文件
    12     QFile pdfFile(sPath);
    13     pdfFile.open(QIODevice::WriteOnly);
    14 
    15     //三、创建生成pdf类,作为绘图设备
    16     QPdfWriter *pPdfWriter = new QPdfWriter(&pdfFile);
    17     pPdfWriter->setResolution(300);
    18     pPdfWriter->setPageSize(QPagedPaintDevice::A4);
    19     pPdfWriter->setPageMargins(QMarginsF(30, 30, 30, 30));
    20 
    21     //四、开始绘制PDF
    22     paintPdf(pPdfWriter);
    23 
    24     delete pPdfWriter;
    25     pdfFile.close();
    26 
    27     //通过其它PDF阅读器来打开刚刚绘制的PDF
    28     QDesktopServices::openUrl(QUrl::fromLocalFile(sPath));
    29 }

    二、使用QPrintPreviewDialog预览绘制的PDF然后输出

    在生成的打印预览界面点击打印按钮,就会弹出导出PDF窗口,选择要PDF文件要保存的路径,然后保存即可。

    如下图所示:

    (1)创建QPrintPreviewDialog对象

    1    QPrintPreviewDialog *m_pPrintPreviewDlg = new QPrintPreviewDialog();

    (2)将paintRequested()信号连接到槽

    1     connect(m_pPrintPreviewDlg, &QPrintPreviewDialog::paintRequested, this, &MainWindow::slot_previewPdf);

    (3)设置默认打印机的一些参数,最重要的是设置打印机输出格式为PDF

    1     m_pPrintPreviewDlg->printer()->setResolution(300);
    2     m_pPrintPreviewDlg->printer()->setOrientation(QPrinter::Portrait);
    3     //设置打印机输出格式为PDF格式
    4     m_pPrintPreviewDlg->printer()->setOutputFormat(QPrinter::PdfFormat);
    5     m_pPrintPreviewDlg->printer()->setPaperSize(QPagedPaintDevice::A4);
    6     m_pPrintPreviewDlg->printer()->setPageMargins(QMarginsF(30, 30, 30, 30));

    (4)槽函数

    1 void MainWindow::slot_previewPdf(QPrinter *printer)
    2 {
    3     paintPdf(printer);
    4 }

    (5)绘制PDF函数

      1 void MainWindow::paintPdf(QPagedPaintDevice *device)
      2 {
      3     QPainter *pPainter = new QPainter(device);
      4     qDebug() << pPainter->viewport();
      5     int nPdfWidth = pPainter->viewport().width();
      6     int nPdfHeight = pPainter->viewport().height();
      7 
      8     //绘制标题
      9     int y = 10;
     10     pPainter->setFont(QFont("宋体", 18, 36));
     11     pPainter->drawText(QRect(0, y, nPdfWidth, 100), Qt::AlignCenter, tr("实验操作记录报告"));
     12 
     13     //绘制报告相关信息
     14     y += 200;
     15     pPainter->setFont(QFont("宋体", 12, 20));
     16     int nLineSpace = 100;
     17     int nLineHeight = 60;
     18     pPainter->drawText(QRect(100, y, nPdfWidth, nLineHeight), Qt::AlignLeft, QString("实验编号:%1").arg("19990926001"));
     19     y += nLineSpace;
     20     pPainter->drawText(QRect(100, y, nPdfWidth, nLineHeight), Qt::AlignLeft, QString("操作仪器:%1").arg("TMachine9568"));
     21     y += nLineSpace;
     22     pPainter->drawText(QRect(100, y, nPdfWidth, nLineHeight), Qt::AlignLeft, QString("操作用户:%1").arg("Dr.Miss"));
     23     y += nLineSpace;
     24     pPainter->drawText(QRect(100, y, nPdfWidth, nLineHeight), Qt::AlignLeft, QString("开始时间:%1").arg("1999-09-26 17:50"));
     25     y += nLineSpace;
     26     pPainter->drawText(QRect(100, y, nPdfWidth, nLineHeight), Qt::AlignLeft, QString("结束时间:%1").arg("1999-09-26 22:30"));
     27 
     28     //绘制每个操作记录的各项标题 (标题宽的比例为 序号:操作时间:操作说明:试管和试剂信息 = 2:5:5:10)
     29     y += 150;
     30     pPainter->setFont(QFont("宋体", 14, 28));
     31     int nUnitW = nPdfWidth / (2 + 5 + 5+ 10);
     32 
     33     int nNoWidth = nUnitW * 2;
     34 
     35     int nTimeX = nNoWidth;
     36     int nTimeWidth = nUnitW * 5;
     37 
     38     int nExplainX = nTimeX + nTimeWidth;
     39     int nExplainWidth = nUnitW * 5;
     40 
     41     int nRgInfoX = nExplainX + nExplainWidth;
     42     int nRgInfoW = nUnitW * 10;
     43 
     44     nLineHeight = 80;
     45     pPainter->drawText(QRect(0, y, nNoWidth, nLineHeight), Qt::AlignCenter, "序号");
     46     pPainter->drawText(QRect(nTimeX, y, nTimeWidth, nLineHeight), Qt::AlignCenter, "操作时间");
     47     pPainter->drawText(QRect(nExplainX, y, nExplainWidth, nLineHeight), Qt::AlignCenter, "操作说明");
     48     pPainter->drawText(QRect(nRgInfoX, y, nRgInfoW, nLineHeight), Qt::AlignCenter, "试管和试剂信息");
     49 
     50     //绘制一条分隔线
     51     y += 100;
     52     pPainter->setPen(QPen(QBrush(QColor(144, 166,188)), 8));
     53     pPainter->drawLine(QLineF(0, y, nPdfWidth, y));
     54 
     55     //绘制染色操作记录,假设此刻有25条记录需要绘制
     56     for(int i = 0; i < 25; ++i)
     57     {
     58         //判断是否应该另起一页(320:根据计算,绘制每条记录大概需要320点)
     59         if(y + 320 >= nPdfHeight)
     60         {
     61             device->newPage();
     62             y = 10;
     63         }
     64 
     65         y += 50;
     66         pPainter->setFont(QFont("宋体", 12, 20));
     67         pPainter->setPen(QPen(QBrush(QColor(0, 0, 0)), 1));
     68 
     69         nLineSpace = 80;
     70         nLineHeight = 50;
     71 
     72         pPainter->drawText(QRect(0, y, nNoWidth, nLineHeight), Qt::AlignVCenter | Qt::AlignLeft, QString("%1").arg(i + 1));
     73         pPainter->drawText(QRect(nTimeX, y, nTimeWidth, nLineHeight), Qt::AlignVCenter | Qt::AlignLeft, "1999-09-26 18:27");
     74         pPainter->drawText(QRect(nExplainX, y, nExplainWidth, nLineHeight), Qt::AlignVCenter | Qt::AlignLeft, "需加试剂DC9混合");
     75 
     76         //试管和试剂信息的每行需列出2项信息 宽度比例为7:3
     77         nUnitW = nRgInfoW / (7 + 3);
     78 
     79         int nLeftX = nRgInfoX;
     80         int nLeftW = nUnitW * 6;
     81 
     82         int nRightX = nLeftX + nLeftW;
     83         int nRightW = nUnitW * 4;
     84 
     85         pPainter->drawText(QRect(nLeftX, y, nLeftW, nLineHeight), Qt::AlignVCenter | Qt::AlignLeft,
     86                            QString("试管ID: %1").arg("15bb00226fa"));
     87         pPainter->drawText(QRect(nRightX, y, nRightW, nLineHeight), Qt::AlignVCenter | Qt::AlignLeft,
     88                            QString("试管位置号: %1").arg(18));
     89 
     90         y += nLineSpace;
     91         pPainter->drawText(QRect(nLeftX, y, nLeftW, nLineHeight), Qt::AlignVCenter | Qt::AlignLeft,
     92                            QString("添加试剂1: %1").arg("AC856"));
     93         pPainter->drawText(QRect(nRightX, y, nRightW, nLineHeight), Qt::AlignVCenter | Qt::AlignLeft,
     94                            QString("添加量:%1 μL").arg(150));
     95 
     96         y += nLineSpace;
     97         pPainter->drawText(QRect(nLeftX, y, nLeftW, nLineHeight), Qt::AlignVCenter | Qt::AlignLeft,
     98                            QString("添加试剂2: %1").arg("SFH51"));
     99         pPainter->drawText(QRect(nRightX, y, nRightW, nLineHeight), Qt::AlignVCenter | Qt::AlignLeft,
    100                            QString("添加量:%1 μL").arg(80));
    101 
    102         y += 70;
    103         pPainter->setPen(QPen(QBrush(QColor(166, 188, 222)), 3));
    104         pPainter->drawLine(QLineF(0, y, nPdfWidth, y));
    105     }
    106 
    107     delete pPainter;
    108 }

     参考:https://www.cnblogs.com/lifexy/p/12620647.html

  • 相关阅读:
    bzoj 2742(树状数组)
    [网络流24题(3/24)] 最长k可重区间集问题(洛谷P3358)
    bzoj 1087(状压dp)
    算法模板整理V1.0
    ACM资料汇总
    算法笔记
    NC20861 兔子的逆序对(数学基础)
    zzuli新生周赛第四周题解
    Gym 102028E Resistors in Parallel(大数)
    HDU 3974 Assign the task(dfs序建线段树)
  • 原文地址:https://www.cnblogs.com/grebamboo/p/13948833.html
Copyright © 2020-2023  润新知