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();
时间: 2024-10-01 01:58:18