- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 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 页”而不仅仅是页码?如果是,如何?
最佳答案
我提出的方案是基于this code .我已经添加了对 HighResolution 的必要支持
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();
}
关于c++ - 第 x 页,共 y 页,使用 QPrinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44537788/
我使用以下代码设置我的 QPrinter 对象的纸张大小: printer_.setPaperSize(QPrinter::A4); 在我的电脑上,它按预期工作,但在另一台电脑上,我的 PDF 报告布
我是 Qt 的新手。下载了 SourceForge 的 Qt 应用程序的源代码,并尝试构建和运行它。通过添加 QT += 解决了一些类似的问题后.pro 文件的声明,我被困在这个: 在尝试在 Qt C
当使用 QTextDocument 和 QPrinter 打印为 PDF 时,是否有任何方法可以检测错误(例如,无法写入 PDF 文件)?我正在使用以下代码: QTextDocument docume
我正在尝试在 iOS 中使用 QT。 #include QPrinter print; 上面的代码给出了“变量的类型不完整‘QPrinter’”错误。尽管 QtGui/qprinter.h 对 QP
我需要在我的程序中打印标签。标签有一张图片和一个应该环绕在图片周围的描述文本。 QPrinter 只提供在矩形中打印文本,但没有我可以定义文本区域或类似内容的地方。 有什么办法还是必须自己写? 最佳答
当我尝试在我的代码中的任何位置声明 QPrinter 时,出现以下错误: Error 1 error LNK2019: unresolved external symbol "__declsp
我正在使用 Qt C++ 进行打印。但是我发现了一个问题,margin 没有按预期工作。这里是我使用的 QPrinter 配置。 QPrinter *pr = new QPrinter(QPrinte
在 Qt Desktop 应用程序中,如何在应用程序运行之间保持 QPrinter 选择? 考虑以下代码: printDialog = new QPrintDialog(); if (printDia
我正在尝试创建一个 Windows 应用程序(但最终也会将它移植到 linux,因此如果可能的话,交叉兼容性很重要)它将从网络摄像头拍摄照片并且可以在不使用 printDialog 框的情况下进行打印
我需要为用户动态创建的一些对象生成一个文档来打印,我想打印这些文档。我编写了以下代码(generateDocument() 引用文档以添加 html 代码): QPrinter printer; QP
我在我的项目中实现了打印报告的功能。报告以 HTML 内容的形式呈现。我用于打印 QWebEnginePage。我需要报告的高分辨率打印输出: QPrinter printer = new QPrin
我想使用 PyQt 通过打印机预览然后打印报告。我尝试了以下代码: printer = QtGui.QPrinter() doc = QtGui.QTextDocument("testing") d
我正在尝试让一些图像打印程序在 Qt 中运行。尝试打印到具有 ppd 的自定义打印机。根据设备信息进行计算,以创建要发送到打印机的图像。 查看打印机属性时,我看到分辨率是300 dpi x 300 d
在我的小应用程序中,我想在每个页面中预览一些 HTML 内容,所以我使用了以下代码 /* * Handle events when clicking button to preview content
在我的小应用程序中,我想在每个页面中预览一些 HTML 内容,所以我使用了以下代码 /* * Handle events when clicking button to preview content
所以我一直在尝试让我的程序与 QPrinter 一起使用 cmake+mingw+qt5.2 进行编译,但我遇到了问题:以下测试程序无法编译,因为它找不到应该是一部分的 QPrinter QtCore
我想创建一个具有正确样式的 HTML 的 PDF 文档。这就是我正在尝试的: QTextDocument *doc = new QTextDocument(); const char* s1 = "b
有没有一种简单的方法可以使用 c++、qt 和 qprinter 类将工作/可点击的 url 添加到 pdf 中?稍微澄清一下:我想在生成新文档时添加 url。我不是要编辑现有的 pdf。 最佳答案
当从 QPrintPreviewDialog 打印文档时,我不想在 Linux 下从调用的 QPrinter 设置对话框中的打印机列表中删除“写入 PDF”。可以吗? 我已经尝试用我自己的按钮和操作替
我对默认页边距有疑问。 我想将页边距全部设置为 0,我做到了。 但它始终具有大约 1 英寸的边距,我认为硬件或某些默认值会保留左侧和顶部边距。 所以我调用了函数 QPrinter.margins().
我是一名优秀的程序员,十分优秀!