0

I am trying to show a bar code on the pdf file. There's a css class already created that converts a job number into a bar code. So is there a way to set the PdfContentByte to this css class?

Code:

using (MemoryStream ms = new MemoryStream())
        {
            Document document = new Document(PageSize.A4);

            PdfWriter writer = PdfWriter.GetInstance(document, ms);

            document.Open();

            document.NewPage();
            string fontpath = "C:\\windows\\fonts\\"; 

            //Setup fonts
            BaseFont customBaseFont = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            Font customFont = new Font(customBaseFont, 9, Font.NORMAL, BaseColor.BLACK);

            BaseFont customBaseFontBold = BaseFont.CreateFont(BaseFont.COURIER_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
            Font customFontBold = new Font(customBaseFontBold, 7, Font.NORMAL, BaseColor.BLACK);


            PdfContentByte cb = writer.DirectContent;
            cb.BeginText();

            cb.SetFontAndSize(customBaseFont, 11);
            cb.SetTextMatrix(57f, 810f + 7f);
            cb.ShowText("*" + jc.JobNo + "*");
}

css code:

@font-face {
    font-family: barcode;
    src: url(data:font/opentype;charset=utf-8;base64,d09GRgABAAAAAAbQAA0AAAAAFagAAQAAAAAAAAAAAAAAAAAAAAAAAAA
url(data:font/truetype;charset=utf-8;base64,AAEAAAAKAIAAAwAgT1MvMhLDANYAAACsAAAATmNtYXAqWoJ7AAAA/AAAA6ZnbHlm1AAAAAAAAAA=) format("truetype");
}

.barcode
{
    font-family:barcode,Arial;
    font-size:15px;
}
user123456789
  • 1,914
  • 7
  • 44
  • 100
  • css is for HTML... but it looks like you just need to define a font and set the font size in your c# code to get the same effect. – Starscream1984 Mar 08 '16 at 17:24
  • @Starscream1984 ok how do I create a new font for the barcode? – user123456789 Mar 09 '16 at 09:09
  • well, where does the one you use in the css come from? – Starscream1984 Mar 09 '16 at 09:17
  • @Starscream1984 its saved in a style sheet – user123456789 Mar 09 '16 at 09:27
  • umm, yeah, but what is the original source for the font? Is there a font file version you can take and import into your .NET code? If it's just Base64 in the css then maybe you could decode it back out to a font file - however - if you have the font file you should be able to import it in your .NET code and apply it to the field for your PDF. – Starscream1984 Mar 09 '16 at 10:19

0 Answers0