mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-06-05 21:59:17 +02:00
Rendering pdf to images (first part)
This commit is contained in:
@ -25,6 +25,7 @@ int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication::setAttribute(Qt::AA_CompressHighFrequencyEvents, true);
|
||||
QApplication::setAttribute(Qt::AA_DisableHighDpiScaling, true);
|
||||
QApplication::setAttribute(Qt::AA_DontCheckOpenGLContextThreadAffinity, true);
|
||||
QApplication application(argc, argv);
|
||||
|
||||
QCoreApplication::setOrganizationName("MelkaJ");
|
||||
|
@ -18,17 +18,29 @@
|
||||
#include "pdfrendertoimagesdialog.h"
|
||||
#include "ui_pdfrendertoimagesdialog.h"
|
||||
|
||||
#include "pdfcms.h"
|
||||
#include "pdfutils.h"
|
||||
#include "pdfwidgetutils.h"
|
||||
#include "pdfoptionalcontent.h"
|
||||
#include "pdfdrawspacecontroller.h"
|
||||
|
||||
#include <QFileDialog>
|
||||
#include <QMessageBox>
|
||||
#include <QPushButton>
|
||||
|
||||
namespace pdfviewer
|
||||
{
|
||||
|
||||
PDFRenderToImagesDialog::PDFRenderToImagesDialog(QWidget* parent) :
|
||||
PDFRenderToImagesDialog::PDFRenderToImagesDialog(const pdf::PDFDocument* document,
|
||||
pdf::PDFDrawWidgetProxy* proxy,
|
||||
pdf::PDFProgress* progress,
|
||||
QWidget* parent) :
|
||||
QDialog(parent),
|
||||
ui(new Ui::PDFRenderToImagesDialog),
|
||||
m_document(document),
|
||||
m_proxy(proxy),
|
||||
m_progress(progress),
|
||||
m_imageExportSettings(document),
|
||||
m_isLoadingData(false)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
@ -56,6 +68,9 @@ PDFRenderToImagesDialog::PDFRenderToImagesDialog(QWidget* parent) :
|
||||
connect(ui->optimizedWriteCheckBox, &QCheckBox::clicked, this, &PDFRenderToImagesDialog::onOptimizedWriteChanged);
|
||||
connect(ui->progressiveScanWriteCheckBox, &QCheckBox::clicked, this, &PDFRenderToImagesDialog::onProgressiveScanWriteChanged);
|
||||
|
||||
ui->resolutionDPIEdit->setRange(pdf::PDFPageImageExportSettings::getMinDPIResolution(), pdf::PDFPageImageExportSettings::getMaxDPIResolution());
|
||||
ui->resolutionPixelsEdit->setRange(pdf::PDFPageImageExportSettings::getMinPixelResolution(), pdf::PDFPageImageExportSettings::getMaxPixelResolution());
|
||||
|
||||
loadImageWriterSettings();
|
||||
loadImageExportSettings();
|
||||
|
||||
@ -219,6 +234,11 @@ void PDFRenderToImagesDialog::onProgressiveScanWriteChanged(bool value)
|
||||
m_imageWriterSettings.setProgressiveScanWrite(value);
|
||||
}
|
||||
|
||||
void PDFRenderToImagesDialog::onRenderError(pdf::PDFRenderError error)
|
||||
{
|
||||
ui->progressMessagesEdit->setPlainText(QString("%1\n%2").arg(ui->progressMessagesEdit->toPlainText()).arg(error.message));
|
||||
}
|
||||
|
||||
void PDFRenderToImagesDialog::on_selectDirectoryButton_clicked()
|
||||
{
|
||||
QString directory = QFileDialog::getExistingDirectory(this, tr("Select output directory"), ui->directoryEdit->text());
|
||||
@ -228,6 +248,67 @@ void PDFRenderToImagesDialog::on_selectDirectoryButton_clicked()
|
||||
}
|
||||
}
|
||||
|
||||
void PDFRenderToImagesDialog::on_buttonBox_clicked(QAbstractButton* button)
|
||||
{
|
||||
if (button == ui->buttonBox->button(QDialogButtonBox::Apply))
|
||||
{
|
||||
QString message;
|
||||
if (m_imageExportSettings.validate(&message))
|
||||
{
|
||||
// We are ready to render the document
|
||||
std::vector<pdf::PDFInteger> pageIndices = m_imageExportSettings.getPages();
|
||||
|
||||
pdf::PDFOptionalContentActivity optionalContentActivity(m_document, pdf::OCUsage::Export, nullptr);
|
||||
pdf::PDFCMSPointer cms = m_proxy->getCMSManager()->getCurrentCMS();
|
||||
pdf::PDFRasterizerPool rasterizerPool(m_document, m_proxy->getFontCache(), cms.data(),
|
||||
&optionalContentActivity, m_proxy->getFeatures(), m_proxy->getMeshQualitySettings(),
|
||||
pdf::PDFRasterizerPool::getDefaultRasterizerCount(), m_proxy->isUsingOpenGL(), m_proxy->getSurfaceFormat(), this);
|
||||
connect(&rasterizerPool, &pdf::PDFRasterizerPool::renderError, this, &PDFRenderToImagesDialog::onRenderError);
|
||||
|
||||
auto imageSizeGetter = [this](const pdf::PDFPage* page) -> QSize
|
||||
{
|
||||
Q_ASSERT(page);
|
||||
|
||||
switch (m_imageExportSettings.getResolutionMode())
|
||||
{
|
||||
case pdf::PDFPageImageExportSettings::ResolutionMode::DPI:
|
||||
{
|
||||
QSizeF size = page->getRotatedMediaBox().size() * m_imageExportSettings.getDpiResolution();
|
||||
return size.toSize();
|
||||
}
|
||||
|
||||
case pdf::PDFPageImageExportSettings::ResolutionMode::Pixels:
|
||||
{
|
||||
int pixelResolution = m_imageExportSettings.getPixelResolution();
|
||||
QSizeF size = page->getRotatedMediaBox().size().scaled(pixelResolution, pixelResolution, Qt::KeepAspectRatio);
|
||||
return size.toSize();
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
Q_ASSERT(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return QSize();
|
||||
};
|
||||
|
||||
auto processImage = [](const pdf::PDFInteger pageIndex, QImage&& image)
|
||||
{
|
||||
Q_UNUSED(pageIndex);
|
||||
Q_UNUSED(image);
|
||||
};
|
||||
|
||||
setEnabled(false);
|
||||
rasterizerPool.render(pageIndices, imageSizeGetter, processImage, m_progress);
|
||||
setEnabled(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
QMessageBox::critical(this, tr("Error"), message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace pdfviewer
|
||||
|
||||
|
||||
|
@ -22,11 +22,19 @@
|
||||
|
||||
#include <QDialog>
|
||||
|
||||
class QAbstractButton;
|
||||
|
||||
namespace Ui
|
||||
{
|
||||
class PDFRenderToImagesDialog;
|
||||
}
|
||||
|
||||
namespace pdf
|
||||
{
|
||||
class PDFProgress;
|
||||
class PDFDrawWidgetProxy;
|
||||
}
|
||||
|
||||
namespace pdfviewer
|
||||
{
|
||||
|
||||
@ -35,11 +43,15 @@ class PDFRenderToImagesDialog : public QDialog
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit PDFRenderToImagesDialog(QWidget* parent);
|
||||
explicit PDFRenderToImagesDialog(const pdf::PDFDocument* document,
|
||||
pdf::PDFDrawWidgetProxy* proxy,
|
||||
pdf::PDFProgress* progress,
|
||||
QWidget* parent);
|
||||
virtual ~PDFRenderToImagesDialog() override;
|
||||
|
||||
private slots:
|
||||
void on_selectDirectoryButton_clicked();
|
||||
void on_buttonBox_clicked(QAbstractButton* button);
|
||||
|
||||
private:
|
||||
/// Loads image writer settings to the ui
|
||||
@ -62,8 +74,12 @@ private:
|
||||
void onGammaChanged(double value);
|
||||
void onOptimizedWriteChanged(bool value);
|
||||
void onProgressiveScanWriteChanged(bool value);
|
||||
void onRenderError(pdf::PDFRenderError error);
|
||||
|
||||
Ui::PDFRenderToImagesDialog* ui;
|
||||
const pdf::PDFDocument* m_document;
|
||||
pdf::PDFDrawWidgetProxy* m_proxy;
|
||||
pdf::PDFProgress* m_progress;
|
||||
pdf::PDFImageWriterSettings m_imageWriterSettings;
|
||||
pdf::PDFPageImageExportSettings m_imageExportSettings;
|
||||
bool m_isLoadingData;
|
||||
|
@ -7,7 +7,7 @@
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>690</width>
|
||||
<height>593</height>
|
||||
<height>602</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
@ -234,7 +234,14 @@
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPlainTextEdit" name="plainTextEdit"/>
|
||||
<widget class="QPlainTextEdit" name="progressMessagesEdit">
|
||||
<property name="undoRedoEnabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QDialogButtonBox" name="buttonBox">
|
||||
|
@ -1279,11 +1279,8 @@ void PDFViewerMainWindow::on_actionPrint_triggered()
|
||||
|
||||
void PDFViewerMainWindow::on_actionRender_to_Images_triggered()
|
||||
{
|
||||
PDFRenderToImagesDialog dialog(this);
|
||||
if (dialog.exec() == QDialog::Accepted)
|
||||
{
|
||||
|
||||
}
|
||||
PDFRenderToImagesDialog dialog(m_pdfDocument.data(), m_pdfWidget->getDrawWidgetProxy(), m_progress, this);
|
||||
dialog.exec();
|
||||
}
|
||||
|
||||
} // namespace pdfviewer
|
||||
|
Reference in New Issue
Block a user