首页 > 解决方案 > QPdfDocument::pageSize 的单位是什么?

问题描述

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

返回的尺寸单位是什么QPdfDocument::pageSize()(不要费心检查那些文档,它不存在)?

它似乎是一些合理的(尽管看似低分辨率)类似像素的单位,但我不确定它是如何推断文档的 DPI 的。此外,我只测试了一组有限的 PDF,它们都是从同一来源生成的,所以我不确定我的观察结果有多正常,特别是考虑到它是 aQSizeF而不是 a 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++qtqt5qtwebengineqt5.15

解决方案


看起来实现是用 PDFium 完成的。Qt 调用 FPDF_GetPageSizeByIndex 并且文档状态高度/宽度以点为单位:

// 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 手册中也提到了这一点。


推荐阅读