看this site,看起來像這樣可以工作。
編輯:
下面是引用的網站
它一直在與iTextSharp的和HTMLWorker類呈現一個HTML頁面PDF人民的代碼和文本知道我是什麼談論:如果HTML包含具有相對路徑的圖像,您可能會獲得「友好」的黃色屏幕!
這意味着iTextShap試圖讓一個圖像與相對路徑「圖像/ screenshot.3.jpg」作爲本地文件「C:\影像\ screenshot.3.jpg」,所以,該圖像不存在。 經過了很多關於如何向iTextSharp提供正確圖像的研究之後,我發現一個人提到了「IImageProvider」界面,該界面爲iTextSharp提供了使用自定義方法查找圖像的能力。那麼,我已經做了一個使用iTextSharp 5.0.2.0的例子。你可以在這裏下載。
首先,在所有你需要創建一個實現IImageProvider接口一個類:
public class CustomItextImageProvider : IImageProvider
{
#region IImageProvider Members
public iTextSharp.text.Image GetImage(string src, Dictionary<string,string> imageProperties, ChainedProperties cprops, IDocListener doc)
{
string imageLocation = imageProperties["src"].ToString();
string siteUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(HttpContext.Current.Request.Url.AbsolutePath, "");
if (siteUrl.EndsWith("/"))
siteUrl = siteUrl.Substring(0, siteUrl.LastIndexOf("/"));
iTextSharp.text.Image image = null;
if (!imageLocation.StartsWith("http:") && !imageLocation.StartsWith("file:") && !imageLocation.StartsWith("https:") && !imageLocation.StartsWith("ftp:"))
imageLocation = siteUrl + (imageLocation.StartsWith("/") ? "" : "/") + imageLocation;
return iTextSharp.text.Image.GetInstance(imageLocation);
}
#endregion
}
它之後,你有渲染之前,這個圖像提供商作爲HTMLWorker類的「img_provider」的接口屬性分配HTML內容:
HTMLWorker worker = new HTMLWorker(document);
Dictionary<string, object> interfaceProps = new Dictionary<string, object>() {
{"img_provider", new CustomItextImageProvider()}
};
worker.InterfaceProps = interfaceProps;
現在,當您呈現HTML時,它應該可以處理相對圖像。
雖然這是ASP.Net的一個例子,但主要思想是如何在呈現HTML時爲iTextSharp創建一個自定義圖像提供程序,並且它可以在任何應用程序上使用,而且,這不僅可以幫助您獲取來自相對位置的圖像,我已經使用它(顯然有更多的代碼)用於從SharePoint或需要驗證的網站獲取圖像。
@kleopatra我已經加入從引用的網站上的代碼和文本。 – Pierre