簡體   English   中英

如何使用PDFSharp從pdf刪除圖像?

[英]How to remove images from pdf using PDFSharp?

如何使用PDFSharp從現有的pdf中刪除圖像(全部)?

我嘗試了這段代碼:

public static PdfDocument RemoveImages(PdfDocument pdf)
{
    foreach (PdfPage page in pdf.Pages)
    {
        PdfDictionary resource = page.Elements.GetDictionary("/Resources");
        if (resource != null)
        {
            PdfDictionary objects = resource.Elements.GetDictionary("/XObject");
            if (objects != null)
            {
                foreach (string itemKey in objects.Elements.Keys)
                {
                    PdfItem item = objects.Elements[itemKey];
                    PdfReference reference = item as PdfReference;                          
                    if (reference != null)
                    {
                        PdfDictionary xObject = reference.Value as PdfDictionary;
                        if (xObject != null && xObject.Elements.GetString("/Subtype") == "/Image")
                        {
                            pdf.Internals.RemoveObject((PdfObject)reference.Value); // remove image from internal document table
                            objects.Elements.Remove(itemKey); // remove image from page resource
                        }
                    }
                }
            }
        }
    }

    return pdf;
}

但是在Acrobat Reader中打開該文件時,此代碼會導致pdf損壞。

如何使用PDFSharp從現有pdf中刪除圖像而不會造成損壞?

提前致謝!

您刪除圖像,但不要更改繪制圖像的頁面的內容。 Adobe Reader嘗試繪制不再存在的圖像。 這就是您的文件損壞的原因。

可能的解決方案(只是猜測,並非我的專業領域):

  • 用透明圖像替換圖像,讓頁面繪制透明圖像。
  • 解析頁面內容,並刪除所有繪制已刪除圖像的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM