<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[itext - JBay Solutions - The Dev Blog]]></title><description><![CDATA[JBay Solutions Development Blog on Java, Android, Play2 and others]]></description><link>http://blog.jbaysolutions.com/</link><generator>Ghost 0.7</generator><lastBuildDate>Wed, 16 Oct 2024 01:15:05 GMT</lastBuildDate><atom:link href="http://blog.jbaysolutions.com/tag/itext/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Adding Image to PDF file using Java and IText]]></title><description><![CDATA[<p>To add an image to an already existing PDF file is a relatively simple task, using <a href="http://itextpdf.com/" title="iText">iText</a>.</p>

<p>On this example, we will add a smile image to the bottom of the PDF first page.</p>

<p>We start by loading the PDF we want to modify, and getting the reference for the</p>]]></description><link>http://blog.jbaysolutions.com/2015/10/27/adding-image-to-pdf-java-itext/</link><guid isPermaLink="false">b440d4a5-a073-4323-9f91-3dee253fe90e</guid><category><![CDATA[java]]></category><category><![CDATA[itext]]></category><category><![CDATA[pdf]]></category><dc:creator><![CDATA[Gustavo Santos]]></dc:creator><pubDate>Tue, 27 Oct 2015 15:49:00 GMT</pubDate><content:encoded><![CDATA[<p>To add an image to an already existing PDF file is a relatively simple task, using <a href="http://itextpdf.com/" title="iText">iText</a>.</p>

<p>On this example, we will add a smile image to the bottom of the PDF first page.</p>

<p>We start by loading the PDF we want to modify, and getting the reference for the first page:</p>

<pre><code class="language-java">    PdfReader reader = new PdfReader(srcPdf);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destPdf));
    PdfContentByte content = stamper.getOverContent(1);
</code></pre>

<p>srcPdf is a String with the full path to the existing PDF file, while destPdf is the full path where the modified PDF file will be created.  </p>

<p>Then, we load the image (imagePath is the full path for the imagem file):</p>

<pre><code>Image image = Image.getInstance(imagePath);

// scale the image to 50px height
image.scaleAbsoluteHeight(50);
image.scaleAbsoluteWidth((image.getWidth() * 50) / image.getHeight());
</code></pre>

<p>Since the image dimensions are large, it's scaled to 50 pixels height before we add it to the PDF.</p>

<p>We then set the page coordinates where we want it. <strong>Be aware that the 0 value for the Y axis is the bottom of the page, not the top:</strong></p>

<pre><code>image.setAbsolutePosition(70, 140);
</code></pre>

<p>All we need to do now is add it to the page reference and close the stamper:</p>

<pre><code>content.addImage(image);

stamper.close();
</code></pre>

<p>That's it! </p>

<p>The sample project with all the code, example PDF and image is available at github, <a href="https://github.com/jbaysolutions/add-image-to-pdf">here</a> </p>

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>  
<!-- Horizontal For Posts - Text Only -->  
<ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1311169549359552" data-ad-slot="3316155422"></ins>
<script>  
(adsbygoogle = window.adsbygoogle || []).push({});
</script>  

<p><br></p>]]></content:encoded></item></channel></rss>