1
我有一個帶有生成pdf文件的按鈕的窗體。該文件通過一系列圖像和文本循環,並將每個圖像和文本插入文檔的絕對位置。我可以添加圖片,沒有文字,效果很好。我還可以在沒有圖像的情況下添加文本,並且一切正常。然而,當我同時添加這兩個,我得到的錯誤iTextsharp:在循環中添加圖片和文本(IE,Firefox,Chrome)
there was an error processing the document.
下面是代碼
[HttpPost]
public FileStreamResult ff(MyModel model)
{
MemoryStream workStream = new MemoryStream();
Document doc = new Document();
doc.SetMargins(50, 25, 25, 30);
PdfWriter.GetInstance(doc, workStream).CloseStream = false;
PdfWriter writer = PdfWriter.GetInstance(doc, workStream);
writer.CloseStream = false;
doc.Open();
PdfContentByte cb = writer.DirectContent;
string path = "";
Image Img;
int num = 0;
float imageSize = 160f, margin = 5f, position = 600;
ColumnText ct;
foreach (var item in model)
{
/*ct = new ColumnText(cb);
myText = new Phrase(new Chunk(numbering[num] + " " + item.Caption, FontFactory.GetFont(FontFactory.TIMES, 9, Font.ITALIC)));
ct.SetSimpleColumn(myText, doc.LeftMargin + margin, position + 5f, imageSize, 2, 15, Element.ALIGN_LEFT);
ct.Go();
x += 10;
y += 12;*/
/////The code above and the code below(withing the loop) do not work when they are together but work well when they are not together
path = item.path;
Img = Image.GetInstance(path);
Img.ScaleToFit(imageSize, imageSize);
Img.SetAbsolutePosition(doc.LeftMargin + margin, position);
Img.Border = Rectangle.TOP_BORDER | Rectangle.RIGHT_BORDER | Rectangle.BOTTOM_BORDER | Rectangle.LEFT_BORDER;
Img.BorderWidth = 1f;
doc.Add(Img);
num++;
position -= imageSize;
}
document.Close();
byte[] byteInfo = workStream.ToArray();
workStream.Write(byteInfo, 0, byteInfo.Length);
workStream.Position = 0;
return new FileStreamResult(workStream, "application/pdf");
}
Internet Explorer和Firefox顯示錯誤There was an error processing the page. There was an error reading this document(18)
。
Chrome確實顯示頁面,但圖片的排列是錯誤的。第二張圖片先出現,最後一張圖片未能顯示,但邊框顯示爲最後一張圖片(如空的畫布)
我在做什麼錯誤?