2013-10-31 36 views
3

我已經成功創建了我的課程中的發票PDF。 我在標題中添加了一個圖像,但問題是它只能在生產機器上運行。當我發佈到活服務器時,圖像會中斷,當我刪除圖像時,它會再次運行。iTextSharp add image

我已經使用這個代碼在pdfpcell中添加圖片: 注意www.mysite.co.za只是一個例子。

string logoUrl = "http://www.mysite.co.za/iCharter/images/Atlantic_logo.PNG"; 

iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(logoUrl); 

PdfPCell cell1 = new PdfPCell(new iTextSharp.text.Phrase(".",h0)); 
cell1.Colspan = 2; 
cell1.Border = 0; 
tblInfo.AddCell(cell1); 

cell1 = new PdfPCell(jpg); 
cell1.Colspan = 2; 
cell1.Border = 0; 
cell1.HorizontalAlignment = Element.ALIGN_RIGHT; 
tblInfo.AddCell(cell1); 
+2

這可能是一個權限問題 - 你的用戶上下文在你的活箱上是否有權進行外部調用?你是否活在一個代理的背後? – Paddy

+0

是的,如果我在瀏覽器中輸入地址,我已檢查權限,並可以訪問圖片。本地奇怪的事情確實發現在活箱上座位上的照片。我已經完成了很多類似的功能,我可以在live box中從webservice獲取圖片,然後在網頁上顯示。 – msandlana

+0

這不是你*可以訪問的圖像,而是你的應用程序運行的用戶上下文是否可以。除非這種情況相同。 – Paddy

回答

-1

如果您使用的是mvc,那麼您可以使用Controller和iTextSharp來生成pdf。

首先增加新PdfGeneratorController

using iTextSharp.text; 
using iTextSharp.text.xml; 
using iTextSharp.text.pdf; 

public class PdfGeneratorController : Controller 
{ 
    /// <summary> 
    /// Renders an action result to a string. This is done by creating a fake http context 
    /// and response objects and have that response send the data to a string builder 
    /// instead of the browser. 
    /// </summary> 
    /// <param name="result">The action result to be rendered to string.</param> 
    /// <returns>The data rendered by the given action result.</returns> 
    protected string RenderActionResultToString(ActionResult result) 
    { 
     // Create memory writer. 
     var sb = new StringBuilder(); 
     var memWriter = new StringWriter(sb); 

     // Create fake http context to render the view. 
     var fakeResponse = new HttpResponse(memWriter); 
     var fakeContext = new HttpContext(System.Web.HttpContext.Current.Request, fakeResponse); 
     var fakeControllerContext = new ControllerContext(
      new HttpContextWrapper(fakeContext), 
      this.ControllerContext.RouteData, 
      this.ControllerContext.Controller); 
     var oldContext = System.Web.HttpContext.Current; 
     System.Web.HttpContext.Current = fakeContext; 

     // Render the view. 
     result.ExecuteResult(fakeControllerContext); 

     // Restore data. 
     System.Web.HttpContext.Current = oldContext; 

     // Flush memory and return output. 
     memWriter.Flush(); 
     return sb.ToString(); 
    } 

    /// <summary> 
    /// Returns a PDF action result. This method renders the view to a string then 
    /// use that string to generate a PDF file. The generated PDF file is then 
    /// returned to the browser as binary content. The view associated with this 
    /// action should render an XML compatible with iTextSharp xml format. 
    /// </summary> 
    /// <param name="model">The model to send to the view.</param> 
    /// <returns>The resulted BinaryContentResult.</returns> 
    protected ActionResult ViewPdf(object model) 
    { 
     // Create the iTextSharp document. 
     Document doc = new Document(); 
     // Set the document to write to memory. 
     MemoryStream memStream = new MemoryStream(); 
     PdfWriter writer = PdfWriter.GetInstance(doc, memStream); 
     writer.CloseStream = false; 
     doc.Open(); 

     // Render the view xml to a string, then parse that string into an XML dom. 
     string xmltext = this.RenderActionResultToString(this.View(model)); 
     XmlDocument xmldoc = new XmlDocument(); 
     xmldoc.InnerXml = xmltext.Trim(); 

     // Parse the XML into the iTextSharp document. 
     ITextHandler textHandler = new ITextHandler(doc); 
     textHandler.Parse(xmldoc); 

     // Close and get the resulted binary data. 
     doc.Close(); 
     byte[] buf = new byte[memStream.Position]; 
     memStream.Position = 0; 
     memStream.Read(buf, 0, buf.Length); 

     // Send the binary data to the browser. 
     return new BinaryContentResult(buf, "application/pdf"); 
    } 
} 

然後從PdfGeneratorController繼承你經常控制器:

public class PrintToPdfController : PdfGeneratorController 
{ 
    public ActionResult Index() 
    { 
     return ViewPdf(GetModel(userName)); 
    } 
} 

而且在你看來,你可以添加圖片標籤:

<%@ Page Language=」C#」 Inherits=」System.Web.Mvc.ViewPage<Sample1.Models.Customer>」 %> 
<%@ Import Namespace=」Sample1.Models」 %> 

<?xml version=」1.0? encoding=」UTF-8? ?> 
<itext creationdate=」2/4/2003 5:49:07 PM」 producer=」iTextSharpXML」> 
<paragraph leading=」18.0? font=」unknown」 size=」16.0? align=」Default」> 
    <chunk>Orders in PDF</chunk> 
</paragraph> 
<paragraph leading=」18.0? font=」unknown」 size=」10.0? align=」Default」> 
    <chunk>Customer Name: <%= this.Model.Name %></chunk><newline /> 
    <chunk>Address: <%= this.Model.Address %></chunk><newline /> 
     <chunk><image url="<%=MapPath(Model.UserDetail.Photo)%>" plainwidth="100.0" plainheight="100.0" /> 
</paragraph> 
<paragraph leading=」18.0? font=」unknown」 size=」10.0? align=」Default」> 
<chunk font=」unknown」 size=」12.0?>Orders:</chunk><newline /> 
<% foreach (Order o in this.Model.Order) 
     { %> 
     <chunk font=」unknown」 size=」10.0?><%= o.OrderId %>, <%= o.Description %>, 
     <%= o.Amount %>, <%= o.Date %></chunk><newline /> 
<% } %> 
</paragraph> 
</itext> 
+1

這爲什麼會成爲這個問題的答案? (我是iText的原始開發人員,所以我不是新手。) –

+1

這是一種解決方法,但對我來說,它是mvc應用程序的精神,它使您可以更靈活地創建pdf。 –

+0

但它不能解決這個問題,這似乎與無法訪問服務器以獲取遠程映像有關。 –