2013-04-17 73 views
0

在我的應用程序中,首先允許用戶使用CKEDITOR創建html文檔,其中用戶可以創建html文檔,並且可以插入圖像,表單字段等,而不是轉換生成的HTML文檔進入PDF。 如果HTML文檔包含純文本而不是PDF文件被成功創建,但是如果用戶在其中插入圖像而不是提供錯誤。 創建PDF文檔的代碼。使用itextsharp將圖像轉換爲PDF時出錯

public ActionResult CreateFile(FormCollection data) 
    { 
     var filename = data["filename"]; 
     var htmlContent = data["content"]; 
     string sFilePath = Server.MapPath(_createdPDF + filename + ".html"); 
     htmlContent = htmlContent.Trim(); 
     if (!System.IO.File.Exists(sFilePath)) 
     { 
      using (FileStream fs = new FileStream(sFilePath, FileMode.Create)) 
      { 
       using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8)) 
       { 
        w.Write(htmlContent); 
       } 
      } 
      createPDF(sFilePath); 
     } 

     return View(); 
    } 

    private MemoryStream createPDF(string sFilePath) 
    { 
     string filename = Path.GetFileNameWithoutExtension(sFilePath); 
     string name = Server.MapPath(_createdPDF + filename + ".pdf"); 
     MemoryStream ms = new MemoryStream(); 
     TextReader tr = new StringReader(sFilePath); 
     Document document = new Document(PageSize.A4, 30, 30, 30, 30); 
     string urldir = Request.Url.GetLeftPart(UriPartial.Path); 
     urldir = urldir.Substring(0, urldir.LastIndexOf("/") + 1); 
     Response.Write(urldir); 
     PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(name, FileMode.Create)); 
     document.Open(); 
     string htmlText = ""; 
     StreamReader sr; 
     sr = System.IO.File.OpenText(sFilePath); 
     htmlText = sr.ReadToEnd(); 
     sr.Close(); 
     WebClient wc = new WebClient(); 
     Response.Write(htmlText); 
     var props = new Dictionary<string, Object>(); 
     props["img_baseurl"] = @"C:\Documents and Settings\shubham\My Documents\visdatemplatemanger\visdatemplatemanger\"; 
     List<IElement> htmlarraylist = HTMLWorker.ParseToList(new StringReader(htmlText), null,props); 
     for (int k = 0; k < htmlarraylist.Count; k++) 
     { 
      document.Add((IElement)htmlarraylist[k]); 
     } 
     document.Close(); 
     System.IO.File.Delete(sFilePath); 
     UploadURL(name); 
     return ms; 
    } 

,我得到的,如果圖像是包括HTML文檔中的錯誤是:

Could not find a part of the path 'C:\Program Files\Common Files\Microsoft Shared\PDFimages\rectangle-shape.png'. 

回答

0

iTextSharp的將盡力解決相對的圖像基於HTTP的文件,但文件系統提供者,你需要或者提供絕對路徑或爲其提供搜索。

//Image search base, path will be concatenated directly so make sure it contains a trailing slash 
var props = new Dictionary<string, Object>(); 
props["img_baseurl"] = @"c:\images\"; 

//Include the props from above 
htmlarraylist = HTMLWorker.ParseToList(sr, null, props); 
+0

在這個應用程序允許用戶上傳圖像。路徑可以是任何東西。 我想要的是將HTML文件(可以包含圖像,表格,表單域等)轉換爲PDF文檔。 如果用戶創建僅包含文本而不包含任何圖像的文件,上面的代碼工作正常。請爲此提供解決方案 – pihu

+0

如果用戶可以上傳圖像,則只需將圖像存儲在服務器上的文件夾中,並將上面的代碼指向該文件夾即可。 –

+0

我照你所說的做了,但仍然得到錯誤信息:「找不到路徑的一部分」C:\ Documents and Settings \ shubham \ My Documents \ visdatemplatemanger \ PDFimages \ rectangle-shape.png'。「我允許用戶在PDF中包含的所有圖像都存儲在C:\ Documents and Settings \ shubham \ My Documents \ visdatemplatemanger \ PDFimages \ – pihu

相關問題