使用iTextSharp更改PDF文档的字体。

3

如何使用C#的iTextSharp更改现有PDF文件的字体?

我想将整个文档的字体更改为一种,例如Arial。


你的期望是什么?仅仅改变所有文本元素的字体就足够了吗?你意识到,如果之前使用的字体与新字体具有不同的度量标准,则结果可能看起来非常难看,字母可能会重叠或相距极远,文本行可能会超出右侧文档边界等等...即使这种丑陋的解决方案也并非总是可行的,因为字符映射信息可能没有包含在文档中的字体信息中,在自定义编码的情况下可能无法知道哪个字符是哪个。 - mkl
1个回答

7

最终我解决了这个问题。以下代码将打开现有的PDF文件,并按照我的期望将所有字体更改为“Braille”。

 private static void ChangeFont()
        {


            string strFile = @"E:\\xyz.pdf";
            string OutputFile = @"E:\\xyz1.pdf";
            PdfReader pdfReader = new PdfReader(strFile);

            //Get first page,Generally we get font information on first page,however we can loop throw pages e.g for(int i=0;i<=pdfReader.NumberOfPages;i++)
                PdfDictionary cpage = pdfReader.GetPageN(1);
                if (cpage == null)
                    return;
                PdfDictionary dictFonts = cpage.GetAsDict(PdfName.RESOURCES).GetAsDict(PdfName.FONT);
                if (dictFonts != null)
                {
                    foreach (var font in dictFonts)
                    {
                        var dictFontInfo = dictFonts.GetAsDict(font.Key);

                        if (dictFontInfo != null)
                        {
                            foreach (var f in dictFontInfo)
                            {
                                //Get the font name-optional code
                                var baseFont = dictFontInfo.Get(PdfName.BASEFONT);
                                string strFontName = System.Text.Encoding.ASCII.GetString(baseFont.GetBytes(), 0,
                                                                                          baseFont.Length);
                                //


                                //Remove the current font
                                dictFontInfo.Remove(PdfName.BASEFONT);
                                //Set new font eg. Braille, Areal etc
                                dictFontInfo.Put(PdfName.BASEFONT, new PdfString("Braille"));
                                break;

                            }
                        }


                    }

            }

            //Now create a new document with updated font
            using (FileStream FS = new FileStream(OutputFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                using (Document Doc = new Document())
                {
                    using (PdfCopy writer = new PdfCopy(Doc, FS))
                    {
                        Doc.Open();
                        for (int j = 1; j <= pdfReader.NumberOfPages; j++)
                        {
                            writer.AddPage(writer.GetImportedPage(pdfReader, j));
                        }
                        Doc.Close();
                    }
                }
            }
            pdfReader.Close();

        } 

谢谢。如何使用上述代码更改PDF的字体颜色? - Narasappa
你应该删除 foreach (var f in dictFontInfo) - isHuman
这段代码很混乱,有迭代和声明未被使用。而且代码无法正常工作。它无法更改PDF中的基本字体名称。 - Tomas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接