Add Image to PDF - Python Sample Code

Sample code to use Apryse SDK for programmatically inserting various raster image formats (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) into a PDF document. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Learn more about our Server SDK and PDF Editing & Manipulation Library.

1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14#-----------------------------------------------------------------------------------
15# This sample illustrates how to embed various raster image formats
16# (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
17#
18# Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
19# be present in the system path.
20#-----------------------------------------------------------------------------------
21
22def main():
23 PDFNet.Initialize(LicenseKey)
24
25 # Relative path to the folder containing test files.
26 input_path = "../../TestFiles/"
27 output_path = "../../TestFiles/Output/"
28
29 doc = PDFDoc()
30
31 f = ElementBuilder() # Used to build new Element objects
32 writer = ElementWriter() # Used to write Elements to the page
33
34 page = doc.PageCreate() # Start a new page
35 writer.Begin(page) # Begin writing to this page
36
37 # ----------------------------------------------------------
38 # Add JPEG image to the output file
39 img = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
40 element = f.CreateImage(img, 50, 500, img.GetImageWidth()/2, img.GetImageHeight()/2)
41 writer.WritePlacedElement(element)
42
43 # ----------------------------------------------------------
44 # Add a PNG image to the output file
45 img = Image.Create(doc.GetSDFDoc(), input_path + "butterfly.png")
46 element = f.CreateImage(img, Matrix2D(100, 0, 0, 100, 300, 500))
47 writer.WritePlacedElement(element)
48
49 # ----------------------------------------------------------
50 # Add a GIF image to the output file
51 img = Image.Create(doc.GetSDFDoc(), input_path + "pdfnet.gif")
52 element = f.CreateImage(img, Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 50, 350))
53 writer.WritePlacedElement(element)
54
55 # ----------------------------------------------------------
56 # Add a TIFF image to the output file
57
58 img = Image.Create(doc.GetSDFDoc(), (input_path + "grayscale.tif"))
59 element = f.CreateImage(img, Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 10, 50))
60 writer.WritePlacedElement(element)
61
62 writer.End() # Save the page
63 doc.PagePushBack(page) # Add the page to the document page sequence
64
65 # ----------------------------------------------------------
66 # Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.
67
68 page = doc.PageCreate(Rect(0, 0, 612, 794))
69 writer.Begin(page) # begin writing to this page
70
71 # Note: encoder hints can be used to select between different compression methods.
72 # For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
73 hint_set = ObjSet();
74 enc = hint_set.CreateArray(); # Initilaize encoder 'hint' parameter
75 enc.PushBackName("JBIG2");
76 enc.PushBackName("Lossy");
77
78 img = Image.Create(doc.GetSDFDoc(), input_path + "multipage.tif");
79 element = f.CreateImage(img, Matrix2D(612, 0, 0, 794, 0, 0));
80 writer.WritePlacedElement(element);
81
82 writer.End() # Save the page
83 doc.PagePushBack(page) # Add the page to the document page sequence
84
85 # ----------------------------------------------------------
86 # Add a JPEG2000 (JP2) image to the output file
87
88 # Create a new page
89 page = doc.PageCreate()
90 writer.Begin(page) # Begin writing to the page
91
92 # Embed the image
93 img = Image.Create(doc.GetSDFDoc(), input_path + "palm.jp2")
94
95 # Position the image on the page
96 element = f.CreateImage(img, Matrix2D(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 96, 80))
97 writer.WritePlacedElement(element)
98
99 # Write 'JPEG2000 Sample' text string under the image
100 writer.WriteElement(f.CreateTextBegin(Font.Create(doc.GetSDFDoc(), Font.e_times_roman), 32))
101 element = f.CreateTextRun("JPEG2000 Sample")
102 element.SetTextMatrix(1, 0, 0, 1, 190, 30)
103 writer.WriteElement(element)
104 writer.WriteElement(f.CreateTextEnd())
105
106 writer.End() # Finish writing to the page
107 doc.PagePushBack(page)
108
109 doc.Save((output_path + "addimage.pdf"), SDFDoc.e_linearized);
110 doc.Close()
111 PDFNet.Terminate()
112
113 print("Done. Result saved in addimage.pdf...")
114
115if __name__ == '__main__':
116 main()

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales