aDContext->BeginDocument();
NS_ENSURE_SUCCESS( aDContext->BeginDocument(), NS_ERROR_FAILURE );
DocumentViewerImpl::PrintContent(nsIWebShell *      aParent,                                 nsIDeviceContext * aDContext){  NS_ENSURE_ARG_POINTER(aParent);  NS_ENSURE_ARG_POINTER(aDContext);  nsCOMPtr<nsIStyleSet>       ss;  nsCOMPtr<nsIViewManager>    vm;  PRInt32                     width, height;  nsIView                     *view;  nsresult                    rv;  PRInt32                     count,i;  nsCOMPtr<nsIContentViewer>  viewer;  nsCOMPtr<nsIDocShellTreeNode> parentAsNode(do_QueryInterface(aParent));  // See if if the incoming doc is the root document  nsCOMPtr<nsIDocShellTreeItem> parentAsItem(do_QueryInterface(aParent));  nsCOMPtr<nsIDocShellTreeItem> parentsParentDoc;  parentAsItem->GetParent(getter_AddRefs(parentsParentDoc));  // When it is the top level document we need to check   // to see if it contains a frameset. If it does, then   // we only want to print the doc's children and not the document itself  // For anything else we always print all the children and the document  // for example, if the doc contains an IFRAME we eant to print the child  // document (the IFRAME) and then the rest of the document.  //  // XXX we really need to search the frame tree, and not the content  // but there is no way to distinguish between IFRAMEs and FRAMEs  // with the GetFrameType call.  // Bug 53459 has been files so we can eventually distinguish   // between IFRAME frames and FRAME frames  PRBool doesContainFrameSet = PR_FALSE;  // only check to see if there is a frameset if there is   // NO parent doc for this doc. meaning this parent is the root doc  if (!parentsParentDoc) {    nsCOMPtr<nsIPresShell> shell;    mPresContext->GetShell(getter_AddRefs(shell));    if (shell) {      nsCOMPtr<nsIDocument> doc;      shell->GetDocument(getter_AddRefs(doc));      if (doc) {        nsCOMPtr<nsIContent> rootContent = getter_AddRefs(mDocument->GetRootContent());        if (rootContent) {          if (NS_SUCCEEDED(FindFrameSetWithIID(rootContent, NS_GET_IID(nsIDOMHTMLFrameSetElement)))) {            doesContainFrameSet = PR_TRUE;          }        }      }    }  }  // print any child documents  // like frameset frames or iframes  parentAsNode->GetChildCount(&count);  if(count> 0) {     for(i=0;i<count;i++) {      nsCOMPtr<nsIDocShellTreeItem> child;      parentAsNode->GetChildAt(i, getter_AddRefs(child));      nsCOMPtr<nsIDocShell> childAsShell(do_QueryInterface(child));      childAsShell->GetContentViewer(getter_AddRefs(viewer));      nsCOMPtr<nsIContentViewerFile> viewerFile(do_QueryInterface(viewer));      if (viewerFile) {        nsCOMPtr<nsIWebShell> childWebShell(do_QueryInterface(child));        NS_ENSURE_SUCCESS(viewerFile->PrintContent(childWebShell,aDContext), NS_ERROR_FAILURE);      }    }  }    // now complete printing the rest of the document  // if it doesn't contain any framesets  if (!doesContainFrameSet) {    aDContext->BeginDocument();    aDContext->GetDeviceSurfaceDimensions(width, height);    nsCOMPtr<nsIPresContext> cx;    rv = NS_NewPrintContext(getter_AddRefs(cx));    if (NS_FAILED(rv)) {      return rv;    }    cx->Init(aDContext);    CreateStyleSet(mDocument, getter_AddRefs(ss));    nsCOMPtr<nsIPresShell> ps;    rv = NS_NewPresShell(getter_AddRefs(ps));    if (NS_FAILED(rv)) {      return rv;    }    rv = nsComponentManager::CreateInstance(kViewManagerCID,                                            nsnull,                                            NS_GET_IID(nsIViewManager),                                            (void **)getter_AddRefs(vm));    if (NS_FAILED(rv)) {      return rv;    }    rv = vm->Init(aDContext);    if (NS_FAILED(rv)) {      return rv;    }    nsRect tbounds = nsRect(0, 0, width, height);    // Create a child window of the parent that is our "root view/window"    rv = nsComponentManager::CreateInstance(kViewCID, nsnull, NS_GET_IID(nsIView), (void **)&view);    if (NS_FAILED(rv)) {      return rv;    }    rv = view->Init(vm, tbounds, nsnull);    if (NS_FAILED(rv)) {      return rv;    }    // Setup hierarchical relationship in view manager    vm->SetRootView(view);    ps->Init(mDocument, cx, vm, ss);    nsCompatibility mode;    mPresContext->GetCompatibilityMode(&mode);    cx->SetCompatibilityMode(mode);    cx->SetContainer(aParent);    // get the old history    nsCOMPtr<nsIPresShell> presShell;    nsCOMPtr<nsILayoutHistoryState>  layoutState;    NS_ENSURE_SUCCESS(GetPresShell(*(getter_AddRefs(presShell))), NS_ERROR_FAILURE);    presShell->CaptureHistoryState(getter_AddRefs(layoutState),PR_TRUE);    ps->BeginObservingDocument();    //lay it out...    ps->InitialReflow(width, height);    // update the history from the old presentation shell    nsCOMPtr<nsIFrameManager> fm;    rv = ps->GetFrameManager(getter_AddRefs(fm));    if(NS_SUCCEEDED(rv) && fm) {      nsIFrame* root;      ps->GetRootFrame(&root);      fm->RestoreFrameState(cx, root, layoutState);    }    ps->EndObservingDocument();    // Ask the page sequence frame to print all the pages    nsIPageSequenceFrame* pageSequence;    nsPrintOptions        options;    ps->GetPageSequenceFrame(&pageSequence);    NS_ASSERTION(nsnull != pageSequence, "no page sequence frame");         if (nsnull != mFilePointer) {      // output the regression test      nsIFrameDebug* fdbg;      nsIFrame* root;      ps->GetRootFrame(&root);      if (NS_SUCCEEDED(root->QueryInterface(NS_GET_IID(nsIFrameDebug), (void**) &fdbg))) {        fdbg->DumpRegressionData(cx, mFilePointer, 0);      }      fclose(mFilePointer);          } else {      pageSequence->Print(cx, options, nsnull);    }    aDContext->EndDocument();    ps->EndObservingDocument();  }  return NS_OK;}