2017-06-03 46 views
0

我正在做一个ASP.NET网站来预订门票,然后下载它们。我有一个预定义的付款凭证和票据,我已经转换成图像。在预订票时,会在这些图像上写入名称,金额等详细信息并以“字节”(varbinary)格式保存在数据库中。我正在使用下面的代码。将两个字节图像数据合并成一个PDF,然后下载或打印PDF

StringFormat stringformat = new StringFormat(); 
stringformat.Alignment = StringAlignment.Near; 
stringformat.LineAlignment = StringAlignment.Near; 
Color StringColor = System.Drawing.ColorTranslator.FromHtml("#000000"); 

System.Drawing.Image creditBitmap = (System.Drawing.Image)Bitmap.FromFile(Server.MapPath("~/Images/voucher.jpg")); 
Graphics creditImage = Graphics.FromImage(creditBitmap); 
creditImage.DrawString(name, new Font("arial", 12, 
FontStyle.Bold), new SolidBrush(StringColor), new Point(160, 519), 
stringformat); 
creditImage.DrawString(amount, new Font("arial", 12, 
FontStyle.Bold), new SolidBrush(StringColor), new Point(320, 181), 
stringformat); 

Response.ContentType = "~/Images/voucher1.jpg"; 
byte[] credit = (byte[])(new ImageConverter()).ConvertTo(creditBitmap, typeof(byte[])); 

然后我将这个值插入列“Voucher”列中的SQL表“UploadedFiles”中。同样,我也使用相同的过程为故障单生成另一张图像,并将其插入列名称“故障单”中。

现在我想下载凭证和票据合并为一个PDF。我正在使用以下代码从数据库中获取它们,并在点击按钮时将它们转换为字节数组。

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString); 
con.Open(); 
SqlCommand select = new SqlCommand("select * from UploadedFiles where [email protected]", con); 
select.Parameters.AddWithValue("@id", bookingID); 
SqlDataReader data = select.ExecuteReader(); 
data.Read(); 
byte[] voucher = (byte[])data["Voucher"]; 
byte[] ticket = (byte[])data["Ticket"]; 
image_voucher.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(voucher, 0, voucher.Length); 
image_ticket.ImageUrl = "data:image/jpeg;base64," + Convert.ToBase64String(ticket, 0, ticket.Length); 
con.Close(); 

这两个图像显示在网页中。但现在我想将它们作为一个PDF文件下载。这两个图像的分辨率都是800 x 480,我想将它们放在PDF的单个页面中。在生成PDF之后,我应该同时选择下载或直接使用两个不同的按钮打印它。我怎样才能做到这一点?有没有简单的方法?

我想要有两个按钮一个用于下载,另一个用于打印。当我点击下载按钮时,应该从数据库中提取图像,将其转换为单个PDF并自动下载。在点击打印按钮时,图像应该从数据库中提取出来,转换成一个PDF文件,并且这个PDF文件应该被提示打印。

回答

0

您可以使用itextsharp lib制作一张带有x张照片的pdf。

使用下面的代码:

iTextSharp.text.Document doc = new iTextSharp.text.Document(); 

//pdfpath is the full path of where you want the pdf to be created 
//keep in mind to check for directory existence 
PdfWriter.GetInstance(doc, new FileStream(pdfpath, FileMode.Create)); 
doc.Open(); 

//files is a list of your files as FileInfo 
foreach (var file in files) 
{ 
    try 
    { 
    Image docImage = Image.GetInstance(file.FullName); 
    //scale the images to fit the document size 
    docImage.ScaleToFit(doc.PageSize.Width, doc.PageSize.Height); 
    docImage.Alignment = Image.ALIGN_CENTER; //align the pictures 
    doc.Add(docImage); 
    } 
    catch (Exception ex) 
    { 
    //exception handle logic 
    } 
    finally 
    { 
    doc.close(); 
    }  
}