We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Working with separate Display and Print versions of a document

I am working with documents stored in a database. These documents have headers, footers and pictures. When viewing the documents in my application I want to remove the headers, footers and pictures, but when printing I want to include them. To make thinks a bit more complicated it is possible that multiple documents from the database will be returned and I want to merge these together when viewing or printing.

The good news is I have been able to accomplish all of this with one exception. When printing I don’t get the headers, footers or pictures. My basic process is to create to document objects one for viewing and one for printing. I loop through the database results and append the document from the database to the print document object. I then remove headers, footers and pictures and append it to the view document object. and then move to the next record. My problem is that the print document and view document objects end up the same.

Here’s my current code, any ideas would be appreciated:

PrintDoc = new Document();
ViewDoc = new Document();
for (int i = 0; i < mReportTable.Rows.Count; i++)
    DataRow row = mReportTable.Rows[i];
    byte[] rpt = (byte[])row["Doc_Text"];
    if (rpt.Length > 0)
        using (System.IO.MemoryStream ms = new System.IO.MemoryStream(rpt))
            Document doc = new Document(ms);

            // Append document to the Print Document before getting rid of headers and footers
            PrintDoc = AppendReport(PrintDoc, doc);

            // Get rid of headers and footers
            // loop through all sections
            foreach (Section sect in doc.Sections)
                int sectIdx = doc.Sections.IndexOf(sect);

            // Get Rid of Pictures
            NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

            // Append this document to the View Document
            ViewDoc = AppendReport(ViewDoc, doc);

if (ViewDoc.PageCount > 0)
    ViewDoc.Save(@"C:\test\test123.rtf", SaveFormat.Rtf);
    // rtb1.Rtf = MainDoc.ToString(SaveFormat.Rtf); // It would be great if this worked

Here is the AppendReport function referenced in the above code.

public Document AppendReport(Document src, Document dst)
    src.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
    foreach (Section srcSection in src)
        srcSection.PageSetup.PageWidth = dst.LastSection.PageSetup.PageWidth;
        srcSection.PageSetup.PageHeight = dst.LastSection.PageSetup.PageHeight;
        Section newSection = (Section)dst.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);
        Paragraph par = new Paragraph(dst);
    return dst;

Hi Davis,

Thank for your inquiry. In your case, I suggest please clear the images and header footer after appending all documents which you want. You can use the following code example for printing and viewing document.

Document docView = new Document(MyDir + "in.docx");
Document docPrint = (Document)docView.Clone(true);
// Your code…
docView.GetChildNodes(NodeType.Shape, true).Clear();
docView.GetChildNodes(NodeType.DrawingML, true).Clear();
foreach (Section section in docView.Sections)
docPrint.Save(MyDir + "docPrint.docx");
docView.Save(MyDir + "docView.docx");

If the problem still remains, please attach your input Word document here for testing. I will investigate the issue on my side and provide you more information.