Add Image to PDF - Ruby 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
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12#-----------------------------------------------------------------------------------
13# This sample illustrates how to embed various raster image formats
14# (e.g. TIFF, JPEG, JPEG2000, JBIG2, GIF, PNG, BMP, etc.) in a PDF document.
15#
16# Note: On Windows platform this sample utilizes GDI+ and requires GDIPLUS.DLL to
17# be present in the system path.
18#-----------------------------------------------------------------------------------
19
20 PDFNet.Initialize(PDFTronLicense.Key)
21
22 # Relative path to the folder containing test files.
23 input_path = "../../TestFiles/"
24 output_path = "../../TestFiles/Output/"
25
26 doc = PDFDoc.new()
27 f = ElementBuilder.new() # Used to build new Element objects
28 writer = ElementWriter.new() # Used to write Elements to the page
29
30 page = doc.PageCreate() # Start a new page
31 writer.Begin(page) # Begin writing to this page
32
33 # ----------------------------------------------------------
34 # Add JPEG image to the output file
35 img = Image.Create(doc.GetSDFDoc(), input_path + "peppers.jpg")
36 element = f.CreateImage(img, 50, 500, img.GetImageWidth()/2, img.GetImageHeight()/2)
37 writer.WritePlacedElement(element)
38
39 # ----------------------------------------------------------
40 # Add a PNG image to the output file
41 img = Image.Create(doc.GetSDFDoc(), input_path + "butterfly.png")
42 element = f.CreateImage(img, Matrix2D.new(100, 0, 0, 100, 300, 500))
43 writer.WritePlacedElement(element)
44
45 # ----------------------------------------------------------
46 # Add a GIF image to the output file (This section is not supported on Linux)
47 img = Image.Create(doc.GetSDFDoc(), input_path + "pdfnet.gif")
48 element = f.CreateImage(img, Matrix2D.new(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 50, 350))
49 writer.WritePlacedElement(element)
50
51 # ----------------------------------------------------------
52 # Add a TIFF image to the output file
53
54 img = Image.Create(doc.GetSDFDoc(), (input_path + "grayscale.tif"))
55 element = f.CreateImage(img, Matrix2D.new(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 10, 50))
56 writer.WritePlacedElement(element)
57
58 writer.End() # Save the page
59 doc.PagePushBack(page) # Add the page to the document page sequence
60
61 # ----------------------------------------------------------
62 # Embed a monochrome TIFF. Compress the image using lossy JBIG2 filter.
63
64 page = doc.PageCreate(Rect.new(0, 0, 612, 794))
65 writer.Begin(page) # begin writing to this page
66
67 # Note: encoder hints can be used to select between different compression methods.
68 # For example to instruct PDFNet to compress a monochrome image using JBIG2 compression.
69 hint_set = ObjSet.new();
70 enc = hint_set.CreateArray(); # Initilaize encoder 'hint' parameter
71 enc.PushBackName("JBIG2");
72 enc.PushBackName("Lossy");
73
74 img = Image.Create(doc.GetSDFDoc(), input_path + "multipage.tif");
75 element = f.CreateImage(img, Matrix2D.new(612, 0, 0, 794, 0, 0));
76 writer.WritePlacedElement(element);
77
78 writer.End() # Save the page
79 doc.PagePushBack(page) # Add the page to the document page sequence
80
81 # ----------------------------------------------------------
82 # Add a JPEG2000 (JP2) image to the output file
83
84 # Create a new page
85 page = doc.PageCreate()
86 writer.Begin(page) # Begin writing to the page
87
88 # Embed the image
89 img = Image.Create(doc.GetSDFDoc(), input_path + "palm.jp2")
90
91 # Position the image on the page
92 element = f.CreateImage(img, Matrix2D.new(img.GetImageWidth(), 0, 0, img.GetImageHeight(), 96, 80))
93 writer.WritePlacedElement(element)
94
95 # Write 'JPEG2000 Sample' text string under the image
96 writer.WriteElement(f.CreateTextBegin(Font.Create(doc.GetSDFDoc(), Font::E_times_roman), 32))
97 element = f.CreateTextRun("JPEG2000 Sample")
98 element.SetTextMatrix(1, 0, 0, 1, 190, 30)
99 writer.WriteElement(element)
100 writer.WriteElement(f.CreateTextEnd())
101
102 writer.End() # Finish writing to the page
103 doc.PagePushBack(page)
104
105 doc.Save((output_path + "addimage.pdf"), SDFDoc::E_linearized);
106 doc.Close()
107 PDFNet.Terminate
108 puts "Done. Result saved in addimage.pdf..."

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales