英文:
Unable to add new pages in pdf file using QPdfWriter
问题
我尝试生成一个包含多页的PDF文件(使用Qt 6.2.0,MinGW 64位,Windows 10)。
使用以下代码,所有对 QPdfWriter::newPage
的调用都失败:
QFile outputFile(filename);
if (!outputFile.open(QIODevice::WriteOnly))
return;
QPdfWriter writer(&outputFile);
writer.setPageSize(QPageSize(QPageSize::A5));
writer.setPageOrientation(QPageLayout::Landscape);
QPainter painter;
// 第一页
if (!writer.newPage())
qWarning() << "newPage failed";
if (!painter.begin(&writer))
qWarning() << "QPainter::begin failed";
QTextDocument page1;
page1.setHtml("<h1 align=center>Page 1</h1><p align=justify>Test page 1</p>");
page1.drawContents(&painter);
if (!painter.end())
qWarning() << "QPainter::end failed";
// 第二页
if (!writer.newPage())
qWarning() << "newPage failed";
if (!painter.begin(&writer))
qWarning() << "QPainter::begin failed";
QTextDocument page2;
page2.setHtml("<h1 align=center>Page 2</h1><p align=justify>Test page 2</p>");
page2.drawContents(&painter);
if (!painter.end())
qWarning() << "QPainter::end failed";
这段代码只生成一个单页的PDF文件(仅包含第二页的内容)。所有对 QPdfWriter::newPage
的调用都失败,其他调用似乎正常工作。
英文:
I am trying to generate a PDF file with multiple pages (using Qt 6.2.0 with MinGW 64 bit on Windows 10).
Using the following code, all calls to QPdfWriter::newPage
fail:
QFile outputFile(filename);
if (!outputFile.open(QIODevice::WriteOnly))
return;
QPdfWriter writer(&outputFile);
writer.setPageSize(QPageSize(QPageSize::A5));
writer.setPageOrientation(QPageLayout::Landscape);
QPainter painter;
// First page
if (!writer.newPage())
qWarning() << "newPage failed";
if (!painter.begin(&writer))
qWarning() << "QPainter::begin failed";
QTextDocument page1;
page1.setHtml("<h1 align=center>Page 1</h1><p align=justify>Test page 1</p>");
page1.drawContents(&painter);
if (!painter.end())
qWarning() << "QPainter::end failed";
// second page
if (!writer.newPage())
qWarning() << "newPage failed";
if (!painter.begin(&writer))
qWarning() << "QPainter::begin failed";
QTextDocument page2;
page2.setHtml("<h1 align=center>Page 2</h1><p align=justify>Test page 2</p>");
page2.drawContents(&painter);
if (!painter.end())
qWarning() << "QPainter::end failed";
This code just generates a PDF file with a single page (only the content of my second page). All calls to QPdfWriter::newPage
fail, all other calls seem to work fine.
答案1
得分: 2
你结束绘制并重新开始绘制第二页时,会覆盖掉第一页的内容。你应该在PDF文件的开头开始,然后在末尾结束。以下是示例代码:
QPdfWriter writer("test.pdf");
writer.setPageSize(QPageSize(QPageSize::A7));
writer.setPageOrientation(QPageLayout::Landscape);
QPainter painter;
// 第一页
if (!painter.begin(&writer))
qWarning() << "QPainter::begin failed";
QTextDocument page1;
page1.setHtml("<h1 style=\"color: red\" align=center>Page 1</h1><p style=\"color: red\" align=justify>Test page 1</p>");
page1.drawContents(&painter);
// 第二页
if (!writer.newPage())
qWarning() << "newPage failed";
QTextDocument page2;
page2.setHtml("<h1 style=\"color: red\" align=center>Page 2</h1><p style=\"color: red\" align=justify>Test page 2</p>");
page2.drawContents(&painter);
if (!painter.end())
qWarning() << "QPainter::end failed";
另外,第一页之前不需要创建新页,否则会在其前面创建一个空白页。这个想法来自于这里:Is it possible to make a pdf with different page size in Qt?
英文:
You're repainting over your first page when you end your painter and start it again for the 2nd page, you should start at the start of your pdf file, and end it at its end.
Here's an example:
QPdfWriter writer("test.pdf");
writer.setPageSize(QPageSize(QPageSize::A7));
writer.setPageOrientation(QPageLayout::Landscape);
QPainter painter;
// First page
if (!painter.begin(&writer))
qWarning() << "QPainter::begin failed";
QTextDocument page1;
page1.setHtml("<h1 style=\"color: red\" align=center>Page 1</h1><p style=\"color: red\" align=justify>Test page 1</p>");
page1.drawContents(&painter);
// second page
if (!writer.newPage())
qWarning() << "newPage failed";
QTextDocument page2;
page2.setHtml("<h1 style=\"color: red\" align=center>Page 2</h1><p style=\"color: red\" align=justify>Test page 2</p>");
page2.drawContents(&painter);
if (!painter.end())
qWarning() << "QPainter::end failed";
Also, no need for a new page before the first page, you'll end up creating an empty page before it.
Here's where I got the idea: Is it possible to make a pdf with different page size in Qt?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论