• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

workign with exisitng pdfs

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying this Itext libraries.Any reply will be appreciated.
Thanks in advance
 
Saloon Keeper
Posts: 5802
146
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you saving the ByteArrayOutputStream?
 
Suguna Devarla
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 5802
146
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i do that.
I did it using servlet using response object.
ServletOutputStream out = response. getOutputStream();
baos.writeTo(out);
 
Suguna Devarla
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!