I am trying to create a PDF using iText 7 in my Asp.Net application. Unfortunately whenever I try to open the PDF it tells me that the file is broken and can't be opened. I have no idea why because my code seems fine to me.
This is what I have:
public ActionResult CreateLieferschein()
{
MemoryStream stream = new MemoryStream();
PdfWriter writer = new PdfWriter(stream);
var pdf = new PdfDocument(writer);
var document = new Document(pdf);
document.Add(new Paragraph("Hello World!"));
return File(stream, "application/pdf", "test.pdf");
}
It looks like you're missing the call to document.Close()
Here's the example from their docs, see the last line:
var writer = new PdfWriter(dest);
var pdf = new PdfDocument(writer);
var document = new Document(pdf);
document.Add(new Paragraph("Hello World!"));
document.Close();
Related
I have this asp.net mvc C# project that returns a downloadable pdf file with A5 page size, and everything seems fine, but when I open the downloaded file and proceed to the print dialog box, I don't get the A5 sheet size by default, but Letter.
What am I missing? Is there a way to achieve this?
Here's the code, thanks in advance:
MemoryStream ms = new MemoryStream();
PdfWriter pw = new PdfWriter(ms);
PdfDocument pdfDocument = new PdfDocument(pw);
Document doc = new Document(pdfDocument, PageSize.A5.Rotate());
doc.SetMargins(5, 5, 5, 5);
//Here I add the content...
doc.Close();
byte[] bytesStream = ms.ToArray();
ms = new MemoryStream();
ms.Write(bytesStream, 0, bytesStream.Length);
ms.Position = 0;
return File(ms, "application/pdf", ID + ".pdf");
This question already has answers here:
How to return PDF to browser in MVC?
(11 answers)
Closed 8 months ago.
im trying to create a pdf file and im trying to return this file on imsonia or webbrowser, but i dont how to do this.
here is my code:
string beneficiarioRelatorio = "Test"
var stream = new MemoryStream();
PdfWriter writer = new PdfWriter(stream);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
//Paragraph header = new Paragraph(beneficiarioRelatorio);
document.Add(new Paragraph(beneficiarioRelatorio));
document.Close();
//return stream.ToArray();
//System.IO.File.WriteAllBytes("hello.pdf", stream.ToArray());
var teste = new FileStream(stream, FileMode.Open);
return new FileStreamResult(teste,"application/pdf");
try it
[HttpGet(Name = "GetPDF")]
public ActionResult GetPDF()
{
string beneficiarioRelatorio = "Test";
MemoryStream ms = new MemoryStream();
Document doc = new Document();
PdfWriter writer = PdfWriter.GetInstance(doc, ms);
doc.Open();
doc.Add(new Paragraph(beneficiarioRelatorio));
doc.Close();
byte[] bytes = ms.ToArray();
return File(bytes, "application/pdf");
}
I am trying to return a PDF with simple text, but getting the following error when downloading the document: Failed to load PDF document. Any ideas on how to resolve this is appreciated.
MemoryStream ms = new MemoryStream();
PdfWriter writer = new PdfWriter(ms);
PdfDocument pdfDocument = new PdfDocument(writer);
Document document = new Document(pdfDocument);
document.Add(new Paragraph("Hello World"));
//document.Close();
//writer.Close();
ms.Position = 0;
string pdfName = $"IP-Report-{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.pdf";
return File(ms, "application/pdf", pdfName);
You have to close the writer without closing the underlying stream, which will flush its internal buffer. As is, the document isn't being written to the memory stream in its entirety. Everything but ms should be in a using, too.
You can verify this is occuring by checking the length of ms in your code vs. the code below.
When the using (PdfWriter writer =...) closes, it will close the writer, which causes it to flush its pending writes to the underlying stream ms.
MemoryStream ms = new MemoryStream();
using (PdfWriter writer = new PdfWriter(ms))
using (PdfDocument pdfDocument = new PdfDocument(writer))
using (Document document = new Document(pdfDocument))
{
/*
* Depending on iTextSharp version, you might instead use:
* writer.SetCloseStream(false);
*/
writer.CloseStream = false;
document.Add(new Paragraph("Hello World"));
}
ms.Position = 0;
string pdfName = $"IP-Report-{DateTime.Now.ToString("yyyyMMddHHmmssfff")}.pdf";
return File(ms, "application/pdf", pdfName);
Keep getting an error 'Cannot access a closed Stream.'
Does the file need to be physically saved on a server first, prior to being Streamed? I am doing the same with Excel file and it works just fine. Trying to use the same principle here with PDF file.
[HttpGet("ExportPdf/{StockId}")]
public IActionResult ExportPdf(int StockId)
{
string fileName = "test.pdf";
MemoryStream memoryStream = new MemoryStream();
// Create PDF document
Document document = new Document(PageSize.A4, 25, 25, 25, 25);
PdfWriter pdfWriter = PdfWriter.GetInstance(document, memoryStream);
document.Open();
document.Add(new Paragraph("Hello World"));
document.Close();
memoryStream.Position = 0;
return File(memoryStream, "application/pdf", fileName);
}
Are you using iText7? If yes, please add that tag onto your question.
using (var output = new MemoryStream())
{
using (var pdfWriter = new PdfWriter(output))
{
// You need to set this to false to prevent the stream
// from being closed.
pdfWriter.SetCloseStream(false);
using (var pdfDocument = new PdfDocument(pdfWriter))
{
...
}
var renderedBuffer = new byte[output.Position];
output.Position = 0;
output.Read(renderedBuffer, 0, renderedBuffer.Length);
...
}
}
Switched to iText& (7.1.1) version as per David Liang comment
Here is the code that worked for me, a complete method:
[HttpGet]
public IActionResult Pdf()
{
MemoryStream memoryStream = new MemoryStream();
PdfWriter pdfWriter = new PdfWriter(memoryStream);
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
Document document = new Document(pdfDocument);
document.Add(new Paragraph("Welcome"));
document.Close();
byte[] file = memoryStream.ToArray();
MemoryStream ms = new MemoryStream();
ms.Write(file, 0, file.Length);
ms.Position = 0;
return File(fileStream: ms, contentType: "application/pdf", fileDownloadName: "test_file_name" + ".pdf");
}
I am using ITextSharp to merge PDFs.
My problem is when I merge huge PDFs, it takes a very long time to do it (many minutes). It appears that it takes all this time on the "document.close()".
Here is my code :
iTextSharp.text.Document doc = new iTextSharp.text.Document();
PdfCopy copy = new PdfCopy(doc, msOutput);
copy.SetMergeFields();
doc.Open();
byte[] byteArray = Convert.FromBase64String("someString");
PdfReader reader = new PdfReader(byteArray);
copy.AddDocument(reader);
doc.Close(); // <== It takes time here !
byte[] form = msOutput.ToArray();
Is there anything I did wrong ?
How can I improve this merging time ?
You are missing some Close() calls - this may help to bring your time down:
byte[] form
using (var msOutput = new MemoryStream())
{
iTextSharp.text.Document doc = new iTextSharp.text.Document();
byte[] byteArray = Convert.FromBase64String("someString");
PdfCopy copy = new PdfCopy(doc, msOutput);
copy.SetMergeFields();
doc.Open();
PdfReader reader = new PdfReader(byteArray);
copy.AddDocument(reader);
reader.Close();
copy.Close();
doc.Close();
form = msOutput.ToArray();
}
You should also be sure you are properly disposing of your stream after use.