无法使用 QPdfWriter 添加新页面到 PDF 文件。

huangapple go评论81阅读模式
英文:

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(&quot;test.pdf&quot;);
writer.setPageSize(QPageSize(QPageSize::A7));
writer.setPageOrientation(QPageLayout::Landscape);

QPainter painter;

// First page
if (!painter.begin(&amp;writer))
   qWarning() &lt;&lt; &quot;QPainter::begin failed&quot;;
QTextDocument page1;
page1.setHtml(&quot;&lt;h1 style=\&quot;color: red\&quot; align=center&gt;Page 1&lt;/h1&gt;&lt;p style=\&quot;color: red\&quot; align=justify&gt;Test page 1&lt;/p&gt;&quot;);
page1.drawContents(&amp;painter);

// second page
if (!writer.newPage())
   qWarning() &lt;&lt; &quot;newPage failed&quot;;
QTextDocument page2;
page2.setHtml(&quot;&lt;h1 style=\&quot;color: red\&quot; align=center&gt;Page 2&lt;/h1&gt;&lt;p style=\&quot;color: red\&quot; align=justify&gt;Test page 2&lt;/p&gt;&quot;);
page2.drawContents(&amp;painter);
if (!painter.end())
   qWarning() &lt;&lt; &quot;QPainter::end failed&quot;;

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?

huangapple
  • 本文由 发表于 2023年5月22日 18:08:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定