您需要将 QR 码添加到现有部分。
此外,要将其定位在右上角,您可以Paragraph
在开头或文档标题中插入右对齐或插入浮动TextBox
.
以下是所有三种建议方法的示例。
插入现有部分的正文
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);