[英]How to extract FlateDecoded Images from PDF with PDFSharp
[英]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.