【问题标题】:Changing font of PDF using iTextSharp使用 iTextSharp 更改 PDF 的字体
【发布时间】:2013-03-05 13:51:08
【问题描述】:

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

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

【问题讨论】:

  • 您的期望是什么?更改所有文本元素的字体就足够了吗?您知道,如果以前使用的字体与新字体的度量不同,字母可能重叠或彼此相距极远,文本行可能超出正确的文档边框等,结果可能看起来非常难看......甚至这种丑陋的解决方案并不总是可行的,因为字符映射信息可能不包含在文档的字体信息中,在自定义编码的情况下,可能无法知道哪个字符是哪个。

标签: c# pdf itextsharp


【解决方案1】:

最后我解决了这个问题。 以下代码将打开一个现有的 Pdf 文件,并按照我的预期将其所有字体更改为“盲文”。

 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 的字体颜色
  • 你应该删除foreach (var f in dictFontInfo)
  • 这段代码是乱七八糟的,没有使用的迭代和声明。代码也不起作用。它不会更改 PDF 中的基本字体名称。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-03-28
  • 2018-05-19
  • 1970-01-01
  • 1970-01-01
  • 2016-12-16
  • 1970-01-01
  • 2015-06-13
相关资源
最近更新 更多