Equal space after content

Hi,

I am creating a document from another one by replacing a special word with content using find and replace call back. The replacement content will be in the form of serial numbers, titles, and some HTML content. Currently, we have a requirement to add this content in the form that the serial number will be on the left side, and the title and HTML content will be slightly right side as shown in the input file
test_input.docx (35.1 KB). So please help me to achieve this feature.

Thank you

@Gptrnt You can achieve this using DocumentBuilder in your implementation of IReplacingCallback. To move HTML content right, your can specify ParagraphFormat.LeftIndent and then instruct DocumentBuilder to use it’s formatting upon inserting HTML. For example see the following simplified code:

int itemNumber = 1;
string title = "In this article, I’d like to reacquaint you";
string htmlContent = "<p>In this article, I’d like to reacquaint you with the humble workhorse of communication that is the paragraph. Paragraphs are everywhere. In fact, at the high risk of stating the obvious, you are reading one now. Despite their</p><p>&nbsp;</p><p>In this article, I’d like to reacquaint you with the humble workhorse of communication that is the paragraph. Paragraphs are everywhere. In fact, at the high risk of stating the obvious, you are reading one now. Despite their</p><p>&nbsp;</p><p>In this article, I’d like to reacquaint you with the humble workhorse of communication that is the paragraph. Paragraphs are everywhere. In fact, at the high risk of stating the obvious, you are reading one now. Despite their</p>";

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert item number and title.
builder.Font.Bold = true;
builder.Writeln($"Item No. {itemNumber}\t{title}");
// Insert HTML content moving it right.
builder.Font.ClearFormatting();
builder.ParagraphFormat.LeftIndent = 100;
builder.InsertHtml(htmlContent, true);

doc.Save(@"C:\Temp\out.docx");

Hi,

This is working fine with the above example. If the title content is bigger or the title contains “\n” (means enter ). then content coming in the next line is picked from the start. Uploading the sample document.


Please help me to figure out this issue.

Thank you

@Gptrnt Could you please provide sample code that will allow us to reproduce the problem on our side? We will check the issue and provide you more information. Unfortunately, the screenshot is not quite informative.

Regarding \n in the title string. Such break is interpreted as a paragraph break, so it is expected that paragraph break is inserted between lines.

Hi,

I am attaching the sample code below

List<String> titles = new ArrayList<>();
titles.add("In this article, I’d like to reacquaint you. In this article");
titles.add("In this article, I’d like to reacquaint you. In this article, I’d like to reacquaint you with the humble workhorse of communication that is the paragraph");
titles.add("In this article, I’d like to reacquaint you. In this article.\nI’d like to reacquaint you with the \nhumble workhorse of communication that is the paragraph");
String htmlContent = "<p>In this article, I’d like to reacquaint you with the humble workhorse of communication that is the paragraph. Paragraphs are everywhere. In fact, at the high risk of stating the obvious, you are reading one now. Despite their</p><p>&nbsp;</p><p>In this article, I’d like to reacquaint you with the humble workhorse of communication that is the paragraph. Paragraphs are everywhere. In fact, at the high risk of stating the obvious, you are reading one now. Despite their</p><p>&nbsp;</p><p>In this article, I’d like to reacquaint you with the humble workhorse of communication that is the paragraph. Paragraphs are everywhere. In fact, at the high risk of stating the obvious, you are reading one now. Despite their</p>";

int space = 100;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getParagraphFormat().getTabStops().add(space, TabAlignment.LEFT, TabLeader.NONE);
double leftIndent = builder.getParagraphFormat().getLeftIndent();
for (int i = 0; i < titles.size() ; i++) {
    // Insert item number and title.
    builder.getFont().setBold(true);
    builder.getParagraphFormat().setLeftIndent(leftIndent);
    builder.writeln("Item No. " + (i +1) + " :\t" + titles.get(i));
    // Insert HTML content moving it right.
    builder.getFont().clearFormatting();
    builder.getParagraphFormat().setLeftIndent(space);
    builder.insertHtml(htmlContent, true);
}
String filePath = "/mnt/out.docx";
doc.save(filePath);
Desktop.getDesktop().open(new File(filePath));

The output document generated by the above code is attaching out.docx (17.5 KB). But my expected document should be expected.docx (25.0 KB). Can you please help me attain my desired output?

@Gptrnt To achieve what you need you should use hanging indentation (negative first line indent) and soft line breaks instead of \n. Please see the following modified code and the output document:

List<String> titles = new ArrayList<>();
titles.add("In this article, I’d like to reacquaint you. In this article");
titles.add("In this article, I’d like to reacquaint you. In this article, I’d like to reacquaint you with the humble workhorse of communication that is the paragraph");
titles.add("In this article, I’d like to reacquaint you. In this article.\nI’d like to reacquaint you with the \nhumble workhorse of communication that is the paragraph");
String htmlContent = "<p>In this article, I’d like to reacquaint you with the humble workhorse of communication that is the paragraph. Paragraphs are everywhere. In fact, at the high risk of stating the obvious, you are reading one now. Despite their</p><p>&nbsp;</p><p>In this article, I’d like to reacquaint you with the humble workhorse of communication that is the paragraph. Paragraphs are everywhere. In fact, at the high risk of stating the obvious, you are reading one now. Despite their</p><p>&nbsp;</p><p>In this article, I’d like to reacquaint you with the humble workhorse of communication that is the paragraph. Paragraphs are everywhere. In fact, at the high risk of stating the obvious, you are reading one now. Despite their</p>";

int space = 100;
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getParagraphFormat().getTabStops().add(space, TabAlignment.LEFT, TabLeader.NONE);
double leftIndent = builder.getParagraphFormat().getLeftIndent();
for (int i = 0; i < titles.size(); i++)
{
    // Insert item number and title.
    builder.getFont().setBold(true);
    builder.getParagraphFormat().setLeftIndent(space);
    builder.getParagraphFormat().setFirstLineIndent(-space);
    builder.writeln("Item No. " + (i + 1) + " :\t" + titles.get(i).replace("\n", ControlChar.LINE_BREAK));
    // Insert HTML content moving it right.
    builder.getFont().clearFormatting();
    builder.getParagraphFormat().clearFormatting();
    builder.getParagraphFormat().setLeftIndent(space);
    builder.insertHtml(htmlContent, true);
}
doc.save("C:\\Temp\\out.docx");

out.docx (7.5 KB)

Hi alexey,

Thank you above solution is working good.

1 Like