PDFlib 8 Windows COM/.NET Tutorial
PDFlib 8 Windows COM/.NET Tutorial
PDFlib 8 Windows COM/.NET Tutorial
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
7 Importing Images and PDF Pages<br />
<strong>PDFlib</strong> offers a variety of features for importing raster images and pages from existing<br />
PDF documents, and placing them on the page. This chapter covers the details of dealing<br />
with raster images and importing pages from existing PDF documents. Placing images<br />
and PDF pages on an output page is discussed in Section 7.3, »Placing Images and<br />
imported PDF Pages«, page 192.<br />
Cookbook Code samples regarding image issues can be found in the images category of the <strong>PDFlib</strong> Cookbook.<br />
7.1 Importing Raster Images<br />
7.1.1 Basic Image Handling<br />
Embedding raster images with <strong>PDFlib</strong> is easy to accomplish. First, the image file has to<br />
be opened with a <strong>PDFlib</strong> function which does a brief analysis of the image parameters.<br />
The load_image( ) function returns a handle which serves as an image descriptor. This<br />
handle can be used in a call to fit_image( ), along with positioning and scaling parameters:<br />
image = p.load_image("auto", "image.jpg", "");<br />
if (image == -1)<br />
throw new Exception("Error: " + p.get_errmsg());<br />
p.fit_image(image, 0.0, 0.0, "");<br />
p.close_image(image);<br />
The last parameter of fit_image( ) function is an option list which supports a variety of<br />
options for positioning, scaling, and rotating the image. Details regarding these options<br />
are discussed in Section 7.3, »Placing Images and imported PDF Pages«, page 192.<br />
Cookbook A full code sample can be found in the Cookbook topic images/starter_image.<br />
Re-using image data. <strong>PDFlib</strong> supports an important PDF optimization technique for<br />
using repeated raster images. Consider a layout with a constant logo or background on<br />
multiple pages. In this situation it is possible to include the actual image data only once<br />
in the PDF, and generate only a reference on each of the pages where the image is used.<br />
Simply load the image file once, and call fit_image( ) every time you want to place the<br />
logo or background on a particular page. You can place the image on multiple pages, or<br />
use different scaling factors for different occurrences of the same image (as long as the<br />
image hasn’t been closed). Depending on the image’s size and the number of occurrences,<br />
this technique can result in enormous space savings.<br />
Scaling and dpi calculations. <strong>PDFlib</strong> never changes the number of pixels in an imported<br />
image. Scaling either blows up or shrinks image pixels, but doesn’t do any downsampling<br />
(the number of pixels in an image will always remain the same). A scaling factor of<br />
1 results in a pixel size of 1 unit in user coordinates. In other words, the image will be imported<br />
with its native resolution (or 72 dpi if it doesn’t contain any resolution informa-<br />
7.1 Importing Raster Images 179