So one of my pdfs has a page number and a link at the bottom of every page. It’s around 500 pages so I dont want to edit it manually. Is there any way I can delete those things all at once from all pages of the pdf?

Maybe ghost script or python script can do this?

I also notice there isn’t a PDF community in Lemmy, maybe somebody should create one.

Thanks a lot in advance.

  • thevoidzero@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    1 day ago

    I don’t know how comfortable you are writing your own, but pdf saves the components with coordinates, bounding box etc so you should be able to automate it with a small script that reads pdf components directly.

    Also try qpdf to convert pdf into qdf format, then you can open it in a text editor, find the element you want to remove. Look at examples of few pages, find the pattern and do regex replace. Make sure to keep a copy and check the diff before accepting it.

  • Cyberflunk@lemmy.world
    link
    fedilink
    arrow-up
    1
    arrow-down
    1
    ·
    19 hours ago

    Claude code wrote and opencv python app to remove every other word from the Declaration of Independence.

    Not really, but you wondered

  • HelloRoot@lemy.lol
    link
    fedilink
    English
    arrow-up
    4
    ·
    edit-2
    1 day ago

    A PDF is (or at least can be) similar to a HTML document on the inside. A long time ago we used that at my company to edit PDFs through java code.

    Is it possible for you to share the document so we can take a closer look at it? Or if you don’t want it on the internet, is there a way to share it privately?

      • Elvith Ma'for@feddit.org
        cake
        link
        fedilink
        arrow-up
        3
        ·
        1 day ago

        It’s not as HTML. It’s just that PDF is a structured file format (as is html, but very different). There are libraries for most programming languages that allow you to edit this structure.

      • HelloRoot@lemy.lol
        link
        fedilink
        English
        arrow-up
        2
        ·
        edit-2
        1 day ago

        to add to what Elvith wrote:

        you can read the HTML like structures inside a PDF and then find out details about the elements you want to remove and then remove them by using that found common property.

        This is very hard to do by hand. But if you are curious you can download https://file-examples.com/wp-content/storage/2017/10/file-sample_150kB.pdf

        and open it with a text editor like kate. You will see a lot of encoded content data, but also the “html-like” structure in plaintext (in between the encoded stuff but also more at the bottom)

        You might find that editing the PDF by hand will break it completely, that is because it is complicated. Iirc you’d need to fix the index, recalculate the checksum or do some other magic bullshit. But that is often taken care of by the library.

        Here is a shitty python example for that demo pdf that redacts the image at the last page by drawing a white rectangle over it. There is no way in pymupdf to delete an image or a textblock … but this is just an example. Other libraries might be able to do it (the one I used a decade ago in java could). I just wanted to point you in the general direction, hope you can see from here how iterating over all the pages, picking the right element and redacting it would work.

        import pymupdf  # PyMuPDF
        
        # Open the PDF
        doc = pymupdf.open("./file-sample_150kB.pdf")
        
        # Get the last page
        page = doc[-1]
        
        # Get all images on the page
        images = page.get_images(full=True)
        
        if images:
            # Get the xref of the first image
            xref = images[0][0]
        
            # Find all instances of the image and redact their bounding boxes
            for info in page.get_image_info(xrefs=True):
                if info["xref"] == xref:
                    rect = pymupdf.Rect(info["bbox"])
                    page.add_redact_annot(rect, fill=(1, 1, 1))  # white fill
        
            page.apply_redactions()
        
        # Save the modified PDF
        doc.save("./modified.pdf")
        doc.close()
        

        A way simpler approach might be to crop all pages at the bottom.

        import pymupdf  # PyMuPDF
        
        doc = pymupdf.open("input.pdf")  # open the PDF
        
        for page in doc:
            rect = page.rect  # original page size
            new_rect = pymupdf.Rect(rect.x0, rect.y0 + 100, rect.x1, rect.y1)  # crop bottom 100px
            page.set_cropbox(new_rect)
        
        doc.save("output.pdf")  # save the cropped PDF
        doc.close()
        

        Here are the docs: https://pymupdf.readthedocs.io/en/latest/the-basics.html

  • lol@discuss.tchncs.de
    link
    fedilink
    arrow-up
    4
    ·
    1 day ago

    I’ve done similar things with a regular PDF editor (Master PDF Editor for Linux in this case) by just selecting all the elements at once through a search and pressing delete.

    This should work with your link, but probably not the page number, since there’s nothing static you could search for.

      • Flagstaff@programming.dev
        link
        fedilink
        English
        arrow-up
        2
        ·
        edit-2
        1 day ago

        The freemium, offline-capable software Stirling-PDF is king of open-source PDF-editing. Nothing else comes even close, despite how counterintuitive its UI can be. Ironically enough, the creator said it was originally a ChatGPT experiment, but once it went viral, he ended up hiring others and they all regularly improve it as its staff.

        If you are on Windows, head over to @ahk@programming.dev and we’ll see what we can do about automating the removal per pg. via AutoHotkey.

    • happeningtofry99158@lemmy.worldOP
      link
      fedilink
      arrow-up
      1
      ·
      22 hours ago

      does it simply place an image of that size on top of every page at the desired position or does it redact the element underneath it as well?

  • Anon518@sh.itjust.works
    link
    fedilink
    English
    arrow-up
    1
    arrow-down
    3
    ·
    edit-2
    22 hours ago

    Why did you link to reddit? Can a mod/admin do something about this? People keep advertising reddit here.

  • beepbooprobot@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    1 day ago

    PDFGear might be able to handle this if your link is available in the page footer.

    Open the file, click edit, header & footer then update and make your changes globally across all 500 pages.

  • nexguy@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    1 day ago

    Just a thought, put a white square over the desired position on every page… quicker? (Not sure how to do it though)

  • VoxAliorum@lemmy.ml
    link
    fedilink
    arrow-up
    3
    arrow-down
    2
    ·
    edit-2
    1 day ago

    I tend towards: No. I can’t know for sure, but given how pdfs are structured this sounds very difficult.

    A workaround might be to automatically place white boxes over those, but you can probably still select the text underneath afterwards.

  • B0rax@feddit.org
    link
    fedilink
    arrow-up
    1
    arrow-down
    6
    ·
    1 day ago

    Ask chatgpt for a python script to do exactly that task. Maybe you get what you want, maybe not.

    Helped me out quite a few times with niche tasks like this.

    • Solumbran@lemmy.world
      link
      fedilink
      arrow-up
      6
      arrow-down
      2
      ·
      1 day ago

      Look at how to do it with python, you’ll learn interesting stuff, get a working result, and not destroy your brain using a chat simulator as a programming help.

      I don’t get why people are fine with comments that are as absurd as saying “to hang a painting, first stab a screwdriver in the wall then attach the painting to it, sometimes it’s not too bad”

      • B0rax@feddit.org
        link
        fedilink
        arrow-up
        1
        arrow-down
        3
        ·
        1 day ago

        How is asking chatgpt to do it any different as to using a different pre made tool? Both do not require programming.

        • Solumbran@lemmy.world
          link
          fedilink
          arrow-up
          4
          arrow-down
          1
          ·
          1 day ago

          How is using a spoon different as to using a military tank? They’re both tools after all.

        • golden_zealot@lemmy.ml
          link
          fedilink
          English
          arrow-up
          3
          ·
          1 day ago

          Pre-made tools have reproducible and known functionality that has been tested whereas LLM’s when generating this across 100 different users may come up with 100 different untested results in which someone who doesn’t know programming won’t really know what complete result to expect from the code it generates.

          In short, pre made tools don’t require programming knowledge because someone has handled all of this for you previously, but LLM’s do require programming knowledge to make sure what it made is going to work safely and correctly.

          • B0rax@feddit.org
            link
            fedilink
            arrow-up
            1
            arrow-down
            2
            ·
            1 day ago

            Sure, but the OP has a singular task to fulfill which he can easily check if it is done correctly. The premade tools don’t have the functionality, now what? Tell him to learn how to code?

            • golden_zealot@lemmy.ml
              link
              fedilink
              English
              arrow-up
              2
              ·
              edit-2
              20 hours ago

              Tell him to learn how to code?

              Optimally yes, the OP should learn some code before doing so - this task doesn’t seem that difficult to do with a script if you wrote it yourself, and it’s even less work to learn enough to just verify what the script is doing.

              I have no idea how a car works at a deep level. However I know enough to know how to drive, and if I see its mirrors are broken off, the seat belts are missing, or there is gas leaking out of it onto the ground, I probably shouldn’t get in and drive it.

              If you don’t understand code and run generated code, the problem is that you are stuck with a result that you may or may not have wanted. You may also just think it worked correctly when in fact It might have done other stuff as well that can’t be seen plainly - this is the inherent risk of running generated code where you can’t actually verify what it’s doing.

              Maybe it performs the requested function correctly but is sourcing the original code from a use case where someone also wanted to delete every other kind of file that wasn’t a pdf in that directory. Maybe not. But this is a difference of one line of code which can have major ramifications if it gets left in.

              The point is that if you aren’t certain what something does before you use it, you should at a minimum go through the necessary steps to be able to make an informed decision, otherwise it’s just reckless.

  • gi1242@lemmy.world
    link
    fedilink
    arrow-up
    1
    arrow-down
    2
    ·
    1 day ago

    pdftk can split it into pages, and then recombine the pages. not sure how to automatically remove an element from each page unfortunately.