I have the pdf file location and pdf file in my POJO class. I want to download thee pdf using servlet. Please tell me some ways to get it done.
File Location=/tmp/SWBC_444Thu May 03 20:01:07 IST 20124366242221752147545.pdf
Using this file location i want to prompt user to download the file as pdf.
Here is my code.
File file = new File(filePath);
OutputStream responseOutputStream = response.getOutputStream();
response.setContentLength((int)filePath.length());
FileInputStream fileInputStream = new FileInputStream(file);
int size = fileInputStream.available();
byte[] content = new byte[size];
int bytesRead;
while ((bytesRead = fileInputStream.read(content)) != -1)
{
responseOutputStream.write(content, 0, bytesRead);
}
responseOutputStream.flush();
fileInputStream.close();
responseOutputStream.close();
. I read and generate the file but when open the file its empty.
Thanking you..!
httpservletresponse.setHeader("Content-disposition", "attachment; filename=\"" + title + ".pdf\""); should do
Related
I have this Code that adds image to a PDF:
string SRC =
#"C:/Saved/Test.pdf";
string DEST = #"C:/Saved/TestComplete.pdf";
string IMG = #"C:Saved//TestImage.JPG";
Document doc = new Document();
try
{
iTextSharp.text.pdf.PdfWriter.GetInstance(doc, new FileStream(SRC, FileMode.Create));
doc.Open();
//doc.Add(new Paragraph("GIF"));
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(IMG);
image.ScalePercent(200f);
doc.Add(image);
}
catch (Exception ex)
{
//Log error;
string error = ex.Message;
}
finally
{
doc.Close();
}
}
The Problem is here that its not just adding the image its replacing the whole PDF with that image is there a way to add a image in PDF just a signature image just add that to the page
Any Idea ?
Also I have upgraded the Itextsharp to IText7 but I couldn't find a way to add image to a existing PDF only there are water marks. If you know a example Link or article on it please let me know.
If you want to use the old method then use this:
string SRC =
#"C:/Saved/Test.pdf";
string DEST = #"C:/Saved/TestComplete.pdf";
string IMG = #"C:Saved//TestImage.JPG";
iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(SRC);
iTextSharp.text.Rectangle Size = reader.GetPageSizeWithRotation(1);
Document document = new Document(Size);
FileStream fs = new FileStream(DEST, FileMode.Create, FileAccess.Write);
iTextSharp.text.pdf.PdfWriter weiter = iTextSharp.text.pdf.PdfWriter.GetInstance(document, fs);
document.Open();
PdfContentByte cb = weiter.DirectContent;
PdfImportedPage page = weiter.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(IMG);
document.Add(jpg);
document.Close();
fs.Close();
weiter.Close();
reader.Close();
For your question to use the iText7 method plesae refer to this link IText7 JumpStart
and to be exactly where you can find an example of dealing with images Please refer to this Chapter7
I also recommend that you read all the chapters
I need to merge multiple pdf files into one pdf and display it in my web browser.
I know how to display one file :
File file = new File(activite.getLienUploadUn());
FileInputStream inputStream = new FileInputStream(file);
byte[] buffer = new byte[8192];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1)
{
baos.write(buffer, 0, bytesRead);
}
response.setHeader("Content-Disposition","inline; filename=\""+file.getName()+"\"");
response.setContentType("application/pdf");
ServletOutputStream outputStream = response.getOutputStream();
baos.writeTo(outputStream);
outputStream.flush();
I think I am going to use PdfBox and its PDFMergerUtility class to merge files :
PDFMergerUtility mergePdf = new PDFMergerUtility();
mergePdf.addSource(file);
mergePdf.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
But from there how can I convert the merged document into a byteArrayOutputStream?
You can call PDFMergerUtility.setDestinationStream(OutputStream destStream) to pass an output stream (javadoc).
I have doc or docx document saved in Unix directory and integrate with web page which allow user to download the attachment. I have following code to stream the character and saves as Word document with correct MIME type but why when open it shows garbage character. It is relate to character encoding problem. How to solve this? Should I use docx4j?
String fullfilename = filename;
File f = new File(fullfilename);
int length = 0;
ServletOutputStream op = response.getOutputStream();
ServletContext context = getContext();
String mimetype = context.getMimeType(fullfilename);
response.setContentType((mimetype != null) ? mimetype
: "application/x-download");
response.setContentLength((int) f.length());
response.setHeader("Content-Disposition", "attachment;filename="
+ filename);
byte[] bbuf = new byte[fullfilename.length()];
DataInputStream in = new DataInputStream(new FileInputStream(f));
while ((in != null) && ((length = in.read(bbuf)) != -1)) {
op.write(bbuf, 0, length);
}
in.close();
op.flush();
op.close();
Please help. Thanks.
Thread closed after setting the correct mime type.
This is my code and I can't seem to get the file I have in my FileUploadCotrol into the FILESTREAM.
// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
FileStream fs = fileInf.OpenRead();
try
{
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();
// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);
// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream
strm.Close();
fs.Close();
}
It seems the I should be using the Fileupload control to do the from my website, yet it seams strange that the control creates a stream and not a filestream. Yes I am FTPing a file.
Here is a sample method that takes the two types we are targeting, FileInfo and FtpWebRequest, as arguments and streams data between them. I believe this will work.
void UploadFileToFtp(FileInfo file, FtpWebRequest req)
{
int buffLength = 2048;
using (var reader = new BinaryReader(file.OpenRead()))
{
using (var writer = new BinaryWriter(req.GetRequestStream()))
{
while (reader.PeekChar() > 0) writer.Write(reader.ReadBytes(buffLength));
}
}
}
Hope this helps!
I like to upload an file in my project. when I click the upload button the file should be stored in client system and the file name and path should be stored in the database. When I clicking the download button it should be downloaded based on the file name and path that I have stored in the database. After making the changes it should be uploaded as different file name and it will not affect the previous file content. If there is any code for this process please send it to me.
Thanks in advance
To upload a file you use the input type file and then process this accordingly on the server. Here is a complete tutorial on CodePlex that goes through exactly what you are looking for.
Warning don't use their code in production. Just noticed a couple of security risks, but anyways, use this to understand the process then figure out how to avoid sql-injections and possible overflows.
Here is another great article over at MSDN that covers File Uploading in ASP.NET 2.0.
string FolderPath = "yourpath";
string FileName = "Namefile";
string FilePath = Server.MapPath("~/" + FileName);
string Extension = Path.GetExtension(FileName);
Response.ContentType = "Application/x-msexcel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName + "");
// Write the file to the Response
const int bufferLength = 10000;
byte[] buffer = new Byte[bufferLength];
int length = 0;
Stream download = null;
try {
download = new FileStream(Server.MapPath("~/" + FileName),
FileMode.Open,
FileAccess.Read);
do {
if (Response.IsClientConnected) {
length = download.Read(buffer, 0, bufferLength);
Response.OutputStream.Write(buffer, 0, length);
buffer = new Byte[bufferLength];
}
else {
length = -1;
}
}
while (length > 0);
Response.Flush();
Response.End();
}
finally {
if (download != null)
download.Close();
}