来自mvc中sql server的字节数组中的PDF [英] PDF from byte array from sql server in mvc
问题描述
我正在遵循的三个步骤。
在一个视图中创建PDF。
以byte []形式存储在数据库中哪一列是varbinary(max)
从数据库中检索并在用户点击时显示为PDF。
创建PDF:
public byte [] PDF(List< DeliveryOrderDetail> deliveryOrdList)
{
< span class =code-keyword> byte [] result;
string headingText = 指南跨度>;
尝试
{
使用(MemoryStream ms = new MemoryStream())
{
Document doc = new Document() ;
PdfWriter.GetInstance(doc,ms).CloseStream = false ;
PdfPTable pdfTab = new PdfPTable( 3 );
段落标题= new 段落(headingText, new 字体(Font.TIMES_ROMAN,18f,字体) .NORMAL,Color.BLACK));
段落ItemDetail = new 段落( 项目在交货单框中:, new 字体(Font.TIMES_ROMAN,12f,Font.NORMAL,Color.BLACK));
heading.Alignment = Element.ALIGN_CENTER;
ItemDetail.Alignment = Element.ALIGN_CENTER;
doc.Open();
doc.Add(heading);
pdfTab.HorizontalAlignment = 1 ; // 0-左,1-中,2-右
pdfTab.SpacingBefore = 20f ;
pdfTab.SpacingAfter = 20f;
// pdfTab.AddCell(Sl.No。);
pdfTab.AddCell( ItemName);
pdfTab.AddCell( BoxId);
pdfTab.AddCell( 制造商);
doc.Add(ItemDetail);
foreach ( var item in deliveryOrdList)
{
pdfTab.AddCell(item.Name);
pdfTab.AddCell(item.Id.ToString());
pdfTab.AddCell(item.Manufacturer);
}
doc.Add(pdfTab);
doc.Close();
result = ms.GetBuffer();
}
返回结果;
}
catch (例外e)
{
}
return null ;
}
使用db.savechanges()将数据表中的byte []存储到数据库表中;
点击图片:
var pdf将获取包含二进制数据的数据库字段
jQuery:点击图片后转到控制器
< script type = text / javascript>
$( document )。ready( function (){
$(< span class =code-string>' .pdf')。点击( function (){
var pdf = $( this )。nearest(< span class =code-string>' tr')。find(' < span class =code-string> td:eq(0)input')。val();;
alert(pdf);
$ .ajax({
url: / ship / PDF,
类型: POST,
dataType: json,
data: JSON .stringify( {pdf:pdf}),
// data:{pdf:pdf},
cache: false ,
dataType: json,
contentType: application / json; charset = utf-8
})
})
})
< / script>
在控制器中:编写以下方法,接受为字符串:
[HttpPost]
public ActionResult PDF( string pdf)
{
try
{
byte [] byteInfo = System.Text.Encoding.UTF8.GetBytes(pdf);
MemoryStream ms = new MemoryStream();
workStream.Write(byteInfo, 0 ,byteInfo.Length);
workStream.Position = 0 ;
return new FileStreamResult(ms, application / pdf);
}
// 捕捉异常
catch (Exception _Exception)
{
}
return 空跨度>;
}
这里我无法在点击图片时看到pdf.in浏览器。我已经尝试了所有选项,但无法得到它。如果我把它写在文件中。 PDF将无法打开
System.IO.File.WriteAllBytes( @ D:\personal\testpdf.pdf,byteInfo);
(< span class =code-sdkkeyword> document )。ready( function (){
(' .pdf')。点击( function ( ){
var pdf =
( this )。nearest(' tr')。find(' td:eq(0)input')。val();;
alert(pdf);
Three steps I am following.
Create PDF in one view.
Store in database in byte[] form which column is varbinary(max)
Retrieve from database and show as PDF when user click.
Creating PDF:
public byte[] PDF(List<DeliveryOrderDetail> deliveryOrdList)
{
byte[] result;
string headingText = "Guide";
try
{
using (MemoryStream ms = new MemoryStream())
{
Document doc = new Document();
PdfWriter.GetInstance(doc, ms).CloseStream = false;
PdfPTable pdfTab = new PdfPTable(3);
Paragraph heading = new Paragraph(headingText, new Font(Font.TIMES_ROMAN, 18f, Font.NORMAL, Color.BLACK));
Paragraph ItemDetail = new Paragraph("Items in Delivery Order Box:", new Font(Font.TIMES_ROMAN, 12f, Font.NORMAL, Color.BLACK));
heading.Alignment = Element.ALIGN_CENTER;
ItemDetail.Alignment = Element.ALIGN_CENTER;
doc.Open();
doc.Add(heading);
pdfTab.HorizontalAlignment = 1; // 0- Left, 1- Center, 2- right
pdfTab.SpacingBefore = 20f;
pdfTab.SpacingAfter = 20f;
// pdfTab.AddCell("Sl. No.");
pdfTab.AddCell("ItemName");
pdfTab.AddCell("BoxId");
pdfTab.AddCell("Manufacturer");
doc.Add(ItemDetail);
foreach (var item in deliveryOrdList)
{
pdfTab.AddCell(item.Name);
pdfTab.AddCell(item.Id.ToString());
pdfTab.AddCell(item.Manufacturer);
}
doc.Add(pdfTab);
doc.Close();
result = ms.GetBuffer();
}
return result;
}
catch(Exception e )
{
}
return null;
}
Storing result which is byte[] into database table with db.savechanges();
On image click :
var pdf will get the database field which contains binary data
jQuery: when image is clicked go to controller
<script type="text/javascript">
$(document).ready(function () {
$('.Pdf').click(function () {
var pdf = $(this).closest('tr').find('td:eq(0) input').val();;
alert(pdf);
$.ajax({
url: "/ship/PDF",
type: "POST",
dataType: "json",
data:JSON.stringify({ pdf: pdf }),
//data:{pdf:pdf},
cache: false,
dataType: "json",
contentType: "application/json; charset=utf-8"
})
})
})
</script>
In Controller: Following method is written which accept as string :
[HttpPost]
public ActionResult PDF(string pdf)
{
try
{
byte[] byteInfo = System.Text.Encoding.UTF8.GetBytes(pdf);
MemoryStream ms = new MemoryStream();
workStream.Write(byteInfo, 0, byteInfo.Length);
workStream.Position = 0;
return new FileStreamResult(ms, "application/pdf");
}
//to catch the exception
catch (Exception _Exception)
{
}
return null;
}
Here I am unable to see pdf.in browser when image is clicked. I have tried all options but unable to get it. If I write it in file. PDF will not get opened
System.IO.File.WriteAllBytes(@"D:\personal\testpdf.pdf", byteInfo);
(document).ready(function () {
('.Pdf').click(function () { var pdf =
(this).closest('tr').find('td:eq(0) input').val();; alert(pdf);
这篇关于来自mvc中sql server的字节数组中的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!