使用iTextSharp更改PDF的字体 [英] Changing font of PDF using iTextSharp

查看:186
本文介绍了使用iTextSharp更改PDF的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I change the font of a existing PDF file with iTextSharp in C#?

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

I want to change the whole documents font into one e.g. Arial

推荐答案

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

Finally I solved the problem. The following code will open an existing Pdf file and will change all its font to 'Braille' as per my expectation.

 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();

        } 

这篇关于使用iTextSharp更改PDF的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆