GemBox C# - 在右上角放置二维码

GemBox C# - position qr code in top right corner

我正在我的 .net 项目中使用 GemBox 创建一个 PDF 文件,我想知道如何将二维码放在右上角。

使用下面的代码,我替换了我的 word 文件中的变量,并添加了二维码部分,二维码是在单独的页面上而不是在同一页面上创建的。

所以我的问题是,如何将二维码放置在同一页面上以及如何将其放置在右上角。

希望有人能帮忙:)

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

您需要将 QR 码添加到现有部分。

此外,要将其放置在右上角,您可以在开头或文档的 header 中插入右对齐 Paragraph 或插入浮动 TextBox.

以下是所有三种建议方法的示例。

插入现有部分的 body

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

插入现有部分的 header

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