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 Image
s.
If there is a way to import it as Graphics
can this be set as the default?
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 fromGraphics
toImage
. Previous behavior can be accessed by specifying"PageGraphics"
upon import.
There are two ways to do this:
Full form:
Import["filename.pdf", {"PDF", "PageGraphics"}]
ImportString[string, {"PDF", "PageGraphics"}]
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.
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"};
"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$
Commented
Aug 4, 2021 at 4:33
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$
Commented
Aug 4, 2021 at 4:44
Import["filename.pdf", "PageGraphics"]
. $\endgroup$