网上有几个使用 itextsharp 删除或调整图像大小的示例,但我无法找到删除内联图像的示例。
我正在使用以下代码删除 XObject 图像:
PdfWriter writer = st.Writer;
PdfDictionary pg = reader.GetPageN(1);
PdfDictionary res = (PdfDictionary)PdfReader.GetPdfObject(pg.Get(PdfName.RESOURCES));
PdfDictionary xobj = (PdfDictionary)PdfReader.GetPdfObject(res.Get(PdfName.XOBJECT));
if (xobj != null)
{
foreach (PdfName name in xobj.Keys)
{
PdfObject obj = xobj.Get(name);
if (obj.IsDictionary())
{
PdfDictionary tg = (PdfDictionary)PdfReader.GetPdfObject(obj);
PdfName type = (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
//PdfName type = (PdfName)PdfReader.GetPdfObject(tg.Get(PdfName.SUBTYPE));
if (PdfName.IMAGE.Equals(type))
{
int xrefIdx = ((PRIndirectReference)obj).Number;
PdfObject pdfObj = reader.GetPdfObject(xrefIdx);
PdfStream str = (PdfStream)(pdfObj);
byte[] bytes = PdfReader.GetStreamBytesRaw((PRStream)str);
iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance((PRIndirectReference)obj);
string filter = tg.Get(PdfName.FILTER).ToString();
if (filter == "/DCTDecode")
{
PdfReader.KillIndirect(obj);
Stream stBrasao2 = File.OpenRead(pasta_recurso + "brasao.jpg");
iTextSharp.text.Image img2 = iTextSharp.text.Image.GetInstance(stBrasao2);
writer.AddDirectImageSimple(img2, (PRIndirectReference)obj);
break;
}
}
}
}
}
有没有办法调整它以删除内联图像而不是 XObject 图像?
谢谢。