Open PDF at specific page with Qt WebEngineView

隐身守侯 提交于 2021-02-08 05:57:35

问题


I want to create a simple PDF Viewer inside my Qt Application. Everything works fine, accept opening the PDF at a specific page over the URL. For example:

url = "file:///D://Repo//PdfViewer//PdfViewer//test.pdf";

Works, but

url = "file:///D://Repo//PdfViewer//PdfViewer//test.pdf#page=9";

dosent. I read somewhere, that chrome dosent offically support #page=x anymore, but I cant find futher information how to solve this. I face the same problem with this url in the Nano-Browser Example from Qt.

PdfViewer::PdfViewer(const QString &pdf_path, QWidget *parent)
: QWidget(parent), m_View(new QWebEngineView(this)), m_ExitButton(new QPushButton())
{
  QUrl url = QUrl::fromLocalFile(pdf_path);

  m_View->settings()->setAttribute(QWebEngineSettings::PluginsEnabled, true);
  m_View->settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
  m_View->settings()->setAttribute(QWebEngineSettings::PdfViewerEnabled, true);
  m_View->load(url);

  m_ExitButton->setIcon(QIcon("Ok.png"));

  QVBoxLayout *layout = new QVBoxLayout();
  layout->addWidget(m_View);
  layout->addWidget(m_ExitButton);
  this->setLayout(layout);

  connect(m_ExitButton, &QPushButton::clicked, this, &PdfViewer::close);
}

I am using Qt 5.13.


回答1:


Qt WebEngine uses the chromium pdf viewer so analyzing the source code I found the function that implements move of page: window.viewer.viewport_.goToPage(page) that can be executed using the runJavaScript() method of QWebEnginePage.

Considering the above, the solution is:

#include <QtWebEngineWidgets>

class PdfViewer: public QWidget{
    Q_OBJECT
public:
    PdfViewer(const QString &pdf_path, QWidget *parent=nullptr)
        : QWidget(parent), m_View(new QWebEngineView(this)), m_ExitButton(new QPushButton())
    {
        QUrl url = QUrl::fromLocalFile(pdf_path);

        m_View->settings()->setAttribute(QWebEngineSettings::PluginsEnabled, true);
        m_View->settings()->setAttribute(QWebEngineSettings::FullScreenSupportEnabled, true);
        m_View->settings()->setAttribute(QWebEngineSettings::PdfViewerEnabled, true);
        m_View->load(url);

        m_ExitButton->setIcon(QIcon("Ok.png"));

        QVBoxLayout *layout = new QVBoxLayout();
        layout->addWidget(m_View);
        layout->addWidget(m_ExitButton);
        this->setLayout(layout);

        connect(m_ExitButton, &QPushButton::clicked, this, &PdfViewer::close);
        connect(m_View, &QWebEngineView::loadFinished, this, &PdfViewer::on_finished);
    }
private Q_SLOTS:
    void on_finished(bool ok){
        if(ok){
            QTimer::singleShot(100, this, [this](){ goToPage(9); });
        }
    }
private:
    void goToPage(int page){
        m_View->page()->runJavaScript(QString("window.viewer.viewport_.goToPage(%1)").arg(page));
    }
    QWebEngineView *m_View;
    QPushButton *m_ExitButton;
};
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QString fileName = QFileDialog::getOpenFileName(nullptr,
                                                    QObject::tr("Open Image"),
                                                    QDir::homePath(),
                                                    QObject::tr("PDF Files (*.pdf)"));
    if(fileName.isEmpty())
        return 0;
    PdfViewer w(fileName);
    w.resize(640, 480);
    w.show();
    return a.exec();
}

#include "main.moc"


来源:https://stackoverflow.com/questions/60560583/open-pdf-at-specific-page-with-qt-webengineview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!