aspose单词合并

发布于 2025-01-28 10:47:46 字数 223 浏览 3 评论 0原文

我正在使用Aspose库进行单词合并。 在上一个Aspose的上一个版本中,如果我们在合并时添加了一个字段的白色空间,则不会将其视为空的,但是在升级到最新版本后,它将Whitespaces视为空白,并删除这些字段,如果设置已打开。

就我的情况而言,我想预防少数文件的空间或空字段,但将其删除以获取其余的字段。 我试图找到一个可以在现场级别上应用的设置,以防止或删除空字段,但没有任何设置。 有什么办法可以实现这一目标?

I am using Aspose library for Word merge.
In the previous version of the Aspose, if we add white spaces for a field then while merging , it doesn't considers it as empty but after upgrade to latest version, it is considering the whitespaces as blank and removing those fields if setting is ON.

For my case, I want to prevent whitespaces or empty fields for few fileds but remove it for rest of the fields.
I tried to find a setting which can be applied on field level to prevent or remove empty fields but have'nt got any.
Is there any way I can acheive this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花辞树 2025-02-04 10:47:46

如果段落仅包含Whitespaces,则将其视为空并删除。因此,例如,如果您使用以下代码:

string[] fieldNames = new string[] { "FirstName", "MidName", "LastName" };
string[] fieldValues = new string[] { "Alexey", " ", "Noskov" };

Document doc = new Document(@"C:\Temp\in.docx");

doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs;
doc.MailMerge.Execute(fieldNames, fieldValues);

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

其中midname合并字段放在单独的段落中,则该段落将被删除为空。

但是,您可以使用 ifieldMergergeCallback )。例如,您可以将隐藏的文本放在合并字段中,以使段落被视为不为空。例如,请参见以下代码:

string[] fieldNames = new string[] { "FirstName", "MidName", "LastName" };
string[] fieldValues = new string[] { "Alexey", " ", "Noskov" };

Document doc = new Document(@"C:\Temp\in.docx");

doc.MailMerge.FieldMergingCallback = new MergeWhitespaceCallback("MidName");
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs;
doc.MailMerge.Execute(fieldNames, fieldValues);

doc.Save(@"C:\Temp\out.docx");
private class MergeWhitespaceCallback : IFieldMergingCallback
{
    private readonly string[] mRetainParagraphsWithFields;

    public MergeWhitespaceCallback(params string[] retainParagraphsWithFields)
    {
        mRetainParagraphsWithFields = retainParagraphsWithFields;
    }

    public void FieldMerging(FieldMergingArgs args)
    {
        if (!string.IsNullOrEmpty(args.FieldValue.ToString().Trim()))
            return;

        if (!mRetainParagraphsWithFields.Contains(args.FieldName))
            return;

        DocumentBuilder builder = new DocumentBuilder(args.Document);
        builder.MoveTo(args.Field.Start);
        builder.Font.Hidden = true;
        builder.Write("<empty paragraph>");
    }

    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // Do nothing.
    }
}

稍后,您可以删除隐藏的文本,如果需要。

If paragraph contains only whitespaces it is considered as empty and is removed. So for example if you use code like the following:

string[] fieldNames = new string[] { "FirstName", "MidName", "LastName" };
string[] fieldValues = new string[] { "Alexey", " ", "Noskov" };

Document doc = new Document(@"C:\Temp\in.docx");

doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs;
doc.MailMerge.Execute(fieldNames, fieldValues);

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

Where MidName merge field is placed in a separate paragraph, the paragraph will be removed as empty.

However, you can work this behavior around using IFieldMergingCallback. For example, you can put hidden text at the merge field to make the paragraph to be considered as not empty. For example see the following code:

string[] fieldNames = new string[] { "FirstName", "MidName", "LastName" };
string[] fieldValues = new string[] { "Alexey", " ", "Noskov" };

Document doc = new Document(@"C:\Temp\in.docx");

doc.MailMerge.FieldMergingCallback = new MergeWhitespaceCallback("MidName");
doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveEmptyParagraphs;
doc.MailMerge.Execute(fieldNames, fieldValues);

doc.Save(@"C:\Temp\out.docx");
private class MergeWhitespaceCallback : IFieldMergingCallback
{
    private readonly string[] mRetainParagraphsWithFields;

    public MergeWhitespaceCallback(params string[] retainParagraphsWithFields)
    {
        mRetainParagraphsWithFields = retainParagraphsWithFields;
    }

    public void FieldMerging(FieldMergingArgs args)
    {
        if (!string.IsNullOrEmpty(args.FieldValue.ToString().Trim()))
            return;

        if (!mRetainParagraphsWithFields.Contains(args.FieldName))
            return;

        DocumentBuilder builder = new DocumentBuilder(args.Document);
        builder.MoveTo(args.Field.Start);
        builder.Font.Hidden = true;
        builder.Write("<empty paragraph>");
    }

    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // Do nothing.
    }
}

Later, you can remove hidden text if required.

沧桑㈠ 2025-02-04 10:47:46

假设您实际上正在执行MailMerge(不仅仅是覆盖Mergefields),则应该能够通过MailMermerge Main文档中的MailMermerge字段编码来控制大多数(如果不是全部)。
在PC上,您可以使用MergeField \ b和/或\ f开关来抑制空合菲尔德之前或之后的空间。例如,假设您有:

«title»«firstName»«secondName»«lastName»,

但«secondname»有时为空,您不希望它在输出中留下两个空格。为了处理:

  1. 选择“第二名”字段,然后按Shift-f9,以便您获得
    {Mergefield secondname};
  2. 编辑字段代码,以便您最终会遇到 -
    • {Mergefield SecondName \ B“”}或
    • {Mergefield SecondName \ f“”}

取决于抑制的空间是遵循还是在Mergefield之前;

  1. 符合删除以下相应的空间或之前的相应空间
    合并菲尔德;
  2. 将光标放置在此字段中的任何位置,然后按F9进行更新。

注1: \ b和\ f开关在MACS上不起作用或与其他交换机一起使用。在这种情况下,您需要使用,如果测试,则按照 -

  • {{Mergefield secondName}&lt;&gt; 进行编码。 “”“ {Mergefield secondName}”}或
  • {如果{Mergefield secondName}&lt;&gt; “”“ {Mergefield secondName}”}

即使这样,您也可以使用\ b和\ f开关来表达具有自己开关的其他MergeFields。例如,假设您有四个字段“产品”,“供应商”,“数量”和“ UnitPrice”,并且如果“供应商”字段为空。在这种情况下,您可能会使用以下行的字段:

{MERGEFIELD "Supplier" \b "{MERGEFIELD Product}→" \f "→{MERGEFIELD Quantity \# 0}→{MERGEFIELD UnitPrice \# "$0.00"}¶
"}

注2:字段支撑对(即'{}')在上面的示例中都是通过ctrl在文档本身中创建的-F9(Mac上的CMD-F9或,如果您使用笔记本电脑,则可能需要使用Ctrl-FN-F9);您不能简单地键入它们或复制&amp;从此消息粘贴它们。通过任何标准单词对话添加它们也不是实际的。同样,人字形(即'«»')是实际合并的一部分 - 您可以从'插入合并字段'下拉列表中插入(即您也不能从此消息中键入或复制&amp;粘贴它们)。田间构造中表示的空间都是必需的。您应该分别使用真实的选项卡和行/段落中断,而不是→↵和¶符号。

有关更多 MailMerge Tips&amp;技巧,请参阅: https:// wwww。 msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html

Assuming you're actually executing a mailmerge (not just overwriting mergefields), you should be able to control most, if not all, of that via mailmerge field coding in the mailmerge main document.
On PCs, you can use the mergefield \b and/or \f switches to suppress a space before or after an empty mergefield. For example, suppose you have:

«Title» «FirstName» «SecondName» «LastName»

but «SecondName» is sometimes empty and you don’t want that to leave two spaces in the output. To deal with that:

  1. select the «SecondName» field and press Shift-F9 so that you get
    {MERGEFIELD SecondName};
  2. edit the field code so that you end up with-
    • {MERGEFIELD SecondName \b " "} or
    • {MERGEFIELD SecondName \f " "}

depending on whether the space to be suppressed is following or before the mergefield;

  1. delete, as appropriate, the corresponding space following or before
    the mergefield;
  2. position the cursor anywhere in this field and press F9 to update it.

Note 1: the \b and \f switches don't work on Macs or in conjunction with other switches. In such cases you need to use and IF test instead, coded along the lines of-

  • {IF{MERGEFIELD SecondName}<> "" " {MERGEFIELD SecondName}"} or
  • {IF{MERGEFIELD SecondName}<> "" "{MERGEFIELD SecondName} "}

Even so, you can use the \b and \f switches to express other mergefields that do have switches of their own. For example, suppose you have four fields ‘Product’, ‘Supplier’, ‘Quantity’ and ‘UnitPrice’, and you don’t want to output the ‘Product’, ‘Quantity’ or ‘UnitPrice’ fields if the ‘Supplier’ field is empty. In that case, you might use a field coded along the lines of:

{MERGEFIELD "Supplier" \b "{MERGEFIELD Product}→" \f "→{MERGEFIELD Quantity \# 0}→{MERGEFIELD UnitPrice \# "$0.00"}¶
"}

Note 2: The field brace pairs (i.e. '{ }') for the above example are all created in the document itself, via Ctrl-F9 (Cmd-F9 on a Mac or, if you’re using a laptop, you might need to use Ctrl-Fn-F9); you can't simply type them or copy & paste them from this message. Nor is it practical to add them via any of the standard Word dialogues. Likewise, the chevrons (i.e. '« »') are part of the actual mergefields - which you can insert from the 'Insert Merge Field' dropdown (i.e. you can't type or copy & paste them from this message, either). The spaces represented in the field constructions are all required. Instead of the →, ↵ and ¶ symbols, you should use real tabs and line/paragraph breaks, respectively.

For more Mailmerge Tips & Tricks, see: https://www.msofficeforums.com/mail-merge/21803-mailmerge-tips-tricks.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文