您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Qt數(shù)據(jù)庫應(yīng)用之怎么實(shí)現(xiàn)圖片轉(zhuǎn)pdf”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
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 //調(diào)整圖片大小比如等比例縮放拉伸填充等 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; //圖片寬度小于繪制區(qū)域?qū)挾葲]有縮放處理過的圖片 才需要按照比例自動居中繪制 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"); } //資源文件則當(dāng)前目錄下 if (imageFile.startsWith(":/")) { fileName = qApp->applicationDirPath() + "/" + QFileInfo(imageFile).baseName() + ".pdf"; } toPdf(QPixmap(imageFile), fileName, scale); return fileName; }
“Qt數(shù)據(jù)庫應(yīng)用之怎么實(shí)現(xiàn)圖片轉(zhuǎn)pdf”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。