将文本叠加在图像背景上并转换为 PDF

发布于 2024-10-30 08:54:24 字数 2017 浏览 0 评论 0原文

使用.NET,我想以编程方式创建一个 PDF,它仅包含一个背景图像,其上有两个具有不同字体和位置的标签。我已阅读过有关现有 PDF 库的信息,但不知道(如果适用)哪一个对于如此简单的任务来说最简单。

有人愿意指导我吗?

PD:我不想使用生成的图像创建 PDF 已经将文本覆盖在背景图像上

编辑:这是最终的工作代码:

public string Create()
{
    if (!Directory.Exists(ApplicationImagePath))
    {
        Directory.CreateDirectory(ApplicationImagePath);
    }

    // Smart card
    var doc = new Document(PageSize.GetRectangle("153 242.65"), 0, 0, 0, 0);            

    using (var stream = File.Create(filepath))
    {
       var writer = PdfWriter.GetInstance(doc, stream);

       doc.Open();

       var image = Image.GetInstance(CarnetData.Frame, ImageFormat.Png);
       image.Alignment = Element.ALIGN_CENTER;
       image.ScaleToFit(153, 242.65f);
       doc.Add(image);

       BaseFont font = BaseFont.CreateFont(GetFontPath(CarnetConfiguration.FontType), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
       font.PostscriptFontName = CarnetConfiguration.FontType.ToString();

       float verticalPosition = writer.GetVerticalPosition(false);
       var pName = new Paragraph(CarnetData.Name, new Font(font, FontData.EmployeeFont.SizeInPoints))
                        {
                            SpacingBefore = verticalPosition - 51f,
                            MultipliedLeading = 1.1f,
                            Alignment = Element.ALIGN_CENTER
                        };

        doc.Add(pName);

        var pDepartment = new Paragraph(CarnetData.Department, new Font(font, FontData.DepartmentFont.SizeInPoints))
        {
            SpacingBefore = 1.5f,
            MultipliedLeading = 1.2f,
            Alignment = Element.ALIGN_CENTER
        };

        doc.Add(pDepartment);

        writer.ViewerPreferences = PdfWriter.PageModeUseNone + PdfWriter.CenterWindow + PdfWriter.PageLayoutSinglePage;
        doc.Close();
    }

    return filepath;
}

感谢您的帮助。 :)

Using .NET, I want to programmatically create a PDF which simply consists of a background image with two labels over it with different fonts and positioning. I have read about existing PDF libraries, but don't know (if applicable) which one is the easiest for such a simple task.

Anyone care to guide me?

P.D.: I do not want to create the PDF with a generated image that already overlays the text over the background image.

Edit: This is the final working code:

public string Create()
{
    if (!Directory.Exists(ApplicationImagePath))
    {
        Directory.CreateDirectory(ApplicationImagePath);
    }

    // Smart card
    var doc = new Document(PageSize.GetRectangle("153 242.65"), 0, 0, 0, 0);            

    using (var stream = File.Create(filepath))
    {
       var writer = PdfWriter.GetInstance(doc, stream);

       doc.Open();

       var image = Image.GetInstance(CarnetData.Frame, ImageFormat.Png);
       image.Alignment = Element.ALIGN_CENTER;
       image.ScaleToFit(153, 242.65f);
       doc.Add(image);

       BaseFont font = BaseFont.CreateFont(GetFontPath(CarnetConfiguration.FontType), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
       font.PostscriptFontName = CarnetConfiguration.FontType.ToString();

       float verticalPosition = writer.GetVerticalPosition(false);
       var pName = new Paragraph(CarnetData.Name, new Font(font, FontData.EmployeeFont.SizeInPoints))
                        {
                            SpacingBefore = verticalPosition - 51f,
                            MultipliedLeading = 1.1f,
                            Alignment = Element.ALIGN_CENTER
                        };

        doc.Add(pName);

        var pDepartment = new Paragraph(CarnetData.Department, new Font(font, FontData.DepartmentFont.SizeInPoints))
        {
            SpacingBefore = 1.5f,
            MultipliedLeading = 1.2f,
            Alignment = Element.ALIGN_CENTER
        };

        doc.Add(pDepartment);

        writer.ViewerPreferences = PdfWriter.PageModeUseNone + PdfWriter.CenterWindow + PdfWriter.PageLayoutSinglePage;
        doc.Close();
    }

    return filepath;
}

Thanks for the help. :)

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

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

发布评论

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

评论(3

贱贱哒 2024-11-06 08:54:24

iTextSharp 是一个很棒的库,您可以使用它,非常简单直观:

var doc = new Document();
using (var stream = File.Create("output.pdf"))
{
    var writer = PdfWriter.GetInstance(doc, stream);
    doc.Open();

    doc.Add(Image.GetInstance(@"c:\foo\test.png"));

    var cb = writer.DirectContent;

    cb.BeginText();
    cb.SetTextMatrix(100, 220);
    var font = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
    cb.SetFontAndSize(font, 12);
    cb.ShowText("Hello World");
    cb.EndText();

    cb.BeginText();
    cb.SetTextMatrix(100, 250);
    cb.ShowText("Some other text");
    cb.EndText();

    doc.Close();
}

iTextSharp is a great library you could use, very simple and intuitive:

var doc = new Document();
using (var stream = File.Create("output.pdf"))
{
    var writer = PdfWriter.GetInstance(doc, stream);
    doc.Open();

    doc.Add(Image.GetInstance(@"c:\foo\test.png"));

    var cb = writer.DirectContent;

    cb.BeginText();
    cb.SetTextMatrix(100, 220);
    var font = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
    cb.SetFontAndSize(font, 12);
    cb.ShowText("Hello World");
    cb.EndText();

    cb.BeginText();
    cb.SetTextMatrix(100, 250);
    cb.ShowText("Some other text");
    cb.EndText();

    doc.Close();
}
滥情空心 2024-11-06 08:54:24

使用iTextSharp。自由的。

Use iTextSharp. Free.

伪装你 2024-11-06 08:54:24

@binaryhowl - 您可以尝试 Syncfusion PDF。它是一个很棒的组件,具有出色的支持

http://asp .syncfusion.com/sfaspnetsamplebrowser/9.1.0.20/Web/Pdf.Web/samples/4.0/

@binaryhowl - You can try Syncfusion PDF. It is great component with excellent support

http://asp.syncfusion.com/sfaspnetsamplebrowser/9.1.0.20/Web/Pdf.Web/samples/4.0/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文