【发布时间】:2016-10-24 08:35:55
【问题描述】:
如何使用 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 中删除图像而不会损坏?
提前致谢!
【问题讨论】: