22 $image->Export($output_filename); // or ExportAsTiff or ExportAsPng
23 // optionally, you can also extract uncompressed/compressed
24 // image data directly using element->GetImageData()
25 }
26 case Element::e_form:
27 {
28 $reader->FormBegin();
29 ProcessElements($reader);
30 $reader->End();
31 }
32 }
33 }
34}
1doc = PDFDoc(filename)
2reader = ElementReader()
3
4itr = doc.GetPageIterator()
5while itr.HasNext():
6 # Read the page
7 reader.Begin(itr.Current())
8 ProcessElements(reader)
9 reader.End()
10 itr.Next()
11
12def ProcessElements(reader):
13 # Traverse the page display list
14 element = reader.Next()
15 while element != None:
16 type = element.GetType()
17 if type == Element.e_image:
18 image = Image(element.GetXObject())
19 image.Export(path) # or ExportAsTiff or ExportAsPng
20 # optionally, you can also extract uncompressed/compressed
21 # image data directly using element.GetImageData()
22 elif type == Element.e_form:
23 reader.FormBegin()
24 ProcessElements(reader)
25 reader.End()
26 element = reader.Next()
1doc = PDFDoc.new(filename)
2reader = ElementReader.new()
3
4itr = doc.GetPageIterator()
5while itr.HasNext() do
6 # Read the page
7 reader.Begin(itr.Current())
8 ProcessElements(reader)
9 reader.End()
10 itr.Next()
11end
12
13def ProcessElements(reader)
14 # Traverse the page display list
15 element = reader.Next()
16 while !element.nil? do
17 type = element.GetType()
18 case type
19 when Element::E_image
20 image = Image.new(element.GetXObject())
21 image.Export(path) # or ExportAsTiff or ExportAsPng
22 # optionally, you can also extract uncompressed/compressed
23 # image data directly using element.GetImageData()
24 when Element::E_form:
25 reader.FormBegin()
26 ProcessElements(reader)
27 reader.End()
28 end
29 end
30end
1Dim doc As PDFDoc = New PDFDoc(filename)
2Dim reader As ElementReader = New ElementReader
3
4Dim itr As PageIterator = doc.GetPageIterator()
5While itr.HasNext()
6 ' Read the page
7 reader.Begin(itr.Current())
8 ProcessElements(reader)
9 reader.End()
10End While
11
12Sub ProcessElements(ByVal reader As ElementReader)
13 ' Traverse the page display list
14 Dim element As Element = reader.Next()
15 While Not IsNothing(element)
16 If element.GetType() = element.Type.e_image Then
17 Dim image As PDFTRON.PDF.Image = New PDFTRON.PDF.Image(element.GetXObject())
18 image.Export(output_filename) ' or ExporAsPng() or ExporAsTiff() ...
19 ' optionally, you can also extract uncompressed/compressed
20 ' image data directly using element.GetImageData()
21 ElseIf element.GetType() = element.Type.e_form Then
22 reader.FormBegin()
23 ProcessElements(reader)
24 reader.End()
25 End If
26 element = reader.Next()
27 End While
28End Sub
PDF image extraction Full code sample which illustrates a few approaches to PDF image extraction.
About reading page content
Page content is represented as a sequence of graphical Elements such as paths, text, images, and forms. The only effect of the ordering of Elements in the display list is the order in which Elements are painted. Elements that occur later in the display list can obscure earlier elements.
A display list can be traversed using an ElementReader object. To start traversing the display list, call reader.Begin(). Then, reader.Next() will return subsequent Elements until null is returned (marking the end of the display list).
While ElementReader only works with one page at a time, the same ElementReader object may be reused to process multiple pages.
About Form XObjects, Type3 font glyphs, and tiling patterns
A PDF page display list may contain child display lists of Form XObjects, Type3 font glyphs, and tiling patterns. A form XObject is a self-contained description of any sequence of graphics objects (such as path objects, text objects, and sampled images), defined as a PDF content stream. It may be painted multiple times — either on several pages or at several locations on the same page — and will produce the same results each time (subject only to the graphics state at the time the Form XObject is painted). In order to open a child display list for a Form XObject, call the reader.FormBegin() method. To return processing to the parent display list call reader.End(). Processing of the Form XObject display (traversing the child display list) is illustrated below.
Note that, in the above sample code, a child display list is opened when an element with type Element.ElementType.e_form is encountered by the reader.FormBegin() method. The child display list becomes the current display list until it is closed using reader.End(). At this point the processing is returned to the parent display list and the next Element returned will be the Element following the Form XObject. Also note that, because Form XObjects may be nested, a sub-display list could have its own child display lists. The sample above shows traversing these nested Form XObjects recursively.
Similarly, a pattern display list can be opened using reader.PatternBegin(), and a Type3 glyph display list can be opened using the reader.Type3FontBegin() method.