【问题标题】:What are the units of QPdfDocument::pageSize?QPdfDocument::pageSize 的单位是什么?
【发布时间】:2021-02-08 18:45:38
【问题描述】:

QPdfDocument (5.15.2),在 QtPDF 模块(QtWebEngine 的一部分)中,似乎还没有完全记录(大概是因为它相当新,首次出现在 5.14 中) .

QPdfDocument::pageSize() 返回的尺寸单位是多少(不用检查那些文档,它不存在)?

它似乎是一些合理的(尽管看似低分辨率)类似像素的单位,但我不确定它是如何推断文档的 DPI 的。此外,我只测试了一组有限的 PDF,它们都是从同一来源生成的,所以我不确定我的观察结果有多正常,特别是考虑到它是 QSizeF 而不是 QSize(这增加了例如,其他尚未遇到的上下文中的其他非像素单位)。

最终我想做的是以物理单位(例如英寸)获取加载文档页面的页面大小,然后在给定用户指定的 DPI 的情况下确定以像素为单位的渲染输出大小。


我观察到的值的一个例子:

#include <QCoreApplication>
#include <QDebug>
#include <QtNetwork>
#include <QtPdf>

int main (int argc, char *argv[]) {

    QCoreApplication app(argc, argv);

    QUrl url("http://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf");
    QNetworkAccessManager internet;
    QNetworkReply *reply = internet.get(QNetworkRequest(url));

    QObject::connect(reply, &QNetworkReply::finished, [reply] () {
        QPdfDocument pdf;
        pdf.load(reply);
        qDebug() << reply->url() << ":";
        for (int k = 0; k < pdf.pageCount(); ++ k)
            qDebug() << k << pdf.pageSize(k);
        QCoreApplication::exit();
    });

    return app.exec();

}

输出:

QUrl("http://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf") :
0 QSizeF(595, 842)

【问题讨论】:

    标签: c++ qt qt5 qtwebengine qt5.15


    【解决方案1】:

    看起来implementation 是用 PDFium 完成的。 Qt 调用 FPDF_GetPageSizeByIndex 和 docs 状态高度/宽度以磅为单位:

    // Function: FPDF_GetPageSizeByIndex
    //          Get the size of the page at the given index.
    // Parameters:
    //          document    -   Handle to document. Returned by FPDF_LoadDocument.
    //          page_index  -   Page index, zero for the first page.
    //          width       -   Pointer to a double to receive the page width
    //                          (in points).
    //          height      -   Pointer to a double to receive the page height
    //                          (in points).
    // Return value:
    //          Non-zero for success. 0 for error (document or page not found).
    // Note:
    //          Prefer FPDF_GetPageSizeByIndexF() above. This will be deprecated in
    //          the future.
    FPDF_EXPORT int FPDF_CALLCONV FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
                                                          int page_index,
                                                          double* width,
                                                          double* height);
    

    PDFium SDK manual 中也注明了这一点。

    【讨论】:

    • 感谢您挖掘它。
    猜你喜欢
    • 2021-10-17
    • 2013-07-24
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多