如何从PDF中删除一个间接引用的图像并保留所有其他图像?

时间:2015-02-27 15:36:24

标签: pdf itextsharp

我想解析PDF并通过已知属性找到徽标,当我找到匹配项时,删除该图像,然后复制其他所有内容。

我使用下面的代码替换带有空白图像的图像,以从要在信头上打印的PDF中删除徽标。它用相同大小的白色图像替换图像。有没有办法修改它以实际删除图像(从而节省一些空间等?)。

    private static void Main(string[] args)
    {
        ManipulatePdf(@"C:\in.pdf", @"C:\out.pdf");

        Console.WriteLine("Finished - press a key");
        Console.ReadKey();
    }

    public static void ManipulatePdf(String src, String dest)
    {
        Console.WriteLine("Start");
        PdfReader reader = new PdfReader(src);

        // first read all references and find the one we wish to work on.
        PdfDictionary page = reader.GetPageN(1); // all resources are available to every page (?)
        PdfDictionary resources = page.GetAsDict(PdfName.RESOURCES);
        PdfDictionary xobjects = resources.GetAsDict(PdfName.XOBJECT);

        page = reader.GetPageN(1);
        resources = page.GetAsDict(PdfName.RESOURCES);
        xobjects = resources.GetAsDict(PdfName.XOBJECT);

        foreach (PdfName pdfName in xobjects.Keys)
        {
            PRStream stream = (PRStream) xobjects.GetAsStream(pdfName);

            if (stream.Length > 100000)
            {
                PdfImage image = new PdfImage(MakeBlankImg(), "", null);
                Console.WriteLine("Calling replace stream");
                ReplaceStream(stream, image);
            }
        }

        PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create));
        stamper.Close();
        reader.Close();
    }

    public static iTextSharp.text.Image MakeBlankImg()
    {
        Console.WriteLine("Making small blank image");
        byte[] array;

        using (MemoryStream ms = new MemoryStream())
        {
            //var drawingImage = image.GetDrawingImage();
            using (Bitmap newBi = new Bitmap(1, 1))
            {

                using (Graphics g = Graphics.FromImage(newBi))
                {
                    g.Clear(Color.White);
                    g.Flush();
                }
                newBi.Save(ms, ImageFormat.Jpeg);
            }

            array = ms.ToArray();
        }
        Console.WriteLine("Image array is " + array.Length + " bytes.");

        return iTextSharp.text.Image.GetInstance(array);
    }

    public static void ReplaceStream(PRStream orig, PdfStream stream)
    {
        orig.Clear();
        MemoryStream ms = new MemoryStream();
        stream.WriteContent(ms);
        orig.SetData(ms.ToArray(), false);

        Console.WriteLine("Iterating keys");

        foreach (KeyValuePair<PdfName, PdfObject> keyValuePair in stream)
        {
            Console.WriteLine("Key: " + keyValuePair.Key.ToString());

            orig.Put(keyValuePair.Key, stream.Get(keyValuePair.Key));
        }
    }
}

0 个答案:

没有答案