Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to add graphic to pdf file

Posted on 2009-04-07
10
Medium Priority
?
873 Views
Last Modified: 2012-05-06
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
0
Comment
Question by:jmarkfoley
  • 6
  • 4
10 Comments
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 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
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 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();

Open in new window

0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 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.
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 1

Author Comment

by:jmarkfoley
ID: 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();

Open in new window

0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 24381704
that is a strange behavior ? can you open the input file without any problems??
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 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....
?

Open in new window

0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 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..
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 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?
0
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 2000 total points
ID: 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?
0
 
LVL 1

Author Comment

by:jmarkfoley
ID: 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();

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update 21-May-2015: I temporarily removed the source code to make major changes to the program. Regards, Joe INTRODUCTION This article presents a solution to a question (http://www.experts-exchange.com/Programming/Installation/Q_28396542.html)…
This article explains how to perform batch conversion of PDF, TIFF, and other image file formats into PDF, PDF Searchable, and TIFF files via a command line interface, using Nuance's latest document imaging software — Power PDF Advanced.
In this second video of the Xpdf series, we discuss and demonstrate the PDFimages utility, which, in a single command, is able to extract all the images from a PDF file and save each one in a separate image file (PBM, PPM, or JPG). Download and inst…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question