5
$\begingroup$

How can I import a PDF file as a Graphics object. When calling Import["filename.pdf","PDF"], Mathematica always imports the PDF as a list of Images.

If there is a way to import it as Graphics can this be set as the default?

$\endgroup$
4
  • 5
    $\begingroup$ Import["filename.pdf",{"PDF", "PageGraphics"}] $\endgroup$ Commented Aug 1, 2021 at 17:06
  • $\begingroup$ @AlexeyPopkov Thank you this works! Is there also a way to make this the default behavior? $\endgroup$
    – Qbyte
    Commented Aug 1, 2021 at 17:12
  • 2
    $\begingroup$ New behavior (and solution) documented here. $\endgroup$
    – Alan
    Commented Aug 1, 2021 at 20:33
  • $\begingroup$ @Qbyte I don't think there is a "legitimate" way to set the old behavior as the default behavior: changing the default behavior of built-in functions is discouraged. You can use the short form Import["filename.pdf", "PageGraphics"]. $\endgroup$ Commented Aug 2, 2021 at 2:13

2 Answers 2

8
$\begingroup$

As you can read on the Documentation page "Incompatible Changes since Mathematica Version 1" (thanks Alan for the link!), starting from version 12.2

The default element for Import[file,"PDF"] has been changed from Graphics to Image. Previous behavior can be accessed by specifying "PageGraphics" upon import.

There are two ways to do this:

  1. Full form:

    Import["filename.pdf", {"PDF", "PageGraphics"}]
    
    ImportString[string, {"PDF", "PageGraphics"}]
    
  2. Short form:

     Import["filename.pdf", "PageGraphics"]
    
     ImportString[string, "PageGraphics"]
    

Is there also a way to make this the default behavior?

I don't think there is a "legitimate" way to set the old behavior as the default behavior: changing the default behavior of built-in functions is discouraged. Just use "PageGraphics" instead of "PDF" as the second argument of Import when importing pdf files (starting from Mathematica version 12.2).

Alternatively, you can use the legacy (pre-version 12.2) syntax and specify "Pages" as the second argument - in version 12.3.1 this syntax is undocumented but still works (thanks chyanog for pointing this out!):

Import["filename.pdf", "Pages"]

ImportString[string, "Pages"]

The advantage is that you can use the same code for the new and legacy versions and avoid the usage of constructs like

ImportString[string, If[$VersionNumber >= 12.2, {"PDF", "PageGraphics"}, 
                                                {"PDF", "Pages"}]]

The disadvantage is that the support for this (now undocumented) syntax can be dropped in future versions of Mathematica.

$\endgroup$
2
  • 2
    $\begingroup$ A little shorter, Import["filename.pdf", "Pages"]. $\endgroup$
    – chyanog
    Commented Aug 2, 2021 at 6:26
  • $\begingroup$ @chyanog In version 12.3.1 this legacy syntax is undocumented but still works, thank you. $\endgroup$ Commented Aug 2, 2021 at 9:14
4
$\begingroup$

An internal hack, and therefore not guaranteed to work in future, to change the default element to "PageGraphics" is as follows:

System`ConvertersDump`defaultElement[Import]["PDF"] = 
 {"PageGraphics"};
Import["ExampleData/mathematica.pdf"]
Head /@ %
(*  {Graphics}  *)

The change will persist until reset or the kernel session ends. It should be relatively safe. The default definition is

System`ConvertersDump`defaultElement[Import]["PDF"] = 
 {"PageImages"};
$\endgroup$
3
  • $\begingroup$ Thank you! How did you find this setting? $\endgroup$
    – Qbyte
    Commented Aug 4, 2021 at 0:14
  • 3
    $\begingroup$ @Qbyte, Darn, but I've forgotten. Here's a different way. I think to myself: "How does "PageImages" show up in the code? So foo = Trace[Import["ExampleData/mathematica.pdf"], TraceInternal -> True]; Position[foo, "PageImages"] shows where. The first few positions end in 32; these are each a list of all the PDF elements available. So take the first one that does not end in 32 (it ends in 1) and examine this part of foo. Repeatedly take away the last number of the position until you get to foo[[4, 2, 3, 9, 2, 2, 1, 2, 4, 2, 30, 1, 18, 15, 5, 1, 1, 1, 1, 4]], which hits paydirt. $\endgroup$
    – Michael E2
    Commented Aug 4, 2021 at 4:33
  • 2
    $\begingroup$ I did look up RegisterFormat (search this site), and examined Import.m in Mathematica.app/Contents/SystemFiles/Formats/PDF starting in the installation directory (MacOS). There a "DefaultElement" is registered, so I searched for ?*onvert*`*efault* (you can never be sure that internal functions will start with a capital). $\endgroup$
    – Michael E2
    Commented Aug 4, 2021 at 4:44

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.