本篇内容介绍了“Qt数据库应用之怎么实现图片转pdf”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
void DataOther::toPdf(const QPixmap &pixmap, const QString &fileName, int scale) { QPrinter printer(QPrinter::HighResolution); if (scale > 1) { printer.setResolution(96); } printer.setFullPage(false); printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName(fileName); #if (QT_VERSION >= QT_VERSION_CHECK(5,3,0)) printer.setPageSize(QPageSize(QPageSize::A4)); printer.setPageOrientation(QPageLayout::Portrait); #else printer.setPaperSize(QPrinter::A4); printer.setOrientation(QPrinter::Portrait); #endif //调整图片大小比如等比例缩放拉伸填充等 QRectF rect = printer.pageRect(QPrinter::DevicePixel); QPixmap pix = pixmap; //保存原图看下效果 #if 0 QString file = fileName; file.replace("pdf", "png"); pix.save(file, "png"); #endif if (scale == 0) { //pix = pix.scaled(rect.width(), rect.height(), Qt::KeepAspectRatio, Qt::FastTransformation); pix = pix.scaled(rect.width(), rect.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation); } else if (scale == 1) { pix = pix.scaled(rect.width(), rect.height()); } QPainter painter; painter.begin(&printer); int x = 0; int y = 0; //图片宽度小于绘制区域宽度没有缩放处理过的图片 才需要按照比例自动居中绘制 if (pixmap.width() < rect.width() && scale > 1) { x = rect.center().x() - pix.width() / 2; //y = rect.center().y() - pix.height() / 2; } painter.drawPixmap(QPoint(x, y), pix); painter.end(); } void DataOther::widgetToPdf(QWidget *widget, const QString &fileName, int scale) { #if (QT_VERSION >= QT_VERSION_CHECK(5,0,0)) QPixmap pixmap = QApplication::primaryScreen()->grabWindow(widget->winId()); #else QPixmap pixmap = QPixmap::grabWindow(widget->winId()); #endif toPdf(pixmap, fileName, scale); } QString DataOther::imageToPdf(const QString &imageFile, const QString &pdfFile, int scale) { //为空则同名文件 QString fileName = pdfFile; if (fileName.isEmpty()) { fileName = imageFile; fileName.replace("." + QFileInfo(imageFile).suffix(), ".pdf"); } //资源文件则当前目录下 if (imageFile.startsWith(":/")) { fileName = qApp->applicationDirPath() + "/" + QFileInfo(imageFile).baseName() + ".pdf"; } toPdf(QPixmap(imageFile), fileName, scale); return fileName; }
“Qt数据库应用之怎么实现图片转pdf”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。