wood burning stoves 2.0*
The moose likes Other Open Source Projects and the fly likes workign with exisitng pdfs Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "workign with exisitng pdfs" Watch "workign with exisitng pdfs" New topic
Author

workign with exisitng pdfs

Suguna Devarla
Greenhorn

Joined: Jan 11, 2008
Posts: 11
Hi,

I am adding a text to the existing pdf and writing to bytearray output stream but when I open the "HelloWorld2.pdf", text is not showing up? But if i create a new pdf for e.g. HelloWorldNew.pdf then the text is shwoing up.

Can any one help me to find out the error reg bytearray outputstream.?

PdfReader pdfReader = new PdfReader(
"C:\\POC\\HelloWorld2.pdf");
ByteArrayOutputStream bao = new ByteArrayOutputStream();
PdfStamper pdfStamper = new PdfStamper(pdfReader, bao);

Calendar currentDate = Calendar.getInstance();
SimpleDateFormat f = new SimpleDateFormat("MM/dd/yyyy");
String signatureDate = f.format(currentDate.getTime());

ct.setAlignment(Element.ALIGN_CENTER);
ct.setSimpleColumn(50, 500, 500, 50);
ct.addText(new Chunk("Current Date : " + signatureDate));
ct.go();

Thanks,
Suguna
Suguna Devarla
Greenhorn

Joined: Jan 11, 2008
Posts: 11
I am trying this Itext libraries.Any reply will be appreciated.
Thanks in advance
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
How are you saving the ByteArrayOutputStream?
Suguna Devarla
Greenhorn

Joined: Jan 11, 2008
Posts: 11
I am not saving boa object , I am thinking PdfStamper pdfStamper = new PdfStamper(pdfReader,bao) will automatically takes care of that.
Can you tell me wts the code I need to add in order to get the text in same Pdf instaed of careting new one.

Thanks
Suguna Devarla
Greenhorn

Joined: Jan 11, 2008
Posts: 11
I have added below code but still no luck..Can any one explain how to add content to exisitng pdf with out creating a new different physical file
I wanted to see the new content in same "HelloWorld2.pdf"

PdfReader pdfReader = new PdfReader(
"C:\\POC\\HelloWorld2.pdf");
ByteArrayOutputStream bao = new ByteArrayOutputStream();
PdfStamper pdfStamper = new PdfStamper(pdfReader, bao);

Calendar currentDate = Calendar.getInstance();
SimpleDateFormat f = new SimpleDateFormat("MM/dd/yyyy");
String signatureDate = f.format(currentDate.getTime());

ct.setAlignment(Element.ALIGN_CENTER);
ct.setSimpleColumn(50, 500, 500, 50);
ct.addText(new Chunk("Current Date : " + signatureDate));
ct.go();

PdfCopy copy = new PdfCopy(doc, bao);
copy.addPage(copy.getImportedPage(pdfReader, 1));


Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
I am not saving boa object , I am thinking PdfStamper pdfStamper = new PdfStamper(pdfReader,bao) will automatically takes care of that.

No. That just saves the altered PDF in the BAOS. You still need to write the contents of the BAOS to a file.
Suguna Devarla
Greenhorn

Joined: Jan 11, 2008
Posts: 11
how do i do that.
I did it using servlet using response object.
ServletOutputStream out = response. getOutputStream();
baos.writeTo(out);
Suguna Devarla
Greenhorn

Joined: Jan 11, 2008
Posts: 11
I tried writing byte array output stream to a file after saving it throws me ane error.Below is the line of code added to the program.

bao.writeTo(new FileOutputStream("C:\\Suguna\\POC\\HelloWorld2.pdf"));

Here is the exception.. any reply would be a big help.
java.io.IOException: PDF header signature not found.
at com.lowagie.text.pdf.PRTokeniser.checkPdfHeader(PRTokeniser.java:204)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: workign with exisitng pdfs
 
Similar Threads
opening PDF from JSP
iText - remove previously inserted over content text - Help Needed
recieving this exception for iText
missing line space in Text field while merging multiple PDF's using PDFCopy of IText
how to change existing pdf font encoding (multi language) dynamically