rebelliard
rebelliard

Reputation: 9611

Overlay text over an image background and convert to PDF

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. :)

Upvotes: 2

Views: 5385

Answers (3)

user438959
user438959

Reputation: 141

@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/

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

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();
}

Upvotes: 1

Adi
Adi

Reputation: 5223

Use iTextSharp. Free.

Upvotes: 0

Related Questions