Question

How to add graphic to pdf file

Asked by: jmarkfoley

I need to be able to add a graphic (timestamp) to the first page of a pdf document using a jsp program. I'm using linux 2.6.27.7-smp, apache 2.2.11, tomcat 6.0.18 and I have PDFBox 0.7.3 if that would be useful. At this point, I don't care what kind of graphic, but jpg would probably be best.

Someone already posted a Perl solution, but I don't think that helps me:
http://www.experts-exchange.com/Programming/Languages/Scripting/Perl/Q_21255104.html?sfQueryTermInfo=1+10+add+graphic+pdf

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-04-07 at 11:22:57ID24303092
Tags

pdf jsp graphic

Topics

Document Imaging

,

Java Server Pages (JSP)

Participating Experts
1
Points
500
Comments
10

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. PDF to graphic format
    Hi Experts, Using Delphi 6, is there any component or anything that will allow me to load a PDF file and then by pressing a button, I'll be able to save/capture this as GIF or JPG or BMP just in any other graphic formats? Can someone help? jrmn
  2. Problem in Apache Tomcat 5.0.24
    The problem i encounter in Apache Tomcat 5.0.24 is: I was added a folder(Test) at C:\Tomcat 5.0\webapps but it's unable to view at internet explorer. The error is The requested resource (/Test) is not available.

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: KuldeepchaturvediPosted on 2009-04-07 at 22:17:31ID: 24094175

this can be as easy as using the command line options of your PDFbox utility

check following link

http://www.pdfbox.org/javadoc/org/pdfbox/examples/pdmodel/AddImageToPDF.html

 

by: jmarkfoleyPosted on 2009-05-13 at 10:24:22ID: 24377379

I'm back, sorry for the delay. Thanks for the link. I'm very close. I believe I pretty much followed the example in the link. My code is shown below. It creates the pdf file with the correct number. However, when I go to open it, I get an Adobe reader error: "A drawing error occured". The pages are not displayed. No further info. Can anyone tell me what's wrong?

String inputFile = "/www/tomcat/webapps/courtscan/OH/demo/documents/2009/1-60.pdf";
String outputFile = "/usr/local/apache/htdocs/new.pdf";
 
PDDocument doc = PDDocument.load( inputFile );
PDPage myPage = (PDPage)doc.getDocumentCatalog().getAllPages().get( 0 ); // 1st page
String imageFile = "/www/tomcat/webapps/courtscan/CourtScanLogo.jpg"; // 197x68 jpg
PDJpeg ximage = new PDJpeg(doc, new FileInputStream( imageFile ) );
PDPageContentStream contentStream = new PDPageContentStream(doc, myPage, true, true$
contentStream.drawImage( ximage, 20, 20 );
contentStream.close();
doc.save( outputFile );
doc.close();
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:

Select allOpen in new window

 

by: jmarkfoleyPosted on 2009-05-13 at 14:05:19ID: 24379678

In fact, my ultimate goal is to simply add a text string (timestamp) to the pdf document. My initial thinking was that I'd have to create a gif or pdf of the string first, then add it to the document, but if there is a way to simply add a text string, that would be cool.

 

by: jmarkfoleyPosted on 2009-05-13 at 14:42:32ID: 24379984

In fact, when I remove all the graphic related code and simply have what's shown in the snippet below, I get the same "A drawing error occured" message. I would have expected it to simple create a copy of the original. So, I must have something more fundamental wrong.

String inputFile = "/www/tomcat/webapps/courtscan/OH/demo/documents/2009/1-60.pdf";
String outputFile = "/usr/local/apache/htdocs/new.pdf";
 
PDDocument doc = PDDocument.load( inputFile );
out.print(doc.getNumberOfPages() );
doc.save( outputFile );
doc.close();

                                              
1:
2:
3:
4:
5:
6:
7:

Select allOpen in new window

 

by: KuldeepchaturvediPosted on 2009-05-13 at 21:20:09ID: 24381704

that is a strange behavior ? can you open the input file without any problems??

 

by: jmarkfoleyPosted on 2009-05-14 at 02:41:26ID: 24383056

I seem to open the input OK, the out.print(doc.getNumberOfPages()); give me "3" which is the number of pages in the original pdf. It does create the output file with a size of 53122 which is a bit smaller than the original: 54353. In fact, when I do the above getNumberOfPages() on the new.pdf it also give me 3. My adobe won't read it though. You could opening it at http://www.fluxrunner.com/new.pdf. I've put a hex dump of the first 255 bytes of original and new below. The new one contains what looks like OK data.

Am I missing some kind of attribute setting?

root@webhost1:/usr/local/apache/htdocs# fd -p new.pdf
     0: 25 50 44 46 2D 31 2E 35 0A 25 F6 E4 FC DF 0A 31    %PDF-1.5.%.....1
    10: 20 30 20 6F 62 6A 0A 3C 3C 0A 2F 4C 61 6E 67 20     0 obj.<<./Lang
    20: 28 78 2D 64 65 66 61 75 6C 74 29 0A 2F 50 61 67    (x-default)./Pag
    30: 65 73 20 32 20 30 20 52 0A 2F 54 79 70 65 20 2F    es 2 0 R./Type /
    40: 43 61 74 61 6C 6F 67 0A 3E 3E 0A 65 6E 64 6F 62    Catalog.>>.endob
    50: 6A 0A 33 20 30 20 6F 62 6A 0A 3C 3C 0A 2F 43 72    j.3 0 obj.<<./Cr
    60: 65 61 74 69 6F 6E 44 61 74 65 20 28 44 3A 32 30    eationDate (D:20
    70: 30 37 30 35 31 31 31 38 32 36 31 39 2B 30 30 27    070511182619+00'
    80: 30 30 27 29 0A 2F 43 72 65 61 74 6F 72 20 28 50    00')./Creator (P
    90: 61 70 65 72 50 6F 72 74 20 31 31 2E 30 29 0A 2F    aperPort 11.0)./
    A0: 4D 6F 64 44 61 74 65 20 28 44 3A 32 30 30 37 30    ModDate (D:20070
    B0: 35 31 31 31 38 32 36 31 39 2B 30 30 27 30 30 27    511182619+00'00'
    C0: 29 0A 2F 50 72 6F 64 75 63 65 72 20 28 50 61 70    )./Producer (Pap
    D0: 65 72 50 6F 72 74 20 31 31 2E 30 29 0A 3E 3E 0A    erPort 11.0).>>.
    E0: 65 6E 64 6F 62 6A 0A 32 20 30 20 6F 62 6A 0A 3C    endobj.2 0 obj.<
    F0: 3C 0A 2F 43 6F 75 6E 74 20 33 0A 2F 4B 69 64 73    <./Count 3./Kids
? q
root@webhost1:/usr/local/apache/htdocs# fd -p old.pdf
     0: 25 50 44 46 2D 31 2E 35 0D 0A 25 F1 F9 F7 F6 33    %PDF-1.5..%....3
    10: 2E 33 0D 0A 34 20 30 20 6F 62 6A 0D 0A 3C 3C 0D    .3..4 0 obj..<<.
    20: 0A 2F 42 69 74 73 50 65 72 43 6F 6D 70 6F 6E 65    ./BitsPerCompone
    30: 6E 74 20 31 20 0D 0A 2F 43 6F 6C 6F 72 53 70 61    nt 1 ../ColorSpa
    40: 63 65 20 2F 44 65 76 69 63 65 47 72 61 79 20 0D    ce /DeviceGray .
    50: 0A 2F 46 69 6C 74 65 72 20 2F 4A 42 49 47 32 44    ./Filter /JBIG2D
    60: 65 63 6F 64 65 20 0D 0A 2F 48 65 69 67 68 74 20    ecode ../Height
    70: 32 31 39 39 20 0D 0A 2F 4C 65 6E 67 74 68 20 35    2199 ../Length 5
    80: 20 30 20 52 20 0D 0A 2F 4E 61 6D 65 20 2F 69 6D     0 R ../Name /im
    90: 61 67 65 30 20 0D 0A 2F 53 75 62 74 79 70 65 20    age0 ../Subtype
    A0: 2F 49 6D 61 67 65 20 0D 0A 2F 54 79 70 65 20 2F    /Image ../Type /
    B0: 58 4F 62 6A 65 63 74 20 0D 0A 2F 57 69 64 74 68    XObject ../Width
    C0: 20 31 37 30 30 20 0D 0A 3E 3E 0D 0A 73 74 72 65     1700 ..>>..stre
    D0: 61 6D 0D 0A 00 00 00 00 30 00 01 00 00 00 13 00    am......0.......
    E0: 00 06 A4 00 00 08 97 00 00 00 C8 00 00 00 C8 01    ................
    F0: 00 00 00 00 00 01 00 01 01 00 00 37 AD 08 00 02    ...........7....
?

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:

Select allOpen in new window

 

by: KuldeepchaturvediPosted on 2009-05-14 at 03:53:53ID: 24383451

in your old and new PDF header looks like the drawing info is missing ( like device and color scheme etc..).. I am not sure why it would do so...
I will keep on looking if I find something..

 

by: jmarkfoleyPosted on 2009-05-14 at 04:21:59ID: 24383618

Well, as you can see all I did was PDDocument.load then PDDocument.save. I would think it would save a copy as-is. Maybe there is some "preserve drawing info" setting not on by default?

 

by: KuldeepchaturvediPosted on 2009-05-15 at 07:58:11ID: 24396370

no luck yet.. & I cant seem to find too many details around PDFBox either.. looks like the last release was in 2006... so it may be only compatible with PDF 5 or something like that...


Any chance you can use another PDF api's like iText?

 

by: jmarkfoleyPosted on 2009-05-17 at 14:35:38ID: 24407867

Yup, that's exactly what I did! iText did the trick. Below is some test code I used. This code gets the ascent and descent values in order to create a crude calculation of line height (if there is such a method, I haven't found it yet). Ti changes the font color (not a trivial research exercise) and adds a couple of line at the top of the 1st pdf page centered on x coord 400. The final app is a bit more elegant that this, but this is the idea. It accomplishes what I want: add text (a timestamp in the app) to the 1st page of a pdf file.

Thanks for hanging in there with me!

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<%@ page contentType="text/html; charset=ISO-8859-1" %>
<html>
<body>
<%@ page import="java.lang.String,java.io.*,java.util.*" %>
<%@ page import="java.lang.Object,
  com.lowagie.text.pdf.PdfReader,
  com.lowagie.text.Rectangle,
  com.lowagie.text.pdf.PdfStamper,
  com.lowagie.text.Font,
  com.lowagie.text.pdf.BaseFont,
  com.lowagie.text.pdf.PdfContentByte,
  com.lowagie.text.Element" %>
<%
 
String inputFile = "/www/tomcat/webapps/courtscan/OH/demo/documents/2009/2-20.pdf";
String outputFile = "/usr/local/apache/htdocs/new.pdf";
 
PdfReader doc = new PdfReader(inputFile);
out.println("p " + doc.getNumberOfPages() );
Float docHeight = doc.getPageSizeWithRotation(1).getHeight();
Float docWidth = doc.getPageSizeWithRotation(1).getWidth();
out.println("<br>Height: " + docHeight + ", width: " + docWidth);
 
PdfStamper stamp = new PdfStamper(doc,new FileOutputStream(outputFile));
 
BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.EMBEDDED);
float fontSize = 14;
 
out.println("<br> font height: " + (bf.getAscentPoint("T",fontSize) - bf.getDescentPoint("j",fontSize)));
float lineHeight = bf.getAscentPoint("T",fontSize) - bf.getDescentPoint("j",FontSize);
lineHeight += .25 * lineHeight;
 
PdfContentByte over = stamp.getOverContent(1);  // first page
over.beginText();
over.setRGBColorFill(0,0,255);
over.setFontAndSize(bf, fontSize);
float x = 400;
float y = docHeight - 60;
Float strWidth = over.getEffectiveStringWidth("FILED", false);
out.println("<br>String Width: " + strWidth);
over.showTextAligned(Element.ALIGN_LEFT, "FILED", x - (strWidth / 2), y, 0); // x,y,rot
strWidth = over.getEffectiveStringWidth("GimbleCounty", false);
out.println("<br>String Width: " + strWidth);
over.showTextAligned(Element.ALIGN_LEFT, "Gimble County", x - (strWidth /2), y - lineHeight, 0); // x,y,rot
over.endText();
 
stamp.close();
doc.close();
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:

Select allOpen in new window

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...