3

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

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

4

1 回答 1

7

最后我解决了这个问题。下面的代码将打开一个现有的 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();

        } 
于 2013-03-06T07:42:36.407 回答