Hello Forum,
I am trying to make it possible for a user to upload a pdf file and create a QR code for the file so that when the QR code is scanned using QR code scanner, it will call the file from the database and redirect the user to the webpage where the PDF file will display.
Here is what I tried but it seems not to work. I need help please
UploadPDF.aspx
<div class="container" id="centered" style="margin-top: -2%; font-family: Nunito;">
<div class="form-horizontal">
<h1 style="font-size: small; margin-top: 0%; color: red;">ID Card with QR Code</h1>
<hr />
<div class="mid-cont">
<br />
<div class="containr-fluid">
<asp:FileUpload runat="server" ID="showPreviewBill" />
<asp:Button ID="buttonmail" runat="server" CssClass="btn btn-primary navbar-btn" BackColor="SteelBlue" Font-Size="9pt" Text="QR code" OnClick="buttonmail_Click" />
<br />
<br />
</div>
<div class="contentt">
<canvas id="the-canvas" style="border: 1px solid grey; height: 850px; width: 700px;"></canvas>
</div>
</div>
<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
</div>
</div>
uploadPDF.aspx.cs
protected void buttonmail_Click(object sender, EventArgs e)
{
string filename = Path.GetFileName(showPreviewBill.PostedFile.FileName);
string contentType = showPreviewBill.PostedFile.ContentType;
using (Stream fs = showPreviewBill.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
using (SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True"))
{
string query = "INSERT INTO tableinvoice(email, Name, ContentType, Data, Uid, CreatedBy, Role, CreatedDate, Organization)" +
" VALUES(@email, @Name, @ContentType, @Data, @Uid, @CreatedBy, @Role, @CreatedDate, @Organization); SELECT @@IDENTITY";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@email", user.Text.Trim());
cmd.Parameters.AddWithValue("@Name", filename);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Data", bytes);
cmd.Parameters.AddWithValue("@Uid", labelid.Text.Trim());
cmd.Parameters.AddWithValue("@CreatedBy", createby.Text.Trim());
cmd.Parameters.AddWithValue("@Role", role.Text.Trim());
cmd.Parameters.AddWithValue("@CreatedDate", DateTime.Now);
cmd.Parameters.AddWithValue("@Organization", named.Text.Trim());
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Session["paperinv"] = Invoice;
Response.Redirect("PDFprint.aspx");
}
}
}
}
}
In the QR code and print page I dont understand the line of code where it is bold
PDFprint.aspx
<div class="container" id="centered" style="margin-top: -2%; font-family: Nunito;">
<div class="form-horizontal">
<h1 style="font-size: small; margin-top: 0%; color: red;">ID Card with QR Code</h1>
<hr />
<br />
<div class="row">
<asp:Panel ID="printpanel" runat="server">
<div class="contentt">
<%--<canvas id="the-canvas" style="border: 1px solid black; height: 750px; width: 650px;"></canvas>--%>
<asp:Literal ID="ltEmbed" runat="server" />
<asp:Image ID="Image1" runat="server" BorderStyle="None" Width="80px" Height="85px" />
</div>
</asp:Panel>
<br />
<div class="print">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnPrinnt" runat="server" CssClass="btn btn-primary navbar-btn" BackColor="SteelBlue" Font-Size="Small" Text="Send to Mail"/>
<br />
<br />
<asp:Button ID="buttonmail" runat="server" CssClass="btn btn-primary navbar-btn" BackColor="SteelBlue" Font-Size="Small" Text="Download Invoice"/>
</div>
</div>
</div>
</div>
using MessagingToolkit.QRCode.Codec;
using System.Drawing.Imaging;
using System.Drawing;
using System.Diagnostics;
using MessagingToolkit.QRCode.Codec.Data;
public partial class PDFprint: System.Web.UI.Page
{
SqlCommand cmd = new SqlCommand();
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Dataregister.mdf;Integrated Security=True");
private string imgBytes;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] == null)
{
// Response.Redirect("LoginForm.aspx");
}
if (!this.IsPostBack)
{
BindCardAndQR();
}
}
private void BindCardAndQR()
{
if (Session["paperinv"] != null)
{
int id = Convert.ToInt32(Session["paperinv"]);
// Binding PDF file in Literal control
string embed = "<object data=\"{0}{1}\" type=\"application/pdf\" width=\"650px\" height=\"750px\"></object>";
ltEmbed.Text = string.Format(embed, ResolveUrl("Invoiceprint.aspx?Id="), id);
//This is where I Bind Image1 control for the QR code generated
byte[] QRBytes = GetQRCodeBytes(Server.MapPath("/Invoiceprint.aspx") + "?Id=" + id);
Image1.ImageUrl = "data:image/jpg;base64," + Convert.ToBase64String(QRBytes);
}
}
public byte[] GetQRCodeBytes(string url)
{
//QR code generated here
QRCodeEncoder encoder = new QRCodeEncoder();
Bitmap bi = encoder.Encode(url);
MemoryStream tmpSteam = new MemoryStream();
bi.Save(tmpSteam, ImageFormat.Jpeg);
return tmpSteam.ToArray();
}
}