c++ - 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)
解决方案
看起来实现是用 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 手册中也提到了这一点。
推荐阅读
- php - #1044 - 用户 'A WEIRD USER'@'localhost' 拒绝访问数据库 'MY DATABASE'
- android - Zipping Room Flowable 可防止更新
- ignite - 在另一个缓存的调用处理器中调用缓存删除时,Ignite 服务挂起,“条带池中可能出现饥饿”?
- macos - 如何使用 sed 替换多行
- boto3 - boto3 的 AWS SNS 和 Waiter 函数
- c# - C# 从 HTTPS URL 获取服务器证书引发 System.PlatformNotSupportedException 异常
- javascript - 如何使用正则表达式在javascript中通过id获取元素?
- listview - 将自定义命令的工具提示添加到列表项的上下文菜单
- html - HTML CSS Nav 获取我的表边距顶部
- c - 不同线程中的全局变体是否意味着不同的变体?