【问题标题】:Combine two Byte Image data into a single PDF and then download or print the PDF将两个字节图像数据合并为一个 PDF,然后下载或打印 PDF
【发布时间】:2017-06-03 03:33:54
【问题描述】:

我正在做一个 ASP.NET 网站来预订门票,然后下载它们。我有一个预定义的付款凭证和这张票,我已将其转换为图像。在订票期间,姓名、金额等详细信息将写在这些图像上,并以“字节”(varbinary)格式保存在数据库中。我正在使用下面的代码。

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[]));

然后,我将此值插入“凭证”列内的 SQL 表“UploadedFiles”中。同样,我也在使用相同的过程为票生成另一个图像,并将其插入到列名“票”中。

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

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString);
con.Open();
SqlCommand select = new SqlCommand("select * from UploadedFiles where BookingID=@id", 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 进行打印。

【问题讨论】:

    标签: c# mysql asp.net image pdf


    【解决方案1】:

    您可以使用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();
      }      
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-23
      • 2014-11-16
      • 1970-01-01
      • 2011-06-21
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多