Exemple #1
0
        private static void ReplaceImg()
        {
            PdfReader     pdf    = new PdfReader(@"E:\pdf\156281489401000002.pdf");
            PdfStamper    stp    = new PdfStamper(pdf, new FileStream(@"E:\pdf\test.pdf", FileMode.Create));
            PdfWriter     writer = stp.Writer;
            Image         img    = Image.GetInstance(@"E:\pdf\sign.png");
            PdfDictionary pg     = pdf.GetPageN(1);
            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)
                {
                    PdfObject obj = xobj.Get(name);
                    if (obj.IsIndirect())
                    {
                        PdfDictionary tg   = (PdfDictionary)PdfReader.GetPdfObject(obj);
                        PdfName       type = (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
                        if (PdfName.IMAGE.Equals(type))
                        {
                            PdfReader.KillIndirect(obj);
                            Image maskImage = img.ImageMask;
                            if (maskImage != null)
                            {
                                writer.AddDirectImageSimple(maskImage);
                                writer.AddDirectImageSimple(img, (PRIndirectReference)obj);
                                break;
                            }
                        }
                    }
                }
                stp.Close();
            }
        }
Exemple #2
0
        private void btnRemoveImage_Click_1(object sender, EventArgs e)
        {
            var sourceFolder = Path.GetDirectoryName(fileName);

            if (!Directory.Exists(sourceFolder + "\\Output"))
            {
                Directory.CreateDirectory(sourceFolder + "\\Output");
            }

            Stream white = File.OpenRead("White.png");

            iTextSharp.text.Image whiteImg = iTextSharp.text.Image.GetInstance(white);

            var outfileName  = sourceFolder + "\\Output\\" + Path.GetFileName(fileName);
            var outputStream = new FileStream(outfileName, FileMode.Create);

            _pdfStamper = new PdfStamper(_reader, outputStream);
            PdfWriter writer = _pdfStamper.Writer;



            //var obj1 = tvDOM.SelectedNode.Tag as PdfObject;


            try
            {
                PdfDictionary pg  = _reader.GetPageN(1);
                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)
                    {
                        PdfObject obj = xobj.Get(name);
                        if (obj.IsIndirect())
                        {
                            PdfDictionary tg   = (PdfDictionary)PdfReader.GetPdfObject(obj);
                            PdfName       type =
                                (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
                            if (PdfName.IMAGE.Equals(type))
                            {
                                //PdfReader.KillIndirect(obj);
                                writer.AddDirectImageSimple(whiteImg, (PRIndirectReference)obj);
                                //break;
                            }
                        }
                    }
                }

                _reader.RemoveUnusedObjects();
                _pdfStamper.Close();
                if (dffOpenOutputInAdobe.Checked)
                {
                    Process.Start(outfileName);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadLine();
            }
        }
Exemple #3
0
        /// <summary>
        /// Procesa un PDF
        /// </summary>
        private static MemoryStream PDFCompress2(Stream fileStream)
        {
            BinaryReader br = new BinaryReader(fileStream);

            byte[]       byt = br.ReadBytes((int)fileStream.Length);
            MemoryStream ms  = new MemoryStream();

            PdfReader pdf = new PdfReader(byt);

            if (pdf.IsOpenedWithFullPermissions)
            {
                PdfStamper stp    = new PdfStamper(pdf, ms);
                PdfWriter  writer = stp.Writer;
                //
                int page_count = pdf.NumberOfPages;
                for (int i = 1; i <= page_count; i++)
                {
                    PdfDictionary pg   = pdf.GetPageN(i);
                    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)
                        {
                            PdfObject obj = xobj.Get(name);
                            if (obj.IsIndirect())
                            {
                                PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
                                if (tg != null)//Veo que a veces que si se trata varias veces la misma imagen esto se vuelve null
                                {
                                    PdfName type = (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
                                    if (PdfName.IMAGE.Equals(type))
                                    {
                                        int       xrefIdx = ((PRIndirectReference)obj).Number;
                                        PdfObject pdfObj  = pdf.GetPdfObject(xrefIdx);
                                        PdfStream str     = (PdfStream)pdfObj;



                                        string filter = string.Empty;
                                        if (tg.Get(PdfName.FILTER) != null)
                                        {
                                            filter = tg.Get(PdfName.FILTER).ToString();
                                        }
                                        else
                                        {
                                        }

                                        if (filter.Contains("/DCTDecode")) //Unas veces es "[/DCTDecode]" y otras "/DCTDecode"
                                        {
                                            try
                                            {
                                                iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance((PRIndirectReference)obj);
                                                //byte[] bytes = PdfReader.GetStreamBytesRaw((PRStream)str);
                                                //System.Drawing.Image imgOriginal = System.Drawing.Image.FromStream(new MemoryStream(bytes));
                                                PdfImageObject pdfImage = new PdfImageObject((PRStream)str);

                                                using (System.Drawing.Image imgOriginal = pdfImage.GetDrawingImage())
                                                    using (System.Drawing.Image img2 = Globals.ResizeImage(imgOriginal))
                                                    {
                                                        if (img2.Width != imgOriginal.Width || img2.Height != imgOriginal.Height)
                                                        {
                                                            //img2 = Resize(img2, maxImageWidth, maxImageHeight);
                                                            var stream = new System.IO.MemoryStream();
                                                            img2.Save(stream, ImageFormat.Jpeg);
                                                            stream.Position = 0;
                                                            PdfReader.KillIndirect(obj);
                                                            img = iTextSharp.text.Image.GetInstance(stream);

                                                            writer.AddDirectImageSimple(img, (PRIndirectReference)obj);
                                                        }
                                                    }
                                            }
                                            catch (Exception ex)
                                            {
                                                throw ex;
                                            }
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                stp.Writer.CloseStream = false;
                stp.FormFlattening     = true;
                stp.Close();
                pdf.Close();
                //return ms;
            }
            else
            {
                ms = null;
            }
            return(ms);
        }