Пример #1
0
        /// <summary>
        /// 替换PDF中的图片
        /// </summary>
        /// <param name="src">pdf文件流</param>
        /// <param name="distinguishMethod">识别需要被替换图片的方法</param>
        /// <param name="replaceToImg">替换成这个图片</param>
        /// <returns></returns>
        private static MemoryStream ReplaceImage(Stream src, DistinguishImage distinguishMethod, Image replaceToImg)
        {
            PdfReader       reader2         = new PdfReader(src);
            PdfMemoryStream outMemoryStream = new PdfMemoryStream();

            outMemoryStream.AllowClose = false;

            using (PdfStamper stamper = new PdfStamper(reader2, outMemoryStream))
            {
                int pageCount2 = reader2.NumberOfPages;
                for (int i = 1; i <= pageCount2; i++)
                {
                    //Get the page
                    var       page = reader2.GetPageN(i);
                    PdfObject obj  = FindImageInPDFDictionary(page, distinguishMethod);
                    if (obj != null)
                    {
                        PdfReader.KillIndirect(obj);//移除老图片
                        //Image img = Image.GetInstance(replaceToImg, BaseColor.WHITE, true);
                        Image img       = Image.GetInstance(replaceToImg);
                        Image maskImage = img.ImageMask;
                        if (maskImage != null)
                        {
                            stamper.Writer.AddDirectImageSimple(maskImage);
                            stamper.Writer.AddDirectImageSimple(img, (PRIndirectReference)obj);
                        }
                    }
                }
            }

            outMemoryStream.Position = 0;
            return(outMemoryStream);
        }
Пример #2
0
        //在pdf页面中 找到logo图片
        private static PdfObject FindImageInPDFDictionary(PdfDictionary pg, DistinguishImage distinguishMethod)
        {
            PdfDictionary res  = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
            PdfDictionary xobj = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));

            if (xobj != null)
            {
                foreach (PdfName name in xobj.Keys)
                {
                    Console.WriteLine(name.ToString());
                    PdfObject obj = xobj.Get(name);
                    if (obj.IsIndirect())
                    {
                        PdfDictionary tg   = (PdfDictionary)PdfReader.GetPdfObject(obj);
                        PdfName       type = (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
                        //image at the root of the pdf
                        if (PdfName.IMAGE.Equals(type))
                        {
                            if (distinguishMethod(tg) == true)
                            {
                                return(obj);
                            }
                            else
                            {
                                continue;//继续找
                            }
                        }// image inside a form
                        else if (PdfName.FORM.Equals(type))
                        {
                            return(FindImageInPDFDictionary(tg, distinguishMethod));
                        } //image inside a group
                        else if (PdfName.GROUP.Equals(type))
                        {
                            return(FindImageInPDFDictionary(tg, distinguishMethod));
                        }
                    }
                }
            }

            return(null);
        }