简体   繁体   English

如何在现有 PDF 中嵌入字体?

[英]How do I embed fonts in an existing PDF?

Background:背景:

I have PDF's I am programmatically generating.我有我以编程方式生成的 PDF。 I need to be able to send the PDF directly to a printer from the server (not through an intermediate application).我需要能够将 PDF 直接从服务器发送到打印机(而不是通过中间应用程序)。 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.目前我可以执行上述所有操作(生成 PDF,发送到打印机),但由于字体未嵌入 PDF 中,因此打印机正在进行字体替换。

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 ).我正在使用 SQL Reporting Services 2008 创建 PDF。SQL Reporting Services 存在一个已知问题,即它不会嵌入字体(除非满足一系列要求 - 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.不要问我为什么,PDF 满足 MS 列出的所有要求,并且字体仍然显示为未嵌入 - 无法真正控制是否嵌入字体,所以我接受了这不起作用并继续前进. 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. Microsoft 建议的解决方法( http://blogs.msdn.com/b/donovans/archive/2007/07/20/reporting-services-pdf-renderer-faq.aspx在“报告服务何时进行字体嵌入”下)是对PDF进行后期处理以手动嵌入字体。

Goal Take an already generated PDF document, programmatically 'open' it and embed the fonts, resave the PDF.目标获取一个已经生成的 PDF 文档,以编程方式“打开”它并嵌入字体,重新保存 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).方法我被指向 iTextSharp,但大多数示例都是针对 Java 版本的,我在翻译到 iTextSharp 版本时遇到了麻烦(我找不到 iTextSharp 的任何文档)。

I am working on this post for what I need to do: Itext embed font in a PDF .我正在写这篇文章,因为我需要做的是: Itext embed font in a PDF

However for the life of me, I cannot seem to use the ByteArrayOutputStream object.但是,对于我的一生,我似乎无法使用 ByteArrayOutputStream 对象。 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.我已经研究和研究过,但似乎没有人说它在哪个类中或我在哪里找到它,因此我可以将它包含在 using 语句中。 I've even cracked open Reflector and can't seem to find it anywhere.我什至打开了 Reflector,但似乎在任何地方都找不到它。

This is what I have so far and it compiles etc. etc. (result is my byte[] of the generated PDF).这是我到目前为止所拥有的,它可以编译等等(结果是我生成的 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?那么任何人都可以帮助我使用 iTextSharp 将字体嵌入到 PDF 中或为我指明正确的方向吗?

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 (ie Affero GPL).我非常乐意使用 iTextSharp 以外的任何其他解决方案来完成这个目标,但它需要是免费的,并且能够被企业用于内部应用程序(即 Affero GPL)。

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:但是您可以使用 Ghostscript 命令行将丢失的字体嵌入到没有嵌入它们的 PDF 中:

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.一件重要的事情是缺少的字体都可以在-sFontPath=...开关指向的目录之一中使用。

Besides Ghostscript, it is also possible to use Poppler and Cairo.除了 Ghostscript,还可以使用Poppler和 Cairo。 There is a command pdftocairo from Poppler that converts PDF to PDF via pdftocairo -pdf input.pdf output.pdf .有一个来自 Poppler 的pdftocairo命令,它通过pdftocairo -pdf input.pdf output.pdf将 PDF 转换为 PDF。 It also considers font substitutions set in a Fontconfig configuration file.它还考虑在Fontconfig配置文件中设置的字体替换。 This is very helpful if you do not have all fonts on your system that are referenced in a PDF file, but know which other font you have installed is a good-looking replacement.如果您的系统上没有 PDF 文件中引用的所有字体,但知道您安装的其他字体是一个好看的替代品,这将非常有用。 After processing, the substitution font is embedded.处理后,嵌入替换字体。

I had this problem today with an existing PDF I uploaded to lulu.com to make a printed copy.我今天在上传到 lulu.com 以制作打印副本的现有 PDF 中遇到了这个问题。 It was rejected for not having all fonts embedded.它因未嵌入所有字体而被拒绝。

I found that if I opened it in Acrobat X and Saved out as postscript .ps file, then when I double clicked this .ps file in File Explorer, it opened in Acrobat X Distiller, and this automatically created a new PDF file with all fonts embedded!我发现如果我在 Acrobat X 中打开它并保存为 postscript .ps 文件,那么当我在文件资源管理器中双击这个 .ps 文件时,它会在 Acrobat X Distiller 中打开,这会自动创建一个包含所有字体的新 PDF 文件嵌入!

Naturally this would mean you must have all the fonts needed on your computer.当然,这意味着您的计算机上必须拥有所有需要的字体。 Otherwise a program like InFix can make font substitutions.否则像 InFix 这样的程序可以进行字体替换。

I had this problem on a Mac with a PDF I was submitting to IEEE.我在 Mac 上遇到了这个问题,我正在提交给 IEEE 的 PDF。 Using Adobe Reader and Preview, I was able to get around this.使用 Adob​​e Reader 和 Preview,我能够解决这个问题。 I think any pdf printer might work in place of Preview if you are on a PC.我认为如果您在 PC 上,任何 pdf 打印机都可以代替预览。

Here are the steps I took.这是我采取的步骤。 You can individually fix each figure, or fix the whole document.您可以单独修复每个图形,或修复整个文档。

  1. Open at pdf file using Adobe Reader.使用 Adob​​e Reader 打开 pdf 文件。

  2. Right click on image, and click “Document Properties.”右键单击图像,然后单击“文档属性”。

  3. Click “Fonts.”点击“字体”。 Check to see if the font isn't embedded.检查字体是否未嵌入。 Should say “Courier” or other font name.应该说“Courier”或其他字体名称。

  4. If your pdf isn't a standard page size, click on “Description” and look at the page size.如果您的 pdf 不是标准页面大小,请单击“说明”并查看页面大小。 Write this down.把这个写下来。 Ex.前任。 19.4 x 5.22 in. 19.4 x 5.22 英寸

  5. Open the pdf up in Preview.在预览中打开 pdf。 Go to File->Print.转到文件-> 打印。 If using a pdf that isn't a standard page size, click on Paper Size and choose custom.如果使用的 pdf 不是标准页面大小,请单击纸张大小并选择自定义。 You will need to create a custom page size that is equal to the one you wrote down in step 4. Don't forget to zero the margins to 0 for all sides.您将需要创建一个与您在第 4 步中记下的页面大小相同的自定义页面大小。不要忘记将所有边的边距都归零。 After doing that, you'll need to set the scale of the print in the print dialog to 100%.完成此操作后,您需要将打印对话框中的打印比例设置为 100%。

  6. In the lower left of the print dialog (in Preview on a Mac), click “PDF” to print the PDF to a new PDF.在打印对话框的左下方(在 Mac 上的预览中),单击“PDF”将 PDF 打印为新的 PDF。 Select the destination and print.选择目的地并打印。

  7. Open the new pdf up in Adobe Reader and verify that the fonts are now embedded.在 Adob​​e Reader 中打开新的 pdf 并验证字体现在已嵌入。

I hope this helps.我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM