Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to coding a QR codes and had a quick question. I noticed that when i am attaching a qr code to a document regardless of the document page size whether one or 5 - it is attaching a different qr code number to each page in a single document. Is that right? I am using this to generate qr code to assignments and some assignments have multiple pages. I should have a single qr code for the document which will be the same on all pages of that document.

here is my code:

C#
for (int idx = 0; idx < inputDocument.PageCount; idx++)
                {

                    using (var dc = new DataContext())
                    {
                        // Add record stub for Assessment unless one already exists
                        Assessment a;
                        a = dc.Assessments.FirstOrDefault(
                            c => c.DocumentID == d.ID && c.FeedbackUserID == u.ID && c.PageNumber == idx + 1);
                        if (a == null)
                        {
                            a = new Assessment()
                            {
                                DocumentID = d.ID,
                                FeedbackUserID = u.ID,
                                PageNumber = idx + 1,
                                ID = Guid.NewGuid()
                            };
                        }
                        a.LastGeneratedDT = DateTime.UtcNow;
                        dc.SubmitChanges();

                        // Add copy of page to new document
                        PdfSharp.Pdf.PdfPage newpg = outputDocument.AddPage(inputDocument.Pages[idx]);

                        // Add tagging to the top or bottom of the page
                        PdfSharp.Drawing.XGraphics gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(newpg);


                        string gurl = a.ID.ToString().Replace("-", "");
                        string gocr = "*" + gurl.Substring(0, 10) + " " + gurl.Substring(10, 11) + " " +
                                      gurl.Substring(21) + "*";
                        // prep the visible string

                        AddQRTag(gfx, 3, 3, baseQR + gurl, gocr);
                    }

                }
Posted

1 solution

Hi,
you can see in your code your are adding a QR code to per page inside the for loop. shouldn't this be something like this:
C#
foreach (Document inputDocument in Documents)
{
    string gurl = "";
    string gocr = "";
    PdfSharp.Drawing.XGraphics gfx;
    for (int idx = 0; idx < inputDocument.PageCount; idx++)
    {
        using (var dc = new DataContext())
        {
            // Add record stub for Assessment unless one already exists
            Assessment a;
            a = dc.Assessments.FirstOrDefault(
            c => c.DocumentID == d.ID && c.FeedbackUserID == u.ID && c.PageNumber == idx + 1);
            if (a == null)
            {
                a = new Assessment()
                {
                    DocumentID = d.ID,
                    FeedbackUserID = u.ID,
                    PageNumber = idx + 1,
                    ID = Guid.NewGuid()
                 };
            }
            a.LastGeneratedDT = DateTime.UtcNow;
            dc.SubmitChanges();

            // Add copy of page to new document
            PdfSharp.Pdf.PdfPage newpg = outputDocument.AddPage(inputDocument.Pages[idx]);
 
            // Add tagging to the top or bottom of the page
            gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(newpg);
 

            string gurl = a.ID.ToString().Replace("-", "");
            string gocr = "*" + gurl.Substring(0, 10) + " " + gurl.Substring(10, 11) + " " +
                                      gurl.Substring(21) + "*";
        }
    }
    // prep the visible string
    AddQRTag(gfx, 3, 3, baseQR + gurl, gocr);
}



Regards
Jegan
 
Share this answer
 
Comments
dvr2013 6-Mar-13 22:04pm    
When i tried this i only got the QR code to show up on the second page and it ignored putting a code on the first page...
dvr2013 6-Mar-13 22:22pm    
I just tested a 3 page document and it only inserted the QR code only on the last page. It seems like it is adding one code on the last page and not in every page.
Jegan Thiyagesan 7-Mar-13 3:50am    
It is just a matter of rearranging the for loop, but I cannot test your code as I do not have your complete code.

There are couple of thing, move the using statement section out side the for loop, move the AddQRTag call back inside the for loop, as below.

using (var dc = new DataContext())
{
// Add record stub for Assessment unless one already exists
Assessment a;
a = dc.Assessments.FirstOrDefault(
c => c.DocumentID == d.ID && c.FeedbackUserID == u.ID &&);
if (a == null)
{
a = new Assessment()
{
DocumentID = d.ID,
FeedbackUserID = u.ID,
PageNumber = inputDocument.PageCount,
ID = Guid.NewGuid()
};
}
a.LastGeneratedDT = DateTime.UtcNow;
dc.SubmitChanges();
}

for (int idx = 0; idx < inputDocument.PageCount; idx++)
{
// Add copy of page to new document
PdfSharp.Pdf.PdfPage newpg = outputDocument.AddPage(inputDocument.Pages[idx]);

// Add tagging to the top or bottom of the page
PdfSharp.Drawing.XGraphics gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(newpg);


string gurl = a.ID.ToString().Replace("-", "");
string gocr = "*" + gurl.Substring(0, 10) + " " + gurl.Substring(10, 11) + " " +
gurl.Substring(21) + "*";

// prep the visible string
AddQRTag(gfx, 3, 3, baseQR + gurl, gocr);
}

Jegan

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900