简体   繁体   English

使用iTextSharp更改PDF的字体

[英]Changing font of PDF using iTextSharp

How can I change the font of a existing PDF file with iTextSharp in C#? 如何在C#中使用iTextSharp更改现有PDF文件的字体?

I want to change the whole documents font into one eg Arial 我想将整个文档字体更改为一个,例如Arial

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. 以下代码将打开一个现有的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();

        } 

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

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