Asp.net mvc iTextSharp将条形码存储为元素-空引用异常
我正在开发一个类,该类将在intranet web服务器上呈现PDF文档。我遇到的问题是,iTextSharp使用PDFWriter来呈现条形码图像,我试图在呈现文档之前构建一个IElements集合。有没有一种方法可以创建包含条形码图像的集合,或者有没有更好的方法来编码此类 希望我的代码能更好地解释它。谢谢Asp.net mvc iTextSharp将条形码存储为元素-空引用异常,asp.net-mvc,collections,itextsharp,barcode,Asp.net Mvc,Collections,Itextsharp,Barcode,我正在开发一个类,该类将在intranet web服务器上呈现PDF文档。我遇到的问题是,iTextSharp使用PDFWriter来呈现条形码图像,我试图在呈现文档之前构建一个IElements集合。有没有一种方法可以创建包含条形码图像的集合,或者有没有更好的方法来编码此类 希望我的代码能更好地解释它。谢谢 public class PDFDoc { public float Width { get; set; } public float Height { get; set
public class PDFDoc
{
public float Width { get; set; }
public float Height { get; set; }
private List<IElement> Elements { get; set;}
...
private MemoryStream ms = new MemoryStream();
private Document doc = new Document();
private PdfWriter writer;
private void initializeDocument()
{
doc = new Document(new Rectangle(Width, Height), marginLeft, marginRight, marginTop, marginBottom);
writer = PdfWriter.GetInstance(doc, ms);
Elements = new List<IElement>();
}
private FileStreamResult GenerateLabel(IEnumerable<IElement> elements)
{
doc.Open();
foreach (var e in elements)
{
doc.Add(e);
}
//doc.Add(GenerateBarcodeImage("123456")); //GenBC works here but not below
writer.CloseStream = false;
doc.Close();
byte[] byteinfo = ms.ToArray();
ms.Write(byteinfo, 0, byteinfo.Length);
ms.Position = 0;
ms.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(ms, "application/pdf");
}
private Image GenerateBarcodeImage(string code)
{
Barcode128 code128 = new Barcode128();
code128.Code = code;
code128.X = 1f;
return code128.CreateImageWithBarcode(new PdfContentByte(writer), null, null);
}
//Program
public FileStreamResult RenderLabel()
{
Elements.Add(new Paragraph("This is a test"));
Elements.Add(GenerateBarcodeImage("123456")); //This gives a null ref exception.
return GenerateLabel(Elements);
}
公共类PDFDoc
{
公共浮点宽度{get;set;}
公共浮动高度{get;set;}
私有列表元素{get;set;}
...
私有MemoryStream ms=新MemoryStream();
私有文档文档=新文档();
私人编剧;
私有无效初始化文档()
{
doc=新文档(新矩形(宽度、高度)、边距左侧、边距右侧、边距顶部、边距底部);
writer=PdfWriter.GetInstance(doc,ms);
元素=新列表();
}
私有文件流Result GenerateLabel(IEnumerable元素)
{
doc.Open();
foreach(元素中的变量e)
{
文件增补(e);
}
//doc.Add(GenerateBarcodeImage(“123456”);//GenBC在这里工作,但在下面不工作
writer.CloseStream=false;
doc.Close();
字节[]byteinfo=ms.ToArray();
ms.Write(byteinfo,0,byteinfo.Length);
ms.Position=0;
Seek女士(0,SeekOrigin.Begin);
返回新文件streamresult(ms,“application/pdf”);
}
专用图像生成器BarCodeImage(字符串代码)
{
Barcode128 code128=新的Barcode128();
代码128。代码=代码;
代码128.X=1f;
返回代码128.CreateImageWithBarcode(新的PdfContentByte(writer),null,null);
}
//节目
公共文件流结果RenderLabel()
{
添加(新段落(“这是一项测试”);
Elements.Add(GenerateBarcodeImage(“123456”);//这会给出一个null ref异常。
返回GenerateLabel(元素);
}
代码片段的问题是,在调用Document.Close()
后,您试图访问MemoryStream
。您可以将Close()
调用移动到调用ms.ToArray()
下面,或者完全取消MemoryStream
当您实例化一个PdfWriter
时,您可以使用任何可用的流
,那么为什么不使用响应。OutputStream
:
List<IElement> Elements = new List<IElement>();
using (Document document = new Document()) {
PdfWriter writer = PdfWriter.GetInstance(
document, Response.OutputStream
);
document.Open();
PdfContentByte cb = writer.DirectContent;
for (int i = 0; i < 9; ++i) {
Barcode128 code128 = new Barcode128();
code128.Code = "code " + i.ToString();
Elements.Add(code128.CreateImageWithBarcode(cb, null, null));
}
for (int i = 0; i < Elements.Count; ++i) {
document.Add(new Paragraph("Barcode: " + i.ToString()));
document.Add(Elements[i]);
}
}
List Elements=newlist();
使用(文档=新文档()){
PdfWriter writer=PdfWriter.GetInstance(
文档,Response.OutputStream
);
document.Open();
PdfContentByte cb=writer.DirectContent;
对于(int i=0;i<9;++i){
Barcode128 code128=新的Barcode128();
code128.Code=“Code”+i.ToString();
添加(code128.CreateImageWithBarcode(cb,null,null));
}
对于(int i=0;i
感谢您的反馈。在阅读了您的代码后,我意识到我的问题不是Closed()被调用了,而是Open()没有被调用。我将doc.Open()移动到initializeDocument()方法,它工作正常。我正在使用memorystream处理MVC中的FileStreamResult。