3

我已成功在课堂上创建了发票 PDF。我在标题中添加了一个图像,但问题是它只在生产机器上工作。当我发布到实时服务器时,图像会中断,当我删除图像时,它会再次起作用。

我已经使用此代码在 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);
4

1 回答 1

-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”&gt;
 <paragraph leading=”18.0? font=”unknown” size=”16.0? align=”Default”&gt;
     <chunk>Orders in PDF</chunk>
 </paragraph>
 <paragraph leading=”18.0? font=”unknown” size=”10.0? align=”Default”&gt;
  <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”&gt;
 <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>
于 2013-10-31T12:01:31.493 回答