How can I assign the contents of an iTextSharp.text.Document to either a byte array or a Memory Stream?

我的未来我决定 提交于 2019-12-24 15:51:59

问题


I have working code in an ASP.NET Web API app that downloads an Excel spreadsheet / .xls file to the user's machine when this method is called via a click on a link:

public HttpResponseMessage Get(string unit, string begindate, string enddate)
{
    byte[] excelContents;

    string selectStmt = "SELECT BinaryData FROM ReportsGenerated WHERE FileBaseName = @fileBaseName";
    string fbn = string.Format("deliveryperformance/{0}/{1}/{2}", unit, begindate, enddate);
    using (SqlConnection connection = new SqlConnection(PlatypusWebReportsConstsAndUtils.CPSConnStr))
    using (SqlCommand cmdSelect = new SqlCommand(selectStmt, connection))
    {
        cmdSelect.Parameters.Add("@fileBaseName", SqlDbType.VarChar).Value = fbn;
        connection.Open();
        excelContents = (byte[])cmdSelect.ExecuteScalar();
        connection.Close();
    }

    HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new ByteArrayContent(excelContents)
    };
    result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = string.Format("{0}.xlsx", fbn)
    };
    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

    return result;
}

I want to accomplish virtually the same thing from an ASP.NET MVC app with a Controller method like so:

internal static HttpResponseMessage GeneratePDFOfReportFutures()
{
    HttpResponseMessage result = null;
    futureReports = GetAllFutureReports();
    try
    {
        using (var ms = new MemoryStream())
        {
            using (var doc = new Document(PageSize.A4.Rotate(), 25, 25, 25, 25))
            {
                using (PdfWriter.GetInstance(doc, ms))
                {
                    doc.Open();
                    . . .
                    doc.Add(tblHeadings);

                    foreach (QueuedReports qr in futureReports)
                    {
                        . . .
                        doc.Add(tblRow);
                    }

                    var bytes = ??? // This is the conundrum

                    result = new HttpResponseMessage(HttpStatusCode.OK)
                    {
                        Content = new ByteArrayContent(bytes)
                    };
                    result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                    {
                        FileName = string.Format("{0}.pdf", "test")
                    };
                    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                } // memoryStream
            } // doc
        } // pdfWriter                
    } // try
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    return result;
} // GeneratePDFOfReportFutures

So in the case of the Excel file, I have already written the data to a database and then in the GET method above I read it back out, assigning the contents of that field in the table to the byte array.

In the case of the PDF file, I have no need to first store the data to a table, so I just want to cut out the DB middleman and save the contents of the byte array to a file on the user's machine. The problem is that I don't know how to assign the contents of the iTextSharp.text.Document to the byte array (see the "???" line in the GeneratePDFOfReportFutures() method).

Alternatively, how could I assign the iTextSharp.text.Document to the MemoryStream, and then assign taht to the byte array like so:

var bytes = ms.ToArray();

???

So what I need to know is, how can I assign the data from an iTextSharp.text.Document either directly to a byte array or to a MemoryStream?

来源:https://stackoverflow.com/questions/37123068/how-can-i-assign-the-contents-of-an-itextsharp-text-document-to-either-a-byte-ar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!