0

.net プロジェクトで GemBox を使用して PDF ファイルを作成していますが、qr コードを右上隅に配置する方法を知りたいと思っています。

以下のコードでは、Word ファイルの変数を置き換えています。qr コード セクションを追加すると、同じページではなく別のページに qr コードが作成されます。

私の質問は、qrコードを同じページに配置する方法と、右上隅に配置する方法です。

誰かが助けてくれることを願っています:)

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

1 に答える 1

1

QR コードを既存のセクションに追加する必要があります。

また、右上隅に配置するには、右揃えParagraphを先頭またはドキュメントのヘッダーに挿入するか、フローティング を挿入しTextBoxます。

以下に、推奨される 3 つのアプローチすべての例を示します。

既存のセクションの本文に挿入

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);
于 2022-02-28T07:09:52.633 に答える