将SharePoint页面转换为PDF

时间:2017-08-01 12:02:28

标签: asp.net pdf sharepoint export-to-pdf pdf-conversion

在过去的几天里,我一直在努力将SharePoint页面转换为PDF。下面是我的代码。使用HttpWebRequest将页面内容读入HTML并使用SelectPdf converter将HTML转换为pdf。

它转换文本但缺少布局和图像。请提出最佳可能性。

string urlAddress = "<Here SP Page URL>";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
request.UseDefaultCredentials = true;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (response.StatusCode == HttpStatusCode.OK)
{
    Stream receiveStream = response.GetResponseStream();
    StreamReader readStream = null;

    if (response.CharacterSet == null)
    {
        readStream = new StreamReader(receiveStream);
    }
    else
    {
        readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
    }
    string strdata = readStream.ReadToEnd();

    response.Close();
    readStream.Close();

    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\testPage1.html"))
    {                     
        file.Write(strdata);   
    }

    HtmlToPdf converter = new HtmlToPdf();

    PdfDocument doc = converter.ConvertHtmlString(strdata);
    HtmlToPdfResult result = converter.ConversionResult;

    // save pdf document
    doc.Save(Response, false, "Sample12.pdf");

    // close pdf document
    doc.Close();

0 个答案:

没有答案