2
$\begingroup$

I need to extract 10 first pages of around 100 PDF files. As you know, Adobe Acrobat DC doesn't allow doing so at once, and I have to extract 10 pages for each file separately. I wrote the following little code that performs such a task

SetDirectory[NotebookDirectory[]];
all = FileNames[];
npage = 10;
Do[
 Clear[orgPDF, newPDF, vname, name, newname];
 SetDirectory[NotebookDirectory[]];
 orgPDF = Import[all[[i]], "PDF"];
 
 newPDF = 
  CreateDocument[orgPDF[[1 ;; npage]], PageBreakBelow -> True, 
   Visible -> False];
 SetDirectory["exPDFfolder"];
 
 vname = all[[i]];
 name = StringSplit[vname, "."][[1]];
 newname = StringTemplate["new_`1`.pdf"][name];
 Export[newname, newPDF, "AllowRasterization" -> True]
 
 , {i, Length[all] - 2}]

Where all PDF files are placed in a same folder and I set loop number to Length[all] - 2 because of excluding the nb file and also exPDFfolder folder. The only problem with this code is that exported/extracted PDF files include pages with lower quality and also very high margins, which doesn't look good. I think the way of importing PDF file (like graphic, not text) leads to the problem. How can I extract the first 10 pages as the same of original PDF file?

Addendum:

Unlike the comments say this is not the best way, I think it's a good idea to take advantage of Mathemtica features which are not available easily in appropriate software programs. In this case, Acrobat DC (the most powerful tool for manipulating PDF files) doesn't have a bulk extraction feature (although it has such a feature for watermarking for example).

To see what happens, I put here one of my original and extracted files

$\endgroup$
4
  • $\begingroup$ I myself would do the 100 PDFs by hand - not the nicest job. But, in my experience, in many cases faster than writing + testing software. Only when the job is considerably more than ~100 repetitions, I will write some software. $\endgroup$ Commented Jan 30, 2023 at 19:55
  • $\begingroup$ Without having your exact pdf files, it's hard to debug this. Can you at least narrow it down to where the problem occurs? I know you said you "think the way of importing" is the problem, but you included code to CreateDocument and Export, so I'm not sure if you just meant that a problem occurs somewhere along the way. Can you provide details about what things look like after each step? Also, I'm guessing that you'll lose the metadata if you do it this way--is that acceptable? And do you even need pdf as the output, or would some other format suffice? $\endgroup$
    – lericr
    Commented Jan 30, 2023 at 20:38
  • $\begingroup$ Also, FWIW I think there are pdf editors out there, and maybe it's easier to automate this task in that context rather than in Mathematica. $\endgroup$
    – lericr
    Commented Jan 30, 2023 at 20:39
  • $\begingroup$ I cannot open your example files. Have you tried the import format like Import["article.pdf", {"PageGraphics", 10}]? The document says "PageGraphics" is vector-like comparing with "PageImages". $\endgroup$
    – Lacia
    Commented Feb 6, 2023 at 19:20

1 Answer 1

4
$\begingroup$

Suggestion: do it with something other than Wolfram Language. Or at least don't do the page extraction part in WL.

There is no standard way to represent a PDF page internally. I suspect that WL's Import converts it into an image internally, so you will potentially lose some information (like copy-passable text) this way.

Instead, use a dedicated command-line tool supported by your platform (e.g., with pdftk or something similar), and just automate the execution of this command-line tool with Wolfram Language with RunProcess and friends if you need to.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.