Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Render PDF Page with QtPDF with its text
Forum Updated to NodeBB v4.3 + New Features

Render PDF Page with QtPDF with its text

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 613 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CPTNLucky
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      CPPUIX
      wrote on last edited by
      #2

      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?

      C 1 Reply Last reply
      0
      • C CPPUIX

        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?

        C Offline
        C Offline
        CPTNLucky
        wrote on last edited by CPTNLucky
        #3

        @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.

        JonBJ 1 Reply Last reply
        0
        • C CPTNLucky

          @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.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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.

          C 1 Reply Last reply
          2
          • JonBJ JonB

            @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.

            C Offline
            C Offline
            CPTNLucky
            wrote on last edited by
            #5

            @JonB Yes that was my conclusion too, but finding such a lib has proven to be quite difficult. Building Poppler isnt exactly easy and there arent many alternatives to that aside from Hummus.

            1 Reply Last reply
            0
            • C CPPUIX referenced this topic on

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved