一段代码,是为了遍历每一页中的图片
PdfDictionary resources = pageDict.GetAsDictionary(PdfName.Resources);
PdfDictionary xObjects = resources.GetAsDictionary(PdfName.XObject);
if (xObjects == null)
return;
var keySet = xObjects.KeySet();
foreach (var pdfName in keySet.ToList())
{
PdfStream stream = xObjects.GetAsStream(pdfName);
PdfObject subtype = stream.Get(PdfName.Subtype);
结果对某个pdf进行处理的时候,在第三页出现了运行时的报错。原因是stream.Get的时候,stream对象的内部成员map是null,因此 map.Get(key) 必然不可运行!但是为什么会出现这种情况呢?先跟踪一下 xObjects.GetAsStream(pdfName) 是怎么处理的
public virtual PdfObject Get(PdfName key, bool asDirect) {
if (!asDirect) {
return map.Get(key);
}
else {
PdfObject obj = map.Get(key);
if (obj != null && obj.GetObjectType() == INDIRECT_REFERENCE) {
return ((PdfIndirectReference)obj).GetRefersTo(true);
}
else {
return obj;
}
}
}
每一页的xobject都是Indirect的object,因此还需要从整个pdf文档中找到这个object的真正定义,通过 PdfIndirectReference::GetRefersTo
public virtual PdfObject GetRefersTo(bool recursively) {
if (!recursively) {
if (refersTo == null && !CheckState(FLUSHED) && !CheckState(MODIFIED) && !CheckState(FREE) && GetReader()
!= null) {
refersTo = GetReader().ReadObject(this);
}
return refersTo;
}
else {
PdfObject currentRefersTo = GetRefersTo(false);
for (int i = 0; i < LENGTH_OF_INDIRECTS_CHAIN; i++) {
if (currentRefersTo is iText.Kernel.Pdf.PdfIndirectReference) {
currentRefersTo = ((iText.Kernel.Pdf.PdfIndirectReference)currentRefersTo).GetRefersTo(false);
}
else {
break;
}
}
return currentRefersTo;
}
}
一般每一页的新对象,其 refersTo == null,需要通过 GetReader().ReadObject(this) 从整个文档中找到真正定义。但是在出错的一页,refersTo 已经不为空,指向一个 {10 0 R Flushed;}。我们再跟踪一下,对第一页的对象,GetReader().ReadObject(this) 是怎么处理的
PdfReader::ReadObject
private PdfObject ReadObject(PdfIndirectReference reference, bool fixXref) {
if (reference == null) {
return null;
}
if (reference.refersTo != null) {
return reference.refersTo;
}
try {
currentIndirectReference = reference;
if (reference.GetObjStreamNumber() > 0) {
PdfStream objectStream = (PdfStream)pdfDocument.GetXref().Get(reference.GetObjStreamNumber()).GetRefersTo(
false);
ReadObjectStream(objectStream);
return reference.refersTo;
}
else {
if (reference.GetOffset() > 0) {
PdfObject @object;
try {
tokens.Seek(reference.GetOffset());
tokens.NextValidToken();
if (tokens.GetTokenType() != PdfTokenizer.TokenType.Obj || tokens.GetObjNr() != reference.GetObjNumber() ||
tokens.GetGenNr() != reference.GetGenNumber()) {
tokens.ThrowError(KernelExceptionMessageConstant.INVALID_OFFSET_FOR_THIS_OBJECT, reference.ToString());
}
@object = ReadObject(false);
}
会根据对象的offset,从原始文档中读出对象。正好第一页中要读的一个对象就是10,它读出来的状态是{10 0 R},是没有Flushed在后面的!那什么时候变成flushed的呢?继续跟踪程序,发现是在每一页处理结束后,程序都会调用一次 page.Flush(true);,对象10就变成了 {10 0 R Flushed;}。而 Flush 函数的说明是
Flushes page dictionary, its content streams, annotations and thumb image. If <c>flushResourcesContentStreams</c> is true, all content streams that are rendered on this page (like FormXObjects, annotation appearance streams, patterns) and also all images associated with this page will also be flushed.
这说明了,由于这个图片同时被第1页和第3页使用了,而因为调用Flush的时候,后面的参数加了true,因此处理完第一页的时候被写出去了。继续跟踪一下,为什么Flush之后,stream对象的map变空了
PdfPage类
private void FlushResourcesContentStreams(PdfDictionary resources) {
if (resources != null && !resources.IsFlushed()) {
FlushWithResources(resources.GetAsDictionary(PdfName.XObject));
FlushWithResources(resources.GetAsDictionary(PdfName.Pattern));
FlushWithResources(resources.GetAsDictionary(PdfName.Shading));
}
}
private void FlushWithResources(PdfDictionary objsCollection) {
if (objsCollection == null || objsCollection.IsFlushed()) {
return;
}
foreach (PdfObject obj in objsCollection.Values()) {
if (obj.IsFlushed()) {
continue;
}
FlushResourcesContentStreams(((PdfDictionary)obj).GetAsDictionary(PdfName.Resources));
FlushMustBeIndirectObject(obj);
}
}
private void FlushMustBeIndirectObject(PdfObject obj) {
// TODO DEVSIX-744
obj.MakeIndirect(GetDocument()).Flush();
}
PdfObject类
public void Flush(bool canBeInObjStm) {
try {
PdfDocument document = GetIndirectReference().GetDocument();
if (document != null) {
if (document.IsAppendMode() && !IsModified()) {
ILogger logger = ITextLogManager.GetLogger(typeof(PdfObject));
logger.LogInformation(iText.IO.Logs.IoLogMessageConstant.PDF_OBJECT_FLUSHING_NOT_PERFORMED);
return;
}
document.CheckIsoConformance(this, IsoKey.PDF_OBJECT);
document.FlushObject(this, canBeInObjStm && GetObjectType() != STREAM && GetObjectType() != INDIRECT_REFERENCE
&& GetIndirectReference().GetGenNumber() == 0);
}
}
PdfWriter类
protected internal virtual void FlushObject(PdfObject pdfObject, bool canBeInObjStm) {
switch (pdfObject.GetObjectType()) {
case PdfObject.STREAM:
case PdfObject.DICTIONARY: {
PdfDictionary dictionary = ((PdfDictionary)pdfObject);
MarkDictionaryContentToFlush(dictionary);
dictionary.ReleaseContent();
break;
}
PdfDictionary类
protected internal virtual void ReleaseContent() {
map = null;
}