Shuffle Questions While Saving as PDF

Hello ASPOSE Team,

I have attached the example ODT file and want to shuffle its questions randomly each time I save it as PDF.
Each question has Question title, it’s options and Answer.

What options do I have using Aspose Words C#? Please suggest me something.example.zip (16.8 KB)

Thanks in advance
aadi

@aadi1295

Thanks for your inquiry. In your case, we suggest you following solution.

  1. Please bookmark the questions in your input document.
  2. Extract the questions from the document. Please read the following article about extracting the contents.
    How to Extract Selected Content Between Nodes in a Document
  3. Remove the bookmark’s text by setting its text to empty string. You can use Bookmark.Text property to set bookmark’s text.
  4. Move the cursor to the desired bookmark location in the document and insert the extracted contents. The extracted contents are imported in Aspose.Words.Document. You can insert it into document using DocumentBuilder.InsertDocument method.

Hope this helps you.

Thanks Tahir,
Can you please guide with some code, thanks

@aadi1295

Thanks for your inquiry. Please check the following code example. Hope this helps you.

Please get the code of ExtractContent and GenerateDocument methods from following article.
How to Extract Selected Content Between Nodes in a Document

We have attached the input document with this post. questions.zip (11.4 KB)

Document doc = new Document(MyDir + "questions.docx");
ArrayList docs = new ArrayList();
foreach (Bookmark bookmark in doc.Range.Bookmarks)
{
    ArrayList nodes = Common.ExtractContent(bookmark.BookmarkStart, bookmark.BookmarkEnd, true);
    Document question = Common.GenerateDocument(doc, nodes);

    docs.Add(question);
    bookmark.Text = "";
}

DocumentBuilder builder = new DocumentBuilder(doc);

//Insert the second section in the first bookmark.
builder.MoveToBookmark(doc.Range.Bookmarks[0].Name);
builder.InsertDocument((Document)docs[1], ImportFormatMode.KeepSourceFormatting);

//Do the same according to your requirement. 

doc.Save(MyDir + "18.10.docx");

thanks tahir,
anyway to diagrammatically add bookmarks in whole document? I am able to shuffle but I am adding bookmarks manually. I want to add bookmark start on Question node and bookmarkend on Answer node. How can I do that?

@aadi1295

Thanks for your inquiry. Please use the following code example to bookmark the questions. Hope this helps you.

We suggest you please read the following articles.
Use DocumentBuilder to Insert Document Elements
Using DocumentBuilder to Modify a Document Easily

int i = 1;
Boolean blnFirst = true;
Document doc = new Document(MyDir + "example.odt");
DocumentBuilder builder = new DocumentBuilder(doc);
                
builder.StartBookmark("q" + i);
foreach (Paragraph paragraph in doc.GetChildNodes(NodeType.Paragraph, true))
{
    if (paragraph.ToString(SaveFormat.Text).Trim().StartsWith("QUESTION"))
    {
        if (blnFirst)
        {
            paragraph.InsertBefore(new BookmarkStart(doc, "q" + i), paragraph.FirstChild);
            blnFirst = false;
        }
        else
        {
            builder.MoveTo(paragraph.PreviousSibling);
            builder.EndBookmark("q" + i);
            i++;
            builder.StartBookmark("q" + i);
        }
                        
    }
}
builder.MoveToDocumentEnd();
builder.EndBookmark("q" + i); 

doc.Save(MyDir + "18.10.docx");