简体   繁体   English

使用qt + qprinter将工作网址添加到pdf中

[英]add working url into pdf, using qt + qprinter

Is there a simple way to add a working/clikable url into a pdf, using c++, qt and the qprinter class? 是否有一种简单的方法可以使用c ++,qt和qprinter类将工作/可clik的url添加到pdf中? To clarify it a little bit: I want to add the url during generating a new document. 为了澄清一点:我想在生成新文档时添加url。 I'm not trying to edit an existing pdf. 我不是要编辑现有的pdf。

You can create a PDF with Qprinter using QTextDocument. 您可以使用QTextDocument使用Qprinter创建PDF。 QTextDocument has setHtml() method which lets you use html tags including <a> tag ( <a href='http://www.google.com'>google</a> ). QTextDocument具有setHtml()方法,允许您使用包含<a>标签( <a href='http://www.google.com'>google</a> )的html标签。 After that, you can create PDF with that HTML using QTextDocument print() method. 之后,您可以使用QTextDocument print()方法使用该HTML创建PDF。

Code Example: 代码示例:

QPrinter printer(QPrinter::HighResolution);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName("out.pdf");

QTextDocument document;
QString html = "<a href='http://www.google.com'>google</a>";
document.setHtml(html);

document.print( &printer ); 

After running this code you will have out.pdf file with a clickable text: "google" that links to "http://www.google.com" 运行此代码后,您将获得带有可点击文本的out.pdf文件:链接到“http://www.google.com”的“google”

PS You could even add an image using <img> tag. PS您甚至可以使用<img>标签添加图像。

I do not believe so, no. 我不相信,不。 Qt doesn't have any classes for editing PDFs, and I'm not quite sure what you'd do with QPrinter. Qt没有任何编辑PDF的课程,我不太清楚你用QPrinter做什么。 All QPrinter can do is generate new PDFs given a non-PDF source. 所有QPrinter都可以在给定非PDF源的情况下生成新的PDF。 PDFs are very nearly write-only. PDF几乎是只写的。 The format is proprietary and quite complicated, so in the free software world it is quite rare to see any sort of code that can do editing to PDFs beyond page reordering or other simple metadata changes. 这种格式是专有的,非常复杂,因此在自由软件世界中,很少见到任何类型的代码可以在页面重新排序或其他简单的元数据更改之外编辑为PDF。 If you're trying to edit an existing PDF, you're most likely on your own. 如果您正在尝试编辑现有PDF,则最有可能是您自己的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM