欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

QPdfWriter QPrinter QPainter 自定义生成PDF文件 基本控件(十六)

程序员文章站 2022-05-28 11:38:35
...

一、效果图
QPdfWriter QPrinter QPainter 自定义生成PDF文件 基本控件(十六)
QPdfWriter QPrinter QPainter 自定义生成PDF文件 基本控件(十六)
二、代码

    QFile file(QCoreApplication::applicationDirPath() + "/demo.pdf");
    file.open(QIODevice::WriteOnly);
    QPdfWriter* writer = new QPdfWriter(&file);
    QPainter* p = new QPainter(writer);
    writer->setPageSize(QPagedPaintDevice::A4);
    p->drawText(QRect(100, 100, 4200, 2200), QString("我国经济长期向好的基本面没有改变(人民论坛)"));
    writer->newPage();
    delete p;
    delete writer;
    file.close();

    QPrinter printerPixmap(QPrinter::HighResolution);
    printerPixmap.setPageSize(QPrinter::A4);
    printerPixmap.setOutputFormat(QPrinter::PdfFormat);
    printerPixmap.setOutputFileName(QCoreApplication::applicationDirPath() + "/demo1.pdf");

    QPixmap pixmap;
    pixmap.load(QCoreApplication::applicationDirPath() + "/1.jpg");

    QPainter painterPixmap;
    painterPixmap.begin(&printerPixmap);
    QRect rect = painterPixmap.viewport();
    int multiple = rect.width() / pixmap.width();
    painterPixmap.scale(multiple, multiple);
    painterPixmap.drawPixmap(0, 0, pixmap);
    painterPixmap.end();
相关标签: # Qt-基本控件