简体   繁体   English

如何在 C# 中使用 IText7 在现有 pdf 顶部添加条形码?

[英]How to add a Barcode at the top of an existing pdf using IText7 in C#?

I'm trying to use IText7 for C# in order to add a Barcode128 at the top of an existing PDF page.我正在尝试将 IText7 用于 C#,以便在现有 PDF 页面的顶部添加 Barcode128。

Every examples I've found for the moment are adding the barcode on a new page or are done using iTextSharp or IText5.我目前发现的每个示例都是在新页面上添加条形码或使用 iTextSharp 或 IText5 完成的。

If you have any clue of how to perform what I need, I'll be very thankful.如果您对如何执行我需要的操作有任何线索,我将不胜感激。

You have several ways to place a barcode on a PDF, as per the examples . 根据示例,您有多种方法可以在 PDF 上放置条形码。 I've converted one of the examples into C# for your convenience:为方便起见,我已将其中一个示例转换为 C#:

using (var document = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST)))
        {
            PdfCanvas canvas = new PdfCanvas(document.GetFirstPage());
            Barcode128 code128 = new Barcode128(document);
            code128.SetCode("12345XX789XXX");
            code128.SetCodeType(Barcode128.CODE128);
            PdfFormXObject xObject =
                code128.CreateFormXObject(ColorConstants.BLACK, ColorConstants.BLACK, document);
            float x = 36;
            float y = 750;
            float w = xObject.GetWidth();
            float h = xObject.GetHeight();
            canvas.SaveState();
            canvas.SetFillColor(ColorConstants.LIGHT_GRAY);
            canvas.Rectangle(x, y, w, h);
            canvas.Fill();
            canvas.RestoreState();
            canvas.AddXObject(xObject, 36, 750);
        }

it should be easy to tweak it to do what you are trying to achieve.应该很容易对其进行调整以实现您想要实现的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM