开发者

How to convert from string into pdf?

Currently I'm using Restful service in asp.net c# and the following is the pdf string return that I get, I would like to convert it and save it as a .pdf file. 开发者_运维百科How should I do it?

static string HttpGet(string url)
        {
            HttpWebRequest req = WebRequest.Create(url)
                                 as HttpWebRequest;
            string result = null;
            using (HttpWebResponse resp = req.GetResponse()
                                          as HttpWebResponse)
            {


                StreamReader reader =
                    new StreamReader(resp.GetResponseStream());
                result = reader.ReadToEnd();
            }
            return result;
        }

/****************************** result returned ******************************/ 
%PDF-1.3
%����
3 0 obj
<<
/Linearized 1
/O 5
/H [ 526 186 ]
/L 47163
/E 46840
/N 1
/T 47053
>>
endobj                          
xref
3 11
0000000016 00000 n
0000000436 00000 n
0000000712 00000 n
0000000957 00000 n
0000001056 00000 n
0000001078 00000 n
0000046475 00000 n
0000046502 00000 n
0000046611 00000 n
0000046725 00000 n
0000000526 00000 n
trailer
<<
/Size 14
/Info 1 0 R
/Root 4 0 R
/Prev 47044
>>
startxref
0
%%EOF
4 0 obj
<<
/Type /Catalog
/Pages 2 0 R
>>
endobj

13 0 obj
<<
/Length 104
/P 0
/S 46
>>                   
stream


using (Stream stream = ... fetch the stream from somewhere)
{
    byte[] buffer = new byte[stream.Length];
    stream.Read(buffer, 0, buffer.Length);
    File.WriteAllBytes("foo.pdf", buffer);
}

and if this RESTful service talks HTTP you could use a WebClient:

using (var client = new WebClient())
{
    client.DownloadFile("http://example.com/api", "foo.pdf");
}


I had this situation and I solved using iTextSharp to generate the pdf and to save in disk.

public void GeneratePdf(string htmlPdf)
{
    var pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
    var htmlparser = new HTMLWorker(pdfDoc);
    using (var memoryStream = new MemoryStream())
    {
        var writer = PdfWriter.GetInstance(pdfDoc, memoryStream);
        pdfDoc.Open();

        htmlparser.Parse(new StringReader(htmlPdf));
        pdfDoc.Close();

        byte[] bytes = memoryStream.ToArray();
        File.WriteAllBytes(@"C:\file.pdf", bytes);

        memoryStream.Close();
    }
}


In my case Controller fetching PDF byte array, that is saved in db. For testing when I tried to convert using System.Text.Encoding.UTF8.GetString(pdfByte) got

"%PDF-1.3%����3 0 obj...."

above solution implemented but after download file not opening in 'Adobe Acrobat'.

Same 'pdfByte' I passed to below method and pdf downloaded successfully and able to open in 'Adobe Acrobat'.

I used iTextSharp.LGPLv2.Core for DownloadPdfFile method and for DownloadPDF method used iTextSharp 5.5.13.3

private FileContentResult DownloadPDF(byte[] pdfByte, string fileName)
{
    byte[] pdf;
    using (var memoryStream = new MemoryStream())
    {
        using (PdfReader reader = new PdfReader(pdfByte))
        {
            var document = new Document(PageSize.A4, 50, 50, 60, 60);
            PdfCopy copy = new PdfSmartCopy(document, memoryStream);
            document.Open();
            copy.AddDocument(reader);
            reader.Close();
            document.Close();
        }
        pdf = memoryStream.ToArray();
    }
    return File(pdf, "application/pdf", fileName + ".pdf");
}

Or

private FileContentResult DownloadPdfFile(byte[] pdfByte, string fileName)
{
    byte[] pdf;
    var memoryStream = new MemoryStream();
    PdfReader reader = new PdfReader(pdfByte);
    var document = new Document(PageSize.A4, 50, 50, 60, 60);
    PdfCopy copy = new PdfSmartCopy(document, memoryStream);
    document.Open();
    for (int pagenumber = 1; pagenumber <= reader.NumberOfPages; pagenumber++)
    {
        copy.AddPage(copy.GetImportedPage(reader, pagenumber));
    }
    reader.Close();
    document.Close();
    pdf = memoryStream.ToArray();
    memoryStream.Close();
    return File(pdf, "application/pdf", fileName + ".pdf");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜