I have a HttpModule that intercepts requests for PDF documents and I would like to add a date to the PDF and stream back to the client.
My code so far is
context.Response.ClearContent();
using (MemoryStream ms = new MemoryStream())
{
PdfReader reader = new PdfReader(document.Url + "&a=1");
PdfStamper stamper = new PdfStamper(reader, ms);
// *** Modify PDF here
stamper.Close();
context.Response.ContentType = "application/pdf";
context.Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
context.Response.OutputStream.Flush();
context.Response.OutputStream.Close();
context.Response.Flush();
context.Response.End();
}
HttpContext.Current.ApplicationInstance.CompleteRequest();
The code above works fine, but as soon as I try to modify the PDF I get a PDF Reader error 'The file is damaged and cannot be repaired', for example
TextField textField = new TextField(stamper.Writer, new Rectangle(0, 1000, 90, 600), name);
textField.Font = FontFactory.GetFont(FontFactory.HELVETICA, DEFAULT_FONT_SIZE, Font.NORMAL).BaseFont;
textField.FontSize = DEFAULT_FONT_SIZE;
textField.Rotation = 90;
PdfFormField field = textField.GetTextField();
stamper.AddAnnotation(field, page);
Does anyone know how I can solve this issue?
You continue sending stuff after the pdf, add
context.Response.End();
after:
context.Response.Flush();
Now you will send only the pdf, instead of the whole page. This sometimes fixes this issue.
You are also reading the buffer twice:
context.Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
try to add
byte[] bytes = ms.ToArray();
and then
context.Response.OutputStream.BinaryWrite(bytes);
Related
I have generated a pdf using PDFSharp.
I call the save method, and save it to disk, and the file is perfect.
I then need to get the file into a MemorySteam, in preparion of sending it to my website to download. However, the file ends up invalid. corrupt.
So, to see where it's going wrong, I have put the file into a MemoryStream, and then I try write that steam to a file, to confirm all is OK. It isn't.
Here I sav the file to disk, to check it (debugging), and then put it into a stream:
document.Save("c:\\temp\\ggg.pdf");
MemoryStream ms = new MemoryStream();
document.Save(ms, false);
byte[] buffer = new byte[ms.Length];
ms.Seek(0, SeekOrigin.Begin);
ms.Flush();
ms.Read(buffer, 0, (int)ms.Length);
return ms;
I then return 'ms' to my calling function, and attempt to write the stream to a file:
var doc = GeneratePdf(1);
using (FileStream file = new FileStream("c:\\temp\\222.pdf", FileMode.Create, System.IO.FileAccess.Write))
{
byte[] bytes = new byte[doc.Length];
doc.Read(bytes, 0, (int)doc.Length);
file.Write(bytes, 0, bytes.Length);
doc.Close();
}
But 222.pdf is not a valid pdf. ggg.pdf was fine. So I am doing something wrong when I write to stream, and write to disk. Why is the file getting corrupted?
I cannot reproduce your issue (PdfSharp 1.32.3057.0). It seems to me that you are messing too much with manual stream copying.
Try the following code, which correctly creates a pdf, streams it into a MemoryStream and than saves it into a file:
var pdf = new PdfSharp.Pdf.PdfDocument();
var page = pdf.AddPage();
var gfx = XGraphics.FromPdfPage(page);
var font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
gfx.DrawString("Hello, World!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
var ms = new MemoryStream();
pdf.Save(ms, false);
ms.Position = 0;
using (var file = File.OpenWrite("test.pdf"))
ms.CopyTo(file); // no need for manual stream copy or buffers
I have this simple piece of code which I took from another question here with the same topic, corrupted PDF files.
I've tried to implement the described solution, along with other references, but have met no sucess yet.
Here are the two functions generating my example PDF:
private void ShowPdf (byte[] str)
{
var Response = HttpContext.Current.Response;
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now);
Response.BinaryWrite(str);
Response.End();
Response.Flush();
Response.Clear();
}
private byte[] CreatePDF2()
{
Document doc = new Document(PageSize.LETTER, 50, 50, 50, 50);
using (MemoryStream output = new MemoryStream())
{
PdfWriter wri = PdfWriter.GetInstance(doc, output);
doc.Open();
Paragraph header = new Paragraph("Test bug") { Alignment = Element.ALIGN_CENTER };
Paragraph paragraph = new Paragraph("test.");
Phrase phrase = new Phrase("testnewline. \nnewline hapenned.");
Chunk chunk = new Chunk("Chucnk cauncuanocnaacoocsinasiocniocsanacsoi chunk.");
doc.Add(header);
doc.Add(paragraph);
doc.Add(phrase);
doc.Add(chunk);
doc.Close();
return output.ToArray();
}
}
Then at an request, I just consume it as in:
ShowPdf(CreatePDF2());
The problem is that this generates a file called "Response.pdf.pdf", which is corrupt and can't be opened.
How can I solve this problem?
Obs.: I am currently using iTextSharp 4.1.6
Try this for output variable,
FileStream output = new FileStream(Server.MapPath("MyFirstPDF.pdf"), FileMode.Create);
I am using following code:
PdfReader PDFReader = new PdfReader("C:\\file.pdf");
FileStream Stream = new FileStream("C:\\new.pdf", FileMode.Create, FileAccess.Write);
PdfStamper PDFStamper = new PdfStamper(PDFReader, Stream);
for (int iCount = 0; iCount < PDFStamper.Reader.NumberOfPages; iCount++)
{
PdfContentByte PDFData = PDFStamper.GetOverContent(iCount + 1);
BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
PDFData.BeginText();
PDFData.SetColorFill(CMYKColor.LIGHT_GRAY);
PDFData.SetFontAndSize(baseFont, 80);
PDFData.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "SAMPLE DOCUMENT", 300, 400, 45);
PDFData.EndText();
}
Stream.Close();
PDFReader.Close();
PDFStamper.Close();
But PDFStamper.Close(); throws error. Also, I am not sure whether to call PDFReader.Close(); before or after PDFStamper.Close();
And watermark is not added in PDF file.
Your order of Close calls is all wrong:
Stream.Close();
PDFReader.Close();
PDFStamper.Close();
In particular the PDFStamper requires both the PDFReader and the Stream to still be open when it is getting closed.
Furthermore, unless an exception is thrown, the Stream automatically is closed during PDFStamper.Close().
Thus, use
PDFStamper.Close();
PDFReader.Close();
instead.
If you want to make sure that the Stream is getting closed in case of an exception, use a using statement.
I've a handler in asp.net app. When there comes some kind of request I want user to get download window, where I put PDF stream. I don't want PDF to be saved in memory.
I have the following code, but it fails to open, says pdf file was demaged and couldn't open it.
tell me other way to achieve my goal if you think mine is not good.
MemoryStream stream = new MemoryStream();
CreatePFD(T.Text, stream);
context.Response.Write(stream);
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("content-disposition", "attachment; filename=" + "Nots__Saved__FIle__fileName.pdf");
Now I have this code:
Document Doc = new Document(PageSize.A4, 80, 50, 30, 65);
System.IO.Stream outputStream = new System.IO.FileStream(#"C:\inetpub\wwwroot\c\wefwe.pdf", System.IO.FileMode.OpenOrCreate);
PdfWriter.GetInstance(Doc, outputStream);
Doc.Open();
List<IElement> htmlarraylist = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StringReader(T.Text), null);
for (int k = 0; k < htmlarraylist.Count; k++)
{
Doc.Add((IElement)htmlarraylist[k]);
}
outputStream.CopyTo(context.Response.OutputStream);
Doc.Close();
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("content-disposition", "attachment; filename=" + "Nots__Saved__FIle__fileName.pdf");
when i save pdf, the file is correct...
but when I get response the file is in correct. demaged...
You need to rewind the stream by setting stream.Position = 0.
Also, Response.Write(stream) will just print stream.ToString().
Instead, you should call stream.CopyTo(Response.OutputStream).
I have this demo code for iTextSharp
Document document = new Document();
try
{
PdfWriter.GetInstance(document, new FileStream("Chap0101.pdf", FileMode.Create));
document.Open();
document.Add(new Paragraph("Hello World"));
}
catch (DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch (IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
document.Close();
How do I get the controller to return the pdf document to the browser?
EDIT:
Running this code does open Acrobat but I get an error message "The file is damaged and could not be repaired"
public FileStreamResult pdf()
{
MemoryStream m = new MemoryStream();
Document document = new Document();
PdfWriter.GetInstance(document, m);
document.Open();
document.Add(new Paragraph("Hello World"));
document.Add(new Paragraph(DateTime.Now.ToString()));
m.Position = 0;
return File(m, "application/pdf");
}
Any ideas why this does not work?
Return a FileContentResult. The last line in your controller action would be something like:
return File("Chap0101.pdf", "application/pdf");
If you are generating this PDF dynamically, it may be better to use a MemoryStream, and create the document in memory instead of saving to file. The code would be something like:
Document document = new Document();
MemoryStream stream = new MemoryStream();
try
{
PdfWriter pdfWriter = PdfWriter.GetInstance(document, stream);
pdfWriter.CloseStream = false;
document.Open();
document.Add(new Paragraph("Hello World"));
}
catch (DocumentException de)
{
Console.Error.WriteLine(de.Message);
}
catch (IOException ioe)
{
Console.Error.WriteLine(ioe.Message);
}
document.Close();
stream.Flush(); //Always catches me out
stream.Position = 0; //Not sure if this is required
return File(stream, "application/pdf", "DownloadName.pdf");
I got it working with this code.
using iTextSharp.text;
using iTextSharp.text.pdf;
public FileStreamResult pdf()
{
MemoryStream workStream = new MemoryStream();
Document document = new Document();
PdfWriter.GetInstance(document, workStream).CloseStream = false;
document.Open();
document.Add(new Paragraph("Hello World"));
document.Add(new Paragraph(DateTime.Now.ToString()));
document.Close();
byte[] byteInfo = workStream.ToArray();
workStream.Write(byteInfo, 0, byteInfo.Length);
workStream.Position = 0;
return new FileStreamResult(workStream, "application/pdf");
}
You must specify :
Response.AppendHeader("content-disposition", "inline; filename=file.pdf");
return new FileStreamResult(stream, "application/pdf")
For the file to be opened directly in the browser instead of being downloaded
If you return a FileResult from your action method, and use the File() extension method on the controller, doing what you want is pretty easy. There are overrides on the File() method that will take the binary contents of the file, the path to the file, or a Stream.
public FileResult DownloadFile()
{
return File("path\\to\\pdf.pdf", "application/pdf");
}
I've run into similar problems and I've stumbled accross a solution. I used two posts, one from stack that shows the method to return for download and another one that shows a working solution for ItextSharp and MVC.
public FileStreamResult About()
{
// Set up the document and the MS to write it to and create the PDF writer instance
MemoryStream ms = new MemoryStream();
Document document = new Document(PageSize.A4.Rotate());
PdfWriter writer = PdfWriter.GetInstance(document, ms);
// Open the PDF document
document.Open();
// Set up fonts used in the document
Font font_heading_1 = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 19, Font.BOLD);
Font font_body = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 9);
// Create the heading paragraph with the headig font
Paragraph paragraph;
paragraph = new Paragraph("Hello world!", font_heading_1);
// Add a horizontal line below the headig text and add it to the paragraph
iTextSharp.text.pdf.draw.VerticalPositionMark seperator = new iTextSharp.text.pdf.draw.LineSeparator();
seperator.Offset = -6f;
paragraph.Add(seperator);
// Add paragraph to document
document.Add(paragraph);
// Close the PDF document
document.Close();
// Hat tip to David for his code on stackoverflow for this bit
// https://stackoverflow.com/questions/779430/asp-net-mvc-how-to-get-view-to-generate-pdf
byte[] file = ms.ToArray();
MemoryStream output = new MemoryStream();
output.Write(file, 0, file.Length);
output.Position = 0;
HttpContext.Response.AddHeader("content-disposition","attachment; filename=form.pdf");
// Return the output stream
return File(output, "application/pdf"); //new FileStreamResult(output, "application/pdf");
}
FileStreamResult certainly works. But if you look at the Microsoft Docs, it inherits from ActionResult -> FileResult, which has another derived class FileContentResult. It "sends the contents of a binary file to the response". So if you already have the byte[], you should just use FileContentResult instead.
public ActionResult DisplayPDF()
{
byte[] byteArray = GetPdfFromWhatever();
return new FileContentResult(byteArray, "application/pdf");
}
You can create a custom class to modify the content type and add the file to the response.
http://haacked.com/archive/2008/05/10/writing-a-custom-file-download-action-result-for-asp.net-mvc.aspx
I know this question is old but I thought I would share this as I could not find anything similar.
I wanted to create my views/models as normal using Razor and have them rendered as Pdfs.
This way I had control over the pdf presentation using standard html output rather than figuring out how to layout the document using iTextSharp.
The project and source code is available here with nuget installation instructions:
https://github.com/andyhutch77/MvcRazorToPdf
Install-Package MvcRazorToPdf
You would normally do a Response.Flush followed by a Response.Close, but for some reason the iTextSharp library doesn't seem to like this. The data doesn't make it through and Adobe thinks the PDF is corrupt. Leave out the Response.Close function and see if your results are better:
Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-disposition", "attachment; filename=file.pdf"); // open in a new window
Response.OutputStream.Write(outStream.GetBuffer(), 0, outStream.GetBuffer().Length);
Response.Flush();
// For some reason, if we close the Response stream, the PDF doesn't make it through
//Response.Close();
HttpContext.Response.AddHeader("content-disposition","attachment; filename=form.pdf");
if the filename is generating dynamically then how to define filename here, it is generating through guid here.
if you return var-binary data from DB to display PDF on popup or browser means follow this code:-
View page:
#using (Html.BeginForm("DisplayPDF", "Scan", FormMethod.Post))
{
View PDF
}
Scan controller:
public ActionResult DisplayPDF()
{
byte[] byteArray = GetPdfFromDB(4);
MemoryStream pdfStream = new MemoryStream();
pdfStream.Write(byteArray, 0, byteArray.Length);
pdfStream.Position = 0;
return new FileStreamResult(pdfStream, "application/pdf");
}
private byte[] GetPdfFromDB(int id)
{
#region
byte[] bytes = { };
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT Scan_Pdf_File FROM PWF_InvoiceMain WHERE InvoiceID=#Id and Enabled = 1";
cmd.Parameters.AddWithValue("#Id", id);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
if (sdr.HasRows == true)
{
sdr.Read();
bytes = (byte[])sdr["Scan_Pdf_File"];
}
}
con.Close();
}
}
return bytes;
#endregion
}