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

发布于 2025-01-10 00:06:04 字数 713 浏览 4 评论 0原文

我正在 .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);

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, 
quot;{qrCodeValue} QR");

document.Sections.Add(new Section(document, new Paragraph(document, qrCodeField)));

document.Content.Replace("%FirstName%", data.firstName);
document.Content.Replace("%LastName%", data.lastName);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柳若烟 2025-01-17 00:06:04

您需要将 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);

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.

Insert into the body of an existing section

var qrCodeField = new Field(document, FieldType.DisplayBarcode, 
quot;{qrCodeValue} QR");
var qrCodeParagraph = new Paragraph(document, qrCodeField);
qrCodeParagraph.ParagraphFormat.Alignment = HorizontalAlignment.Right;
document.Sections[0].Blocks.Insert(0, qrCodeParagraph);

Insert into the header of an existing section

var qrCodeField = new Field(document, FieldType.DisplayBarcode, 
quot;{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);

Insert with a floating textbox

var qrCodeField = new Field(document, FieldType.DisplayBarcode, 
quot;{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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文