I am creating a PDF-file with GemBox in my .net project and I am wondering how to position the qr code in the top right corner.
With the code below, I am replacing variables in my word file and with the addition of the qr code section, the qr code is created on a separate page instead on the same page.
So my question is, how to place the qr code on the same page and how to position it in the top right corner.
I hope someone can help :)
var qrCodeValue = JsonConvert.SerializeObject(new
{
FirstName = data.firstName,
LastName = data.lastName,
CreationDate = data.documentCreationDate
});
var qrCodeField = new Field(document, FieldType.DisplayBarcode, $"{qrCodeValue} QR");
document.Sections.Add(new Section(document, new Paragraph(document, qrCodeField)));
document.Content.Replace("%FirstName%", data.firstName);
document.Content.Replace("%LastName%", data.lastName);
You need to add the QR code to an existing section.
Also, to position it in the top right corner you could insert the right aligned Paragraph
at the beginning or in the document's header or insert a floating TextBox
.
Here are the examples for all three suggested approaches.
var qrCodeField = new Field(document, FieldType.DisplayBarcode, $"{qrCodeValue} QR");
var qrCodeParagraph = new Paragraph(document, qrCodeField);
qrCodeParagraph.ParagraphFormat.Alignment = HorizontalAlignment.Right;
document.Sections[0].Blocks.Insert(0, qrCodeParagraph);
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;
if (headersFooters[HeaderFooterType.HeaderFirst] == null)
headersFooters.Add(new HeaderFooter(document, HeaderFooterType.HeaderFirst));
headersFooters[HeaderFooterType.HeaderFirst].Blocks.Insert(0, qrCodeParagraph);
var qrCodeField = new Field(document, FieldType.DisplayBarcode, $"{qrCodeValue} QR");
var qrCodeParagraph = new Paragraph(document, qrCodeField);
var qrTextBox = new TextBox(document,
new FloatingLayout(
new HorizontalPosition(-50, LengthUnit.Point, HorizontalPositionAnchor.RightMargin),
new VerticalPosition(50, LengthUnit.Point, VerticalPositionAnchor.TopMargin),
new Size(100, 100)),
qrCodeParagraph);
qrTextBox.Outline.Fill.SetEmpty();
var paragraph = (Paragraph)document.Sections[0]
.GetChildElements(false, ElementType.Paragraph)
.First();
paragraph.Inlines.Add(qrTextBox);