简体   繁体   English

使用itextsharp从pdf中删除嵌入式图像

[英]Using itextsharp to remove inline images from pdf

There are several examples of removing or resizeing images using itextsharp on the net, but i'm unable to find exemples of removing inline images. 在网上有几个使用itextsharp删除或调整图像大小的示例,但是我找不到删除内联图像的示例。

I´m using the following code to remove XObject images: 我正在使用以下代码删除XObject图像:

         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;
                            }
                        }
                    }
                }
            }

Is there any way to adapt this to remove inline images rather than XObject images? 有什么方法可以使它删除内联图像而不是XObject图像?

Thanks. 谢谢。

The code wont remove inline images, with iText such task is done as Bruno Lowagie pointed out in the comments. 该代码不会删除嵌入式图像,使用iText可以完成此任务,如注释中的Bruno Lowagie所指出的那样。 In the end my solution was to parse the pdf with PDFSharp before IText. 最后,我的解决方案是在IText之前使用PDFSharp解析pdf。 I´m using PDFSharp to read the pdf stream read the bytes, remove the bytes from image and then output a file for iText. 我正在使用PDFSharp读取pdf流,读取字节,从图像中删除字节,然后为iText输出文件。

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

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