1

I have the following code:

public byte[] ExportToPdf(DataTable dt)
{
    iTextSharp.text.Document document = new iTextSharp.text.Document();

    document.Open();
    iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont(FontFactory.HELVETICA, 5);

    PdfPTable table = new PdfPTable(dt.Columns.Count);
    PdfPRow row = null;
    float[] widths = new float[] { 4f, 4f, 4f, 4f, 4f, 4f, 4f, 4f };

    table.SetWidths(widths);

    table.WidthPercentage = 100;

    foreach (DataColumn c in dt.Columns)
    {
        table.AddCell(new Phrase(c.ColumnName, font5));
    }

    document.Add(table);
    document.Close();

    byte[] bytes;
    MemoryStream msPDFData = new MemoryStream();
    PdfWriter writer = PdfWriter.GetInstance(document, msPDFData);

    return msPDFData.ToArray();
}

And in another function i call the function like this:

byte[] bytes = ExportToPdf(table);
return File(bytes, "application/pdf", "RaportDocumenteEmise.pdf");

When i try to open the pdf it says that is damaged. Somehow the byte array is empty. Can say me what am i doing wrong?

2
  • Actually the PdfWriter should be instantiated even before the document is opened. Commented Jun 17, 2014 at 16:51
  • Your "solution" is wrong, please remove the comment. See my answer for the correct solution. Commented Jun 18, 2014 at 9:01

2 Answers 2

1

This is wrong:

iTextSharp.text.Document document = new iTextSharp.text.Document();
document.Open();

A PDF is created using 5 simple steps:

  1. Create a Document object
  2. Create a PdfWriter instance
  3. Open the document
  4. Add content
  5. Close the document

You don't have step 2. In your comment, you say that you've solved the problem by creating that instance after opening the document, but that's wrong! You need to create the PdfWriter instance before opening the document.

Opening the document writes the PDF header to the OutputStream. That can't happen without a valid PdfWriter instance.

Sign up to request clarification or add additional context in comments.

Comments

0

I was trying to do the same thing as you. After many tries, I discovered that if I put PdfWriter.GetInstance inside using (var ms = new MemoryStream()) { } everything works alright!

My full code is:

    public FileContentResult GetPDF() {
        string htmlContent = "<p>First line</p><p>Second line</p>";
        StringReader sr = new StringReader(htmlContent);
        Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
        HTMLWorker hw= new HTMLWorker(pdfDoc);

        FileContentResult result;
        using (var ms = new MemoryStream()) {
            PdfWriter.GetInstance(pdfDoc, ms);
            pdfDoc.Open();
            hw.Parse(sr);
            pdfDoc.Close();
            result = this.File(ms.ToArray(), "application/pdf", "teste.pdf");
        }
        return result;
    }

Ps.: This is a method inside my Controller.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.