0

I have to add (register, embed) font to a PDF programmatically. I tried with a lot of utilities like ghostscript or itextsharp but i did not manage to solve the problem.

For example in a situation like this one: enter image description here

I would like to add Courier-Bold and get this situation:

enter image description here

Julian
  • 33,915
  • 22
  • 119
  • 174
user3492925
  • 161
  • 2
  • 14
  • Please, show us what you have tried already. – kiziu Sep 19 '16 at 14:34
  • I tried via ghostscript; the most complete script i used: -dSAFER -dNOPLATFONTS -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/printer -dCompatibilityLevel=1.4 -dSubsetFonts=true -dEmbedAllFonts=true -sFONTPATH=font_path.ttf -sOutputFile=pdf_out_path -f pdf_in_path I also searched in the itextsharp library something usefull to this goal, but i could not find anything. It seems that you can only set the font for a paragraph. – user3492925 Sep 19 '16 at 14:40
  • Maybe this will be of some help: http://stackoverflow.com/questions/4231656/how-do-i-embed-fonts-in-an-existing-pdf – kiziu Sep 19 '16 at 14:44
  • 1
    Since your document doesn't (it seems) use Courier-Bold, instead using CourierNew-Bold, why do you want to add Courier-Bold ? Ghostscript certainly won't add an unused font to a PDF file, what would be the point ? It might help if you posted a URL for an example PDF file to look at, and an explanation of why you want to embed a font in it. – KenS Sep 19 '16 at 15:13
  • I searched on SO and didn't find an exact answer for this and I plan to use itext in the future so I threw something together. Will post soon. – KSib Sep 19 '16 at 15:27

1 Answers1

0

I just created a project using iTextSharp v5.5.9 via NuGet and used the following code:

    const string PdfLocation = @"C:\fakepath\output.pdf";

    static void Main(string[] args)
    {
        using (var pdfDoc = new Document())
        using (var fs = new FileStream(PdfLocation, FileMode.OpenOrCreate))
        using (var writer = PdfWriter.GetInstance(pdfDoc, fs))
        {
            pdfDoc.Open();

            var font = FontFactory.GetFont(FontFactory.COURIER_BOLD);

            // Doesn't use font
            var paragraph = new Paragraph("LINE 1");
            paragraph.Font = font;
            pdfDoc.Add(paragraph);

            // Doesn't use font
            var paragraph2 = new Paragraph();
            paragraph2.Add("LINE 2");
            paragraph2.Font = font;
            pdfDoc.Add(paragraph2);

            // Does use font
            var paragraph3 = new Paragraph();
            paragraph3.Font = font;
            paragraph3.Add("LINE 3"); // This must be done after setting the font
            pdfDoc.Add(paragraph3);

            var cb = writer.DirectContent;

            pdfDoc.Close();
        }
    }

I discovered that you need to set the font first before you write the text. The following code outputs the PDF with the following properties. I didn't get the TrueType requirement out of this, but perhaps this will set you in the right direction.

Where I'm using paragraph and paragraph2 will use the default font which was Helvetica for me because I'm setting the font after I'm setting the text. Order does matter!

The documentation for this certainly needs to be expanded upon...

KSib
  • 893
  • 1
  • 7
  • 24
  • Your bonus doesn't make any difference at all: you only read the pdf into memory, remove the unused objects in memory, and eventually throw away that in-memory representation. The pdf on disk is not changed at all. – mkl Sep 19 '16 at 20:05
  • Ah you're right. I had some code in there that was working. I'll fix it in a second @mkl. I'm going to have to delete the original file to make it work as I don't know if there's a command to do it while creating the PDF. – KSib Sep 19 '16 at 20:13