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.

all 22 comments

sorted by: hot top controversial new old
[–] 4 points 1 year ago* (3 children)

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?

  • source
  • hideshow 3 child comments
  • [–] [S] 2 points 1 year ago (2 children)

    I'm really sorry the pdf contains some personal information. May I ask how you process PDF as HTML?

  • source
  • parent
  • hideshow 2 child comments
  • [–] 2 points 1 year ago* (last edited 1 year ago) (1 child)

    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

  • source
  • parent
  • hideshow 1 child comment
  • [+] 4 points 1 year ago* (last edited 1 year ago) (3 children)
  • [–] [S] 3 points 1 year ago (2 children)

    Much appreciated. Is there an Opensource alternative for this?

  • source
  • parent
  • hideshow 2 child comments
  • [–] 2 points 1 year ago* (1 child)

    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.

  • source
  • parent
  • hideshow 1 child comment
  • [–] 3 points 1 year 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.

  • source
  • [–] 2 points 1 year ago (1 child)

    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.

  • source
  • hideshow 1 child comment
  • [–] 1 point 1 year ago (1 child)
  • [–] 1 point 1 year ago

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

  • source
  • [–] 0 points 1 year 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.

  • source
  • [+] -1 points 1 year ago (2 children)
  • [–] -2 points 1 year ago* (3 children)

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

  • source
  • hideshow 3 child comments