2013-04-25 142 views
0

有几个使用网络上的itextsharp去除或调整图像大小的示例,但我无法找到删除嵌入图像的示例。使用itextsharp从pdf中删除嵌入图像

I'm使用下面的代码删除X对象的图像:

  PdfWriter writer = st.Writer; 
      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.IsDictionary()) 
        { 
         PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj); 
         PdfName type = (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE)); 
         //PdfName type = (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE)); 



         if (PdfName.IMAGE.Equals(type)) 
         { 
          int xrefIdx = ((PRIndirectReference)obj).Number; 
          PdfObject pdfObj = reader.GetPdfObject(xrefIdx); 
          PdfStream str = (PdfStream)(pdfObj); 
          byte[] bytes = PdfReader.GetStreamBytesRaw((PRStream)str); 
          iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance((PRIndirectReference)obj); 

          string filter = tg.Get(PdfName.FILTER).ToString(); 

          if (filter == "/DCTDecode") 
          { 
           PdfReader.KillIndirect(obj); 
           Stream stBrasao2 = File.OpenRead(pasta_recurso + "brasao.jpg"); 
           iTextSharp.text.Image img2 = iTextSharp.text.Image.GetInstance(stBrasao2); 


           writer.AddDirectImageSimple(img2, (PRIndirectReference)obj); 
           break; 
          } 
         } 
        } 
       } 
      } 

有什么办法,以适应这种去除内嵌图像,而不是X对象的图像?

谢谢。

+0

你的问题是什么?这不行吗? – 2013-04-26 01:58:21

+0

看起来他想要删除内嵌图像,并不知道如何做到这一点。这个任务并不简单,因为人们必须在每一个内容流中搜索每一个适用的xobject流中的内容... – mkl 2013-04-26 04:16:33

+0

在[parallel post](http://itext-general.2136553.n4.nabble.com) /Remove-Inline-Image-tt4658130.html),@ uacaman对itext-questions邮件列表进行了更多解释。 – mkl 2013-04-26 09:35:04

回答

0

该代码不会删除内嵌图像,iText这样的任务是按照Bruno Lowagie在评论中指出的那样完成的。最后,我的解决方案是在IText之前用PDFSharp解析pdf。我正在使用PDFSharp读取pdf流读取字节,从图像中删除字节,然后为iText输出一个文件。

+0

我有同样的需要...... Bruno Lowagie也向我解释了XObjects图像和Inline图像之间的区别......根据他的解释,我找到了你的帖子......如果有机会你看到我的评论,会你介意分享你的代码? 10.000thx(如果你来布鲁塞尔来拜访我的话,还有一些Biers或巧克力;)! – 2014-07-12 11:27:10

+0

有关信息的目的,这里是我的“解决方案”:http://stackoverflow.com/questions/24725730/issue-when-trying-to-remove-inline-images-from-pdf-with-itextsharp – 2014-07-13 20:51:38

+0

对不起,迟到了回复,我没有访问代码了。 – uacaman 2015-05-22 12:47:44