Add QR Code to every page of my document

I have a Word document (3 pages) and want to insert a QR Code at the top of every page and save it as PDF.
I have seen this question on stackoverflow, but I can only display the code at the first page:

Hi,

You can add the QR code inside the default header instead of the first header.
In other words, something like this:

var qrCodeField = new Field(document, FieldType.DisplayBarcode, $"{qrCodeValue} QR");
var qrCodeParagraph = new Paragraph(document, qrCodeField);
qrCodeParagraph.ParagraphFormat.Alignment = HorizontalAlignment.Right;

var headersFooters = document.Sections[0].HeadersFooters;
var header = headersFooters[HeaderFooterType.HeaderDefault];
if (header == null)
{
    header = new HeaderFooter(document, HeaderFooterType.HeaderDefault);
    headersFooters.Add(header);
}

header.Blocks.Insert(0, qrCodeParagraph);

I hope this helps.

Regards,
Mario

Thank you. Strangely, the qr code is now only printed on pages two and three, not on the first page.

Also I have another question: Is it possible, to insert different qr codes on every page? As far as I can see, you can only set headers for even and odd page numbers. Can you select each page and define a header for that certain page?

Hi,

If you’re not getting the QR code on the first page, that means that your document has both default and first header specified. So, what you would need to do is add the QR code to both of those headers.

But anyway, to insert a different QR code on each page, you’ll need to add them to your document’s body content.
To do this you’ll need to select one Paragraph element that’s on each page and then add a QR code to it.

Can you send us a sample of your Word document?
I’ll create an example that will show how you can do that.

Regards,
Mario

Sure. How can I send the Word document to you?

Send us an email, or submit a support ticket, see our Contact page.

Or upload it somewhere (like your Google Drive or OneDrive) and post a download link here.

I’ve uploaded the document to: https://easyupload.io/ud8f9o

Hi,

Try this:

var document = DocumentModel.Load("sample_document.docx");

// Add PAGE fields.
var pageFields = new List<Field>();
foreach (Paragraph paragraph in document.Sections.SelectMany(s => s.Blocks).OfType<Paragraph>())
{
    var pageField = new Field(document, FieldType.Page) { CharacterFormat = { Hidden = true } };
    pageFields.Add(pageField);
    paragraph.Inlines.Insert(0, pageField);
}

// Update PAGE fields result.
document.GetPaginator(new PaginatorOptions() { UpdateFields = true });

// Retrieve one Paragraph for each page.
var pageParagraphs = new List<Paragraph>();
string previousPage = "";
foreach (var pageField in pageFields)
{
    var paragraph = pageField.Parent as Paragraph;
    string page = pageField.Content.ToString();

    if (page != previousPage)
    {
        pageParagraphs.Add(paragraph);
        previousPage = page;
    }

    paragraph.Inlines.Remove(pageField);
}

// Add floating TextBox elements to each page Paragraph.
for (int i = 0; i < pageParagraphs.Count; i++)
{
    var qrCodeField = new Field(document, FieldType.DisplayBarcode, $"{i + 1000} QR");
    var qrCodeParagraph = new Paragraph(document, qrCodeField);

    var textbox = new TextBox(document,
        new FloatingLayout(
            new HorizontalPosition(18, LengthUnit.Centimeter, HorizontalPositionAnchor.Page),
            new VerticalPosition(0, LengthUnit.Centimeter, VerticalPositionAnchor.Page),
            new Size(3, 3, LengthUnit.Centimeter))
        { WrappingStyle = TextWrappingStyle.InFrontOfText }, qrCodeParagraph);

    textbox.TextBoxFormat.InternalMargin = new Padding(0);
    textbox.Outline.Fill.SetEmpty();

    pageParagraphs[i].Inlines.Insert(0, textbox);
}

document.Save("output.docx");
document.Save("output.pdf");

This is the result:

I hope this helps.

Regards,
Mario