How to create empty document pages with particular dimension in the source document

Hi Team,

My requirement is to create empty document and also get the source document margin ,orientation and create destination document with same margins,orientation and same number of pages as source document.

please kindly help me to resolve the issue.

source document: Enzymatic optimisation_submitted 2.zip (661.8 KB)

expected output : expected output (2).zip (266.4 KB)

Thanks & regards,
priyanga G

@priyanga,

Please try using the following code:

Document doc = new Document(MyDir + @"Enzymatic optimisation_submitted 2\Enzymatic optimisation_submitted 2.docx");

Node[] runs = doc.GetChildNodes(NodeType.Run, true).ToArray();
for (int i = 0; i < runs.Length; i++)
{
    Run run = (Run)runs[i];
    int length = run.Text.Length;

    Run currentNode = run;
    for (int x = 1; x < length; x++)
    {
        currentNode = SplitRun(currentNode, 1);
    }
}

NodeCollection smallRuns = doc.GetChildNodes(NodeType.Run, true);
LayoutCollector collector = new LayoutCollector(doc);

foreach (Section sec in doc.Sections)
{
    NodeCollection runsInSection = sec.Body.GetChildNodes(NodeType.Run, true);
    Run firstRun = (Run)runsInSection[0];
    Run lastRun = (Run)runsInSection[runsInSection.Count - 1];

    int firstPage = collector.GetStartPageIndex(firstRun);
    int lastPage = collector.GetStartPageIndex(lastRun);

    for (int i = firstPage; i <= lastPage; i++)
    {
        Document emptyDoc = new Document();
        emptyDoc.RemoveAllChildren();
        emptyDoc.AppendChild(emptyDoc.ImportNode(sec, false));

        emptyDoc.Save(MyDir + @"Enzymatic optimisation_submitted 2\18.3_" + i + ".docx");
    }
}

Hi @awais.hafeez,

Could you please share the code in java .

Thanks & regards,
Priyanga G

@priyanga,

Please see the following Java code:

Document doc = new Document("D:\\Temp\\Enzymatic optimisation_submitted 2\\Enzymatic optimisation_submitted 2.docx");

Node[] runs = doc.getChildNodes(NodeType.RUN, true).toArray();
for (int i = 0; i < runs.length; i++) {
    Run run = (Run) runs[i];
    int length = run.getText().length();

    Run currentNode = run;
    for (int x = 1; x < length; x++) {
        currentNode = splitRun(currentNode, 1);
    }
}

NodeCollection smallRuns = doc.getChildNodes(NodeType.RUN, true);
LayoutCollector collector = new LayoutCollector(doc);

for (Section sec : doc.getSections()) {
    NodeCollection runsCollection = sec.getBody().getChildNodes(NodeType.RUN, true);

    Run firstRun = (Run) runsCollection.get(0);
    Run lastRun = (Run) runsCollection.get(runsCollection.getCount() - 1);

    int firstPage = collector.getStartPageIndex(firstRun);
    int lastPage = collector.getStartPageIndex(lastRun);

    for (int i = firstPage; i <= lastPage; i++) {
        System.out.println(i);
        Document emptyDoc = new Document();
        emptyDoc.removeAllChildren();
        emptyDoc.appendChild(emptyDoc.importNode(sec, false));

        emptyDoc.save("D:\\Temp\\Enzymatic optimisation_submitted 2\\2\\18.3_" + i + ".docx");
    }
}

Hi @awais.hafeez,

Thank you very much.

Thanks & regards,
priyanga G