void pdf::generatePdf() {
printer.
setOrientation(QPrinter::Portrait);
//set the orientation of the paper printer.
setOutputFormat(QPrinter::PdfFormat);
//make that printer as a PDF printer.setOutputFileName("filename.pdf"); //set the PDF file name
printer.
setPaperSize(QPrinter::Letter);
//set paper size that the PDF uses printer.
setPageMargins (0.5,
0.5,
0.5,
0.5,
QPrinter::Inch);
//1/2 inch margins QPainter painter
(&printer
);
//make a painter, which uses this printer,
//draw a blue round edged rectangle with the word "Hi" in it:
painter.setPen(Qt::blue);
painter.
setFont(QFont("Arial",
30));
QRectF r1
(100,
200,
100,
100);
painter.drawText(r1, Qt::AlignCenter, "Hi");
painter.drawRoundedRect(r1, 15.0, 15.0);
//draw a black line from the upper left, to the lower right side of the page
painter.setPen(Qt::black);
painter.drawLine(line);
//start a new page:
printer.newPage();
//draw a red box that reads "hello!" in it:
painter.setPen(Qt::red);
painter.
setFont(QFont("Arial",
30));
painter.drawText(r2, Qt::AlignCenter, "Hello!");
painter.drawRect(r2);
//draw a green dashed/dotted line in thick stroke, from upper right to lower left of page:
QPen pen
(Qt
::green,
3, Qt
::DashDotLine, Qt
::RoundCap, Qt
::RoundJoin);
painter.setPen(pen);
painter.drawLine(line);
//end the painter causing the PDF to be generated as "filename.pdf":
painter.end(); //done drawing, so save the PDF
}
void pdf::generatePdf() {
QPrinter printer; //create a printer
printer.setOrientation(QPrinter::Portrait); //set the orientation of the paper
printer.setOutputFormat(QPrinter::PdfFormat); //make that printer as a PDF
printer.setOutputFileName("filename.pdf"); //set the PDF file name
printer.setPaperSize(QPrinter::Letter); //set paper size that the PDF uses
printer.setPageMargins (0.5, 0.5, 0.5, 0.5, QPrinter::Inch); //1/2 inch margins
QPainter painter(&printer); //make a painter, which uses this printer,
//draw a blue round edged rectangle with the word "Hi" in it:
painter.setPen(Qt::blue);
painter.setFont(QFont("Arial", 30));
QRectF r1(100, 200, 100, 100);
painter.drawText(r1, Qt::AlignCenter, "Hi");
painter.drawRoundedRect(r1, 15.0, 15.0);
//draw a black line from the upper left, to the lower right side of the page
painter.setPen(Qt::black);
QLineF line(0, 0, 576, 756);
painter.drawLine(line);
//start a new page:
printer.newPage();
//draw a red box that reads "hello!" in it:
painter.setPen(Qt::red);
painter.setFont(QFont("Arial", 30));
QRectF r2(10, 30, 100, 100);
painter.drawText(r2, Qt::AlignCenter, "Hello!");
painter.drawRect(r2);
//draw a green dashed/dotted line in thick stroke, from upper right to lower left of page:
QPen pen(Qt::green, 3, Qt::DashDotLine, Qt::RoundCap, Qt::RoundJoin);
painter.setPen(pen);
QLineF line2(0, 756, 576, 0);
painter.drawLine(line);
//end the painter causing the PDF to be generated as "filename.pdf":
painter.end(); //done drawing, so save the PDF
}
To copy to clipboard, switch view to plain text mode
you can then play around with more lines, positions, images, getting text from a database, etc which you can add using the painter. You can also print rich text documents to PDF like this, there is a text editor example that comes with Qt and within there is code to convert rich text documents to PDF if they are already in that format.
Bookmarks