halil
halil

Reputation: 800

Printing different values (Copy number) in different copies of a particular document

My application uses an RTF file with merge fields as source and creates a PDF file with it using Aspose.Words. The users of this application give that resulting document to their clients, so copies of same document will be printed for each of their client. There is only one difference on those copies however, and that is copy number at the end of each document copy.

For now; lets say there are 4 clients so 4 copies of the same document will be printed with only copy numbers different. I achieve this by creating same document for 4 times and each time I insert my html text, merge fields, and add copy number then append the documents. In the end, I have one big document in which all 4 created documents appended.

Here is my code block for it, there were lots of code there, so I tried to downsize them to only related parts:

import com.aspose.words.*

Document docAllAppended = new Document(loadDocument("/documents/" + RTFFileName));
Document docTemp=null;
for (int i = 1; i <= copyNumber; i++) {
  docTemp = new Document(loadDocument("/documents/" + RTFFileName));

  DocumentBuilder builder = new DocumentBuilder(docTemp);
  //insert html which includes file context
  builder.insertHtml(htmlText);

  //insert Copy number
  builder.moveToBookmark("sayfa");
  Font font = builder.getFont();
  font.setBold(true);
  font.setSize(8);
  builder.write("Copy Number-" + i+ " / ");
  font.setBold(false);

  docAllAppended.appendDocument(docTemp,ImportFormatMode.USE_DESTINATION_STYLES);
}

This looks so unnecessary and has low performance. Also each time my users try to change copy number to be printed, my application calculates whole thing from the start. What I am asking is, is there a way to make this faster or how not to create whole thing again when copy number to be printed changes? So far I haven't found much.

Thanks in advance.

Upvotes: 1

Views: 207

Answers (1)

Saqib Razzaq
Saqib Razzaq

Reputation: 1430

If the only difference is the copy number, then you can just prepare the document once by inserting HTML, merging etc.

Then, in a for loop, set the copy number and save the document as docx or pdf. Appending the document in the loop is not necessary, you can save each copy as different name.

import com.aspose.words.*

Document docAllAppended = new Document(loadDocument("/documents/" + RTFFileName));
Document docTemp=null;
docTemp = new Document(loadDocument("/documents/" + RTFFileName));

DocumentBuilder builder = new DocumentBuilder(docTemp);
//insert html which includes file context
builder.insertHtml(htmlText);

// In for loop, only update the copy number
for (int i = 1; i <= copyNumber; i++) {
    // Use DocumentBuilder for font setting
    builder.moveToBookmark("sayfa");
    Font font = builder.getFont();
    font.setBold(true);
    font.setSize(8);
    builder.write("dummy value");
    font.setBold(false);

    // Use Bookmark for setting the actual value
    Bookmark bookmark = docAllAppended.getRange().getBookmarks().get("sayfa");
    bookmark.setText("Copy Number-" + i + " / ");

    // Save the document for each client
    docAllAppended.save(Common.DATA_DIR + "Letter-Client-" + i + ".docx");
}

I work with Aspose as Developer Evangelist.

Upvotes: 1

Related Questions