Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

c# - How do I embed fonts in an existing PDF?

Background:

I have PDF's I am programmatically generating. I need to be able to send the PDF directly to a printer from the server (not through an intermediate application). At the moment I can do all of the above (generate PDF, send to printer), but because the fonts aren't embedded in the PDF the printer is doing font substitution.

Why the fonts aren't embedded when generated:

I am creating PDF's using SQL Reporting Services 2008. There is a known issue with SQL Reporting Services in that it will not embed fonts (unless a series of requirements are met - http://technet.microsoft.com/en-us/library/ms159713%28SQL.100%29.aspx). Don't ask me why, the PDF meets all of MS's listed requirements and the fonts still show up as not embedded - there is no real control over whether the fonts are embedded, so I have accepted that this isn't working and moved on. The suggested workaround from Microsoft (http://blogs.msdn.com/b/donovans/archive/2007/07/20/reporting-services-pdf-renderer-faq.aspx under 'When will Reporting Services do font embedding') is to post process the PDF to manually embed the fonts.

Goal Take an already generated PDF document, programmatically 'open' it and embed the fonts, resave the PDF.

Approach I was pointed towards iTextSharp, but most of the examples are for the Java version and I'm having trouble translating to the iTextSharp version (I can't find any documentation for iTextSharp).

I am working on this post for what I need to do: Itext embed font in a PDF.

However for the life of me, I cannot seem to use the ByteArrayOutputStream object. It can't seem to find it. I've researched and researched but nobody seems to say what class it's in or where I find it so I can include it in the using statements. I've even cracked open Reflector and can't seem to find it anywhere.

This is what I have so far and it compiles etc. etc. (result is my byte[] of the generated PDF).

PdfReader pdf = new PdfReader(result);            
BaseFont unicode = BaseFont.CreateFont("Georgia", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// the next line doesn't work as I need a ByteArrayOutputStream variable to pass in
PdfStamper stamper = new PdfStamper(pdf, MISSINGBYTEARRAYOUTPUTSTREAMVARIABLE);
stamper.AcroFields.SetFieldProperty("test", "textfont", unicode, null); 
stamper.Close();
pdf.Close();

So can anybody either help me with using iTextSharp to embed fonts into a PDF or point me in the right direction?

I'm more than happy to use any other solutions other than iTextSharp to complete this goal, but it needs to be free and able to be used by a business for an internal application (i.e. Affero GPL).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This may not be the answer you are looking for (since you want to get your problems solved programmatically, not by an external tool).

But you can use Ghostscript commandline to embed missing fonts in retrospect to PDFs which have not embedded them:

gs 
  -sFONTPATH=/path/to/fonts:/another/dir/with/more/fonts 
  -o output-pdf-with-embedded-fonts.pdf 
  -sDEVICE=pdfwrite 
  -dPDFSETTINGS=/prepress 
   input-pdf-where-some-fonts-are-not-embedded.pdf

One important thing is that the missing fonts are all available in one of the directories pointed to by the -sFontPath=... switch.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...