第 x 页,共 y 页,使用 QPrinter
page x of y using QPrinter
im 使用 qt 从 HTML 代码生成 PDF 文件:
QTextDocument *document = new QTextDocument();
document->setHtml(htmlContent);
QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("filename.pdf");
document->print(printer);
是否可以拥有页面信息"第 X 页,共 Y 页",而不仅仅是页码?如果是,如何?
我提出的解决方案基于此代码。我添加了对高分辨率的必要支持
static const int textMargins = 12; // in millimeters
static const int borderMargins = 10; // in millimeters
static double mmToPixels(QPrinter& printer, int mm)
{
return mm * 0.039370147 * printer.resolution();
}
static void paintPage(int pageNumber, int pageCount,
QPainter* painter, QTextDocument* doc,
const QRectF& textRect, qreal footerHeight)
{
painter->save();
// textPageRect is the rectangle in the coordinate system of the QTextDocument, in pixels,
// and starting at (0,0) for the first page. Second page is at y=doc->pageSize().height().
const QRectF textPageRect(0, pageNumber * doc->pageSize().height(), doc->pageSize().width(), doc->pageSize().height());
// Clip the drawing so that the text of the other pages doesn't appear in the margins
painter->setClipRect(textRect);
// Translate so that 0,0 is now the page corner
painter->translate(0, -textPageRect.top());
// Translate so that 0,0 is the text rect corner
painter->translate(textRect.left(), textRect.top());
doc->drawContents(painter);
painter->restore();
QRectF footerRect = textRect;
footerRect.setTop(textRect.bottom());
footerRect.setHeight(footerHeight);
painter->drawText(footerRect, Qt::AlignVCenter | Qt::AlignRight, QObject::tr("Page %1 of %2").arg(pageNumber+1).arg(pageCount));
}
static void printDocument(QPrinter& printer, QTextDocument* doc)
{
QPainter painter( &printer );
doc->documentLayout()->setPaintDevice(&printer);
doc->setPageSize(printer.pageRect().size());
QSizeF pageSize = printer.pageRect().size(); // page size in pixels
// Calculate the rectangle where to lay out the text
const double tm = mmToPixels(printer, textMargins);
const qreal footerHeight = painter.fontMetrics().height();
const QRectF textRect(tm, tm, pageSize.width() - 2 * tm, pageSize.height() - 2 * tm - footerHeight);
doc->setPageSize(textRect.size());
const int pageCount = doc->pageCount();
bool firstPage = true;
for (int pageIndex = 0; pageIndex < pageCount; ++pageIndex) {
if (!firstPage)
printer.newPage();
paintPage(pageIndex, pageCount, &painter, doc, textRect, footerHeight );
firstPage = false;
}
}
例:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QTextDocument *document = new QTextDocument();
QTextCursor cursor(document);
QTextBlockFormat blockFormat;
for(int i=0; i < 10; i++){
cursor.insertBlock(blockFormat);
cursor.insertHtml(QString("<h1>This is the %1 page</h1>").arg(i+1));
blockFormat.setPageBreakPolicy(QTextFormat::PageBreak_AlwaysBefore);
}
QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("filename.pdf");;
printDocument(printer, document);
return app.exec();
}
以上
是一个很好的解决方案,但如果在打印后使用它,则会破坏您的 QTextDocument 对象。我有一个QTextDocument,它是对话框的成员,当提供打印机(paintDevice(的调用函数失去范围时,它会使文档崩溃。
我建议在函数开始时获取当前 paintDevice 的指针,然后在结束时恢复它。
static void printDocument(QPrinter& printer, QTextDocument* doc)
{
QPaintDevice * oldDevice=doc->documentLayout()->paintDevice();
doc->documentLayout()->setPaintDevice(&printer);
doc->documentLayout()->setPaintDevice(oldDevice);
}
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- C++:TypeDef使用元组
- 使用std::multimap迭代器创建std::list
- 从不同线程使用int64的不同字节安全吗
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么在全局范围内使用"extern int a"似乎不行?
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用Google Mock来模拟gettimeofday()
- 如何使用默认参数等选择模板专业化
- 为什么使用 "this" 指针调用派生成员函数?
- 使用新行和不使用新行读取文件
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 如何确定我已使用非编码文件到达 EOF?
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 使用CMake创建QML插件
- 第 x 页,共 y 页,使用 QPrinter
- 使用QPrinter::高分辨率不打印
- 使用 QThread 和 QPrinter 时出现内存泄漏
- 使用qt+qprinter将工作url添加到pdf中