简体   繁体   English

如何删除图像属性,例如Adobe Illustrator已嵌入到PDF文件的本地路径?

[英]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). 我正在尝试使用iTextSharp(不是Java版本)替换PDF文件中的图像。 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. 它工作正常,但是唯一的问题是,当我使用Adobe Illustrator打开该PDF时,始终使用旧的硬链接打开它。 It means Abode Illustrator always view the old image before replace. 这意味着Abode Illustrator始终在替换之前查看旧图像。 And a little weird here that it view fine with Adobe Reader(the replaced image can be viewed). 这里有点奇怪,它可以使用Adobe Reader很好地查看(可以查看替换的图像)。

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. 您的PDF包含两个不同的文档:一个使用PDF语法描述,另一个使用Adobe Illustrator语法描述。 These two different documents should look identical, but as you have changed the PDF version of the document, they no longer do. 这两个不同的文档看起来应该相同,但是当您更改了该文档的PDF版本时,它们不再起作用。

You perceive the document as only one document, because the AI document is stored inside the PDF document. 您将文档视为一个文档,因为AI文档存储在PDF文档中。 In another question on SO, mkl explains the mechanism: Insert hidden digest in pdf using iText library 在SO的另一个问题中,mkl解释了该机制: 使用iText库在pdf中插入隐藏的摘要

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. 在他的回答中,mkl解释了如何将隐藏数据(在这种情况下为隐藏摘要,在您情况下为IA格式的文档)添加到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. 当然,这将完全丢弃Adobe Illustrator,因此您将无法再在Adobe Illustrator中编辑PDF。 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). 如果您希望图像更改AI语法,则需要一个能够更改AI语法的库(我不知道任何此类库)。

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

相关问题 如何使用以及我可以在 Adobe Illustrator api C# 中找到导出参数的位置 - how to use and where i can find export params in Adobe Illustrator api C# Adobe Reader无法打开“ StreamTest5.pdf”,因为它不是受支持的文件或文件已损坏错误 - Adobe Reader could not open 'StreamTest5.pdf' because it is either not a supported file or file has been damaged error 如何检查PDF文件是否使用嵌入字体? - How can I check if a PDF file is using embedded fonts? 我如何理解URL中的图像是否已更改? - How can I understand if Image from URL has been changed? 如何删除 <img> 如果没有从数据库提交文件路径,则标记 - How to remove an <img> tag if no file path has been submitted from database 如何在现有的pdf文件中将图像设置为pdf字段? - How can i set an image to a pdf field in existing pdf file? 如何删除已在webbrowser控件中导航的文件? - How can i delete the file that has been navigated in a webbrowser control? 如何查看远程Web服务器上的文件是否已更新? - How can I see if a file on a remote webserver has been updated? 在 SftpClient 中,如何检查文件是否已在目录中下载? - In SftpClient how can i check if the file has been donwlonded in directory or not? 在应用资源文件后,如何更改Control的属性? - How could I change the properties of a Control after the resource file has been applied?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM