How to add graphic to pdf file

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
LVL 1
jmarkfoleyAsked:
Who is Participating?
 
KuldeepchaturvediConnect With a Mentor Commented:
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?
0
 
KuldeepchaturvediCommented:
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
0
 
jmarkfoleyAuthor Commented:
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();

Open in new window

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
jmarkfoleyAuthor Commented:
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.
0
 
jmarkfoleyAuthor Commented:
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();

Open in new window

0
 
KuldeepchaturvediCommented:
that is a strange behavior ? can you open the input file without any problems??
0
 
jmarkfoleyAuthor Commented:
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....
?

Open in new window

0
 
KuldeepchaturvediCommented:
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..
0
 
jmarkfoleyAuthor Commented:
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?
0
 
jmarkfoleyAuthor Commented:
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();

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.