Hi,
I have following piece of code in my existing application. data is byte
array, which is when written to OutputStream generate pdf file. I am
required to attached a document(pdf, doc etc) to the genrated pdf.
*Original Code*
*
*
*ByteArrayOutputStream byteOS = new ByteArrayOutputStream(); *
*BufferedOutputStream bufOS = new BufferedOutputStream( byteOS); *
*getContent(bufOS,planId, siteName, paramsMap,req); // Black Box. *
*bufOS.flush(); *
*byteOS.flush(); *
*data = byteOS.toByteArray(); //byte array which contains data to be
written to pdf.*
*res.getOutputStream().write(data);*
I used following approach using *itext code* to add attachment.
public class AddingAttachment {
public static void main(String[] args) throws IOException,
DocumentException {
String src = "HelloWorldForm.pdf";
String dest = "HelloWorldFormWithCertificateAttachment.pdf";
addAttachments(src,dest);
}
public static void addAttachments(
String src, String dest) throws IOException, DocumentException {
File temp = File.createTempFile("testing", ".txt");
BufferedWriter out = new BufferedWriter(new FileWriter(temp));
out.write("aString");
out.close();
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
addAttachment(stamper.getWriter(), temp);
stamper.close();
System.out.println("Ok");
}
protected static void addAttachment(PdfWriter writer, File src) throws
IOException {
PdfFileSpecification fs =
PdfFileSpecification.fileEmbedded(writer, src.getAbsolutePath(),
src.getName(), null);
writer.addFileAttachment(src.getName().substring(0,
src.getName().indexOf('.')), fs);
}
}
After merging the *original code* from and *itext code*, I got the
following code:
//Orininal byte array.
byte[] data = getAttachmentData(decryptedString, siteName,
paramsMap, req);
if (data != null) {
//Temporary attachment file.
File temp = File.createTempFile("testing", ".txt");
BufferedWriter out2 = new BufferedWriter(new FileWriter(temp));
out2.write("aString");
out2.close();
InputStream ins = new ByteArrayInputStream(data);
//The output file which will have the original data along with attachment.
*String dest = "d://java2.pdf";*
FileOutputStream fos = new FileOutputStream(dest);
PdfReader reader = new PdfReader(ins);
PdfStamper stamper = new PdfStamper(reader,
fos);
addAttachment(stamper.getWriter(), temp);
stamper.close();
//Reading back the file with attachment as byte array and flushing it to
output stream.
File file = new File("d://java2.pdf");
byte[] b = new byte[(int) file.length()];
FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(b);
res.getOutputStream().write(b);
}
//Code for adding attachment to pdf file.
protected static void addAttachment(PdfWriter writer, File src)
throws IOException {
PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(writer,
src.getAbsolutePath(), src.getName(), null);
writer.addFileAttachment(
src.getName().substring(0, src.getName().indexOf('.')), fs);
}
The above code generates a temporary file *java2.pdf*. Is there a way I can
handle this file in memory without having to do I/O operation.
--
Regards,
Naresh C
------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. SALE $99.99 this month only -- learn more at:
http://p.sf.net/sfu/learnmore_122412
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions
iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples:
http://itextpdf.com/themes/keywords.php