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'.
在這個應用程序允許用戶上傳圖像。路徑可以是任何東西。 我想要的是將HTML文件(可以包含圖像,表格,表單域等)轉換爲PDF文檔。 如果用戶創建僅包含文本而不包含任何圖像的文件,上面的代碼工作正常。請爲此提供解決方案 – pihu
如果用戶可以上傳圖像,則只需將圖像存儲在服務器上的文件夾中,並將上面的代碼指向該文件夾即可。 –
我照你所說的做了,但仍然得到錯誤信息:「找不到路徑的一部分」C:\ Documents and Settings \ shubham \ My Documents \ visdatemplatemanger \ PDFimages \ rectangle-shape.png'。「我允許用戶在PDF中包含的所有圖像都存儲在C:\ Documents and Settings \ shubham \ My Documents \ visdatemplatemanger \ PDFimages \ – pihu