.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtPrintSupport/QPrinter>
#include <QPainter>
#include <QFileDialog>
#include <QFile>
#include <QPdfWriter>
#include <QTextOption>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString fileName = QFileDialog::getSaveFileName(this,"Select Save Location",QApplication::applicationDirPath(),"pdf Files (*.pdf)");//创建要写入的文件名字
QFile pdfFile(fileName);//创建文件
pdfFile.open(QIODevice::WriteOnly);//打开文件
QPdfWriter *pdfWriter = new QPdfWriter(&pdfFile);//创建PDF写入对象
pdfWriter->setPageSize(QPagedPaintDevice::A4);//设置纸张大小
pdfWriter->setResolution(300);//设置纸张的分辨率为300,因此像素为3508*2479
pdfWriter->setTitle("?????");
int iMargin = 60;
pdfWriter->setPageMargins(QMarginsF(iMargin,iMargin,iMargin,iMargin));//设置页边距
//添加内容使用QPainter
QPainter *pdfPainter = new QPainter(pdfWriter);
//设置标题
QTextOption option(Qt::AlignHCenter | Qt::AlignVCenter);
option.setWrapMode(QTextOption::WordWrap);
int iTop = 100;
int iContentWidth = 2100;
//标题字体
QFont font;
font.setFamily("simhei.ttf");
int fontSize = 22;
font.setPointSize(fontSize);
pdfPainter->setFont(font);
pdfPainter->drawText(QRect( 0, iTop, iContentWidth, 90), QString::fromLocal8Bit("任务数据报告"),option);
fontSize = 16;
font.setPointSize(fontSize);
pdfPainter->setFont(font);
option.setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
iTop += 60;
pdfPainter->drawText(QRect(0, iTop, iContentWidth, 60),QString::fromLocal8Bit("1、目录一"));
iTop += 60;
// 左侧缩进2字符
int iLeft = 120;
//pdfPainter->drawText(QRect(iLeft, iTop, iContentWidth - iLeft, 60),QString::fromLocal8Bit("我的目录一的内容。"),option);
pdfPainter->drawText(QRect(iLeft,iTop,iContentWidth-iLeft,60),QString::fromLocal8Bit("我的目录一的内容。 "),option);
iTop += 60;
pdfPainter->drawText(QRect(0, iTop, iContentWidth, 60), QString::fromLocal8Bit("2、目录二"));
iTop += 60;
pdfPainter->drawText(QRect(iLeft, iTop, iContentWidth - iLeft, 60),QString::fromLocal8Bit("我的目录一的内容 "), option);
delete pdfPainter;
delete pdfWriter;
pdfFile.close();
}
MainWindow::~MainWindow()
{
delete ui;
}
.h:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtPrintSupport/QPrinter>
#include <QPainter>
#include <QFileDialog>
#include <QFile>
#include <QPdfWriter>
#include <QTextOption>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString fileName = QFileDialog::getSaveFileName(this,"Select Save Location",QApplication::applicationDirPath(),"pdf Files (*.pdf)");//创建要写入的文件名字
QFile pdfFile(fileName);//创建文件
pdfFile.open(QIODevice::WriteOnly);//打开文件
QPdfWriter *pdfWriter = new QPdfWriter(&pdfFile);//创建PDF写入对象
pdfWriter->setPageSize(QPagedPaintDevice::A4);//设置纸张大小
pdfWriter->setResolution(300);//设置纸张的分辨率为300,因此像素为3508*2479
pdfWriter->setTitle("?????");
int iMargin = 60;
pdfWriter->setPageMargins(QMarginsF(iMargin,iMargin,iMargin,iMargin));//设置页边距
//添加内容使用QPainter
QPainter *pdfPainter = new QPainter(pdfWriter);
//设置标题
QTextOption option(Qt::AlignHCenter | Qt::AlignVCenter);
option.setWrapMode(QTextOption::WordWrap);
int iTop = 100;
int iContentWidth = 2100;
//标题字体
QFont font;
font.setFamily("simhei.ttf");
int fontSize = 22;
font.setPointSize(fontSize);
pdfPainter->setFont(font);
pdfPainter->drawText(QRect( 0, iTop, iContentWidth, 90), QString::fromLocal8Bit("任务数据报告"),option);
fontSize = 16;
font.setPointSize(fontSize);
pdfPainter->setFont(font);
option.setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
iTop += 60;
pdfPainter->drawText(QRect(0, iTop, iContentWidth, 60),QString::fromLocal8Bit("1、目录一"));
iTop += 60;
// 左侧缩进2字符
int iLeft = 120;
//pdfPainter->drawText(QRect(iLeft, iTop, iContentWidth - iLeft, 60),QString::fromLocal8Bit("我的目录一的内容。"),option);
pdfPainter->drawText(QRect(iLeft,iTop,iContentWidth-iLeft,60),QString::fromLocal8Bit("我的目录一的内容。 "),option);
iTop += 60;
pdfPainter->drawText(QRect(0, iTop, iContentWidth, 60), QString::fromLocal8Bit("2、目录二"));
iTop += 60;
pdfPainter->drawText(QRect(iLeft, iTop, iContentWidth - iLeft, 60),QString::fromLocal8Bit("我的目录一的内容 "), option);
delete pdfPainter;
delete pdfWriter;
pdfFile.close();
}
MainWindow::~MainWindow()
{
delete ui;
}
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}