Search code examples
c#pdfposter

Generate a PDF poster in C#


I have a requirement to auto generate a poster in PDF format based on a template in. This PDF must contain custom text and images provided via a web application. I know you can add from fields to a PDF then fill them with something like iTextSharp (I'm using C#) but these have solid backgrounds so can't be overlaid onto a poster. I have tried to create a transparent form control in OpenOffice but it does not seem possible. Can anyone suggest a method of fulfilling this requirement?

Many thanks


Solution

  • This can be done using ITextsharp.Its a very powerful PDF library. Keep the background as a pdf and load it in a stream. Then you can write on the loaded pdf and can return the poster.

     private PdfContentByte _pcb; 
    Document document = new Document();
    
    FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    PdfReader Mypdfreader= new PdfReader(fileStream);
     PdfTemplate background = writer.GetImportedPage(Mypdfreader, 1);
     document.NewPage();
    _pcb = writer.DirectContentUnder;
    _pcb.AddTemplate(background, 0, 0);
     _pcb = writer.DirectContent;
    _pcb.BeginText();
             _pcb.SetFontAndSize(BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false), 10);
     _pcb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, text, 10, 5, 0);//10,5 are x and y coordinates resp.
     _pcb.EndText();
    
     writer.Flush();
    fileStream.Close();
    

    or refer to the sample PDF poster