Render PDF Page with QtPDF with its text
-
My goal is to merge two pdfs but preserve the text in the pages so i can search the document later. I want to do this with QtPDF because adding poppler to Qt on windows has proven to be pretty hard.
void mergePDFs(const QStringList& fileList, const QString& outputFileName) { QPdfWriter pdfWriter(outputFileName); pdfWriter.setResolution(150); QPdfDocument firstFilePDF; firstFilePDF.load(fileList.first()); QPageSize pageSize = QPageSize(firstFilePDF.pagePointSize(1).toSize()); pdfWriter.setPageSize(pageSize); firstFilePDF.close(); QPainter painter(&pdfWriter); foreach(const QString& file, fileList) { QPdfDocument pdfDocument; pdfDocument.load(file); for(int i = 0; i < pdfDocument.pageCount(); i++) { QPdfDocumentRenderOptions renderOptions = QPdfDocumentRenderOptions(); renderOptions.setRenderFlags(QPdfDocumentRenderOptions::RenderFlag::Annotations); QPageSize pageSize = QPageSize(pdfDocument.pagePointSize(i).toSize()); pdfWriter.setPageSize(pageSize); QSize scaledSize = pdfDocument.pagePointSize(i).toSize()*=(2); QImage image = pdfDocument.render(i, scaledSize); painter.drawImage(QPoint(0, 0), image); pdfWriter.newPage(); } pdfDocument.close(); } painter.end(); }
Right now im using this function and it works but i cant search the merged pdf. Can anybody offer any help?
-
Hi,
What do you mean by "search the document later" and "i cant search the merged pdf"?
Exactly, how, when and where do you try to do that? And why can't it be achieved?
I also feel like your function here has nothing to do with the problem. Can it be reproduced by using a multipage PDF document directly?
-
Hi,
What do you mean by "search the document later" and "i cant search the merged pdf"?
Exactly, how, when and where do you try to do that? And why can't it be achieved?
I also feel like your function here has nothing to do with the problem. Can it be reproduced by using a multipage PDF document directly?
@Abderrahmene_Rayene
First of all sorry for not answering in time, i didnt receive any notification of your message.
I want to search the merged PDF for words. I want to use this tool to append different scripts from my university into one complete script. So when i want to look up a keyword like "pnp transistor" i can search the whole script of the lecture and see where its mentioned.
This isn't possible because the render function renders the page to a image and my pdf viewer (ie Edge or FireFox) cant search images for text.
When/Where: When I have opened the PDF in my browser and want to search it by keyword.
How: "ctrl + f" in the browser.
Why: Because what was previously text is now an image after it was merged. This image is now written to the new merged PDF with the help of a QPainter object. -
@Abderrahmene_Rayene
First of all sorry for not answering in time, i didnt receive any notification of your message.
I want to search the merged PDF for words. I want to use this tool to append different scripts from my university into one complete script. So when i want to look up a keyword like "pnp transistor" i can search the whole script of the lecture and see where its mentioned.
This isn't possible because the render function renders the page to a image and my pdf viewer (ie Edge or FireFox) cant search images for text.
When/Where: When I have opened the PDF in my browser and want to search it by keyword.
How: "ctrl + f" in the browser.
Why: Because what was previously text is now an image after it was merged. This image is now written to the new merged PDF with the help of a QPainter object.@CPTNLucky said in Render PDF Page with QtPDF with its text:
This isn't possible because the render function renders the page to a image and my pdf viewer (ie Edge or FireFox) cant search images for text.
Indeed. So merging via a "painter" is not going to be suitable. Presumably you need a library which merges PDF documents retaining their PDF-text-ness.
-
@CPTNLucky said in Render PDF Page with QtPDF with its text:
This isn't possible because the render function renders the page to a image and my pdf viewer (ie Edge or FireFox) cant search images for text.
Indeed. So merging via a "painter" is not going to be suitable. Presumably you need a library which merges PDF documents retaining their PDF-text-ness.
-