程序中数据导出是经常有的需求,今天学习把文本导出到pdf文件。主要是用QPrinter,QPainter

  1. TextEditToPdf::TextEditToPdf(QWidget *parent, Qt::WFlags flags)
  2. : QDialog(parent, flags)
  3. {
  4. ui.setupUi(this);
  5. int ret = connect(ui.m_pExportBtn,SIGNAL(clicked()),this,SLOT(exportSlot()));
  6. }
  7. TextEditToPdf::~TextEditToPdf()
  8. {
  9. }
  10. void TextEditToPdf::exportSlot()
  11. {
  12. QTextDocument  textDocument;
  13. QPrinter printer(QPrinter::ScreenResolution);
  14. printer.setPaperSize(QPrinter::A4);
  15. printer.setPageMargins(30,30,30,30,QPrinter::DevicePixel);
  16. printer.setOutputFormat(QPrinter::PdfFormat);
  17. printer.setOutputFileName(QString("11") + ".pdf");
  18. QPainter painter( &printer );
  19. QFont font;
  20. font.setPixelSize(15);
  21. painter.setFont(font);
  22. QTextOption textOption( Qt::AlignCenter);
  23. QPen pen;
  24. pen.setColor(Qt::black);
  25. pen.setWidth(1);
  26. painter.setPen(pen);
  27. int paperWidth = printer.width();
  28. int paperHeigth = printer.height();
  29. painter.drawText( QRect(QPoint( 30,30),QPoint(paperWidth - 30,paperHeigth- 20)),ui.m_pTextEdit->toPlainText (),textOption);
  30. textDocument.print(&printer);
  31. }

文本导出到pdf文件(使用QPrinter和QPainter和QTextDocument)-LMLPHP

文本导出到pdf文件(使用QPrinter和QPainter和QTextDocument)-LMLPHP

http://blog.csdn.net/hai200501019/article/details/37576715

04-13 17:58