PDA

View Full Version : How to write test into PDF file



phillip_Qt
16th August 2013, 11:08
Dear Experts,

I want to create a pdf file and the contents will vry dynamically. e.g i want to write like below


HEADINGS
1

DESCRIPTION
My description for 1

HEADINGS
2

DESCRIPTION
My description for 2 goes here

DESCRIPTION & HEADING are static text and description is dynamic input provided by user. it may b mail id or description about . i've a textedit control where user will provide the description.

Please let me know how to generate a pdf file and write my text into it.

I used QPrnter class. but i dont know when my text is exceeding a page, so that i can create a new page.
Thanks in advance for your help

pkj
16th August 2013, 11:53
Refer this link... do similarly... http://doc.qt.digia.com/4.6/printing.html#painting-onto-a-page

Refer this link... do similarly... http://doc.qt.digia.com/4.6/printing.html#painting-onto-a-page

Added after 4 minutes:

QTextEdit from 4.3 onwards also has a convenience function QTextEdit::print(QPrinter *printer)
Use something like, (not tested)


QPrinter printer(QPrinter::HighResolution);
printer.setOutputFileName("print.ps");
QPainter painter;
painter.begin(&printer);
QTextEdit text;
text.setText("ur text here");
text.print(&printer);

phillip_Qt
16th August 2013, 13:14
Refer this link... do similarly... http://doc.qt.digia.com/4.6/printing.html#painting-onto-a-page

Refer this link... do similarly... http://doc.qt.digia.com/4.6/printing.html#painting-onto-a-page

Added after 4 minutes:

QTextEdit from 4.3 onwards also has a convenience function QTextEdit::print(QPrinter *printer)
Use something like, (not tested)


QPrinter printer(QPrinter::HighResolution);
printer.setOutputFileName("print.ps");
QPainter painter;
painter.begin(&printer);
QTextEdit text;
text.setText("ur text here");
text.print(&printer);


Hi,
Thanks for the reply. I tried like above code. I've passes .pdf instead of .ps file format. Though its generating file its not appending the text "ur text here inside the file.

If i go as per link, i've to draw text in my pdf file and i dont know when to create a new page, as the input text is dynamic for me. Please help.

pkj
16th August 2013, 14:11
Just tried a simple example...


void MainWindow::printPdf()
{
QPrinter printer(QPrinter::HighResolution);
printer.setOutputFileName("print.pdf");
ui->textEdit->print(&printer);
}

textEdit is a object in ui designer file mainWindow. Text edit is not a must. You can use QPainter or refer to the source of Qt to see how a QTextDocument is being used to print.