File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

workign with exisitng pdfs

 
Suguna Devarla
Greenhorn
Posts: 11
  • 0
  • 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
  • 0
  • 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
 
Tim Moores
Bartender
Pie
Posts: 2488
4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you saving the ByteArrayOutputStream?
 
Suguna Devarla
Greenhorn
Posts: 11
  • 0
  • 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
  • 0
  • 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
Bartender
Pie
Posts: 2488
4
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic