简体   繁体   中英

How can I remove image properties such as local path that Adobe Illustrator has been embedded to PDF file?

I'm trying to replace image in PDF file using iTextSharp(not a java version). It works fine but there only the problem is when I open that PDF with Adobe Illustrator it's always opened with the old hard link. It means Abode Illustrator always view the old image before replace. And a little weird here that it view fine with Adobe Reader(the replaced image can be viewed).

This is the snip code that I've tried:

public static void ReplaceImage(string pdfIn, string imagePath, string pdfOut)
        {
            PdfReader reader = new PdfReader(pdfIn);
            PdfStamper stamper = new PdfStamper(reader, new FileStream(pdfOut, FileMode.Create));

            PdfWriter writer = stamper.Writer;
            Image img = Image.GetInstance(SysDrawing.Image.FromFile(imagePath), ImageFormat.Tiff);

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

            PdfDictionary xobject = resources.GetAsDict(PdfName.XOBJECT);
            PdfDictionary properties = resources.GetAsDict(PdfName.PROPERTIES);
            PdfDictionary procset = resources.GetAsDict(PdfName.PROCSET);

            if (xobject != null)
            {
                List<PdfName> imgs = new List<PdfName>();
                foreach (var ele in xobject.Keys)
                {
                    PdfIndirectReference iref = xobject.GetAsIndirectObject(ele);

                    imgs.Add(ele);

                    if (iref.IsIndirect())
                    {
                        try
                        {
                            PdfDictionary pg = (PdfDictionary)PdfReader.GetPdfObject(iref);
                            if (pg != null)
                            {
                                PdfReader.KillIndirect(iref);
                                if (PdfName.IMAGE.Equals(SubType))
                                {
                                    if (img.ImageMask != null)
                                        writer.AddDirectImageSimple(img.ImageMask);
                                    writer.AddDirectImageSimple(img, iref);
                                }
                            }
                            else
                            {
                                PdfReader.KillIndirect(iref);
                                writer.AddDirectImageSimple(img, iref);
                            }
                        }
                        catch { 
                            continue; 
                        }

                    }

                }
            }

            //stamper.SetFullCompression();
            stamper.Close();
            stamper.Dispose();

            reader.RemoveUnusedObjects();
            reader.RemoveAnnotations();
            reader.RemoveFields();
            reader.Close();
            reader.Dispose();
        }

Any answer would be appreciated!

Your PDF contains two different documents: one described using PDF syntax and one described using Adobe Illustrator syntax. These two different documents should look identical, but as you have changed the PDF version of the document, they no longer do.

You perceive the document as only one document, because the AI document is stored inside the PDF document. In another question on SO, mkl explains the mechanism: Insert hidden digest in pdf using iText library

In his answer, mkl explains how to add hidden data (in this case a hidden digest, in your case the document in IA format) into a PDF.

You can remove this second document like this:

PdfDictionary catalog = reader.getCatalog();
catalog.remove(PdfName.PIECEINFO);

Of course, this throws away the Adobe Illustrator entirely, so you won't be able to edit the PDF in Adobe Illustrator anymore. If you want the image to change in the AI syntax, you need a library that is able to change AI syntax (and I don't know of any such library).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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