Free Support Forum - aspose.com

Removing IFs from resolved document

How can I remove IFs from resolved document so when I press alt F9 to see clear document without IF instructions (like in PDF output document)
This message was posted using Aspose.Live 2 Forum

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. In this case, you can use DocumentVisitor to achieve this. Here is code:

//Open document

Document doc = new Document("in.doc");

//Create new object

RemoveIFFields ifRem = new RemoveIFFields();

doc.Accept(ifRem);

doc.Save("out.doc");

public class RemoveIFFields : DocumentVisitor

{

private bool _delete = false;

public override VisitorAction VisitFieldStart(Aspose.Words.Fields.FieldStart fieldStart)

{

if (fieldStart.FieldType == Aspose.Words.Fields.FieldType.FieldIf)

{

_delete = true;

fieldStart.Remove();

}

return VisitorAction.Continue;

}

public override VisitorAction VisitFieldSeparator(Aspose.Words.Fields.FieldSeparator fieldSeparator)

{

if (fieldSeparator.FieldType == Aspose.Words.Fields.FieldType.FieldIf)

{

_delete = false;

fieldSeparator.Remove();

}

return VisitorAction.Continue;

}

public override VisitorAction VisitFieldEnd(Aspose.Words.Fields.FieldEnd fieldEnd)

{

if (fieldEnd.FieldType == Aspose.Words.Fields.FieldType.FieldIf)

{

_delete = false;

fieldEnd.Remove();

}

return VisitorAction.Continue;

}

public override VisitorAction VisitRun(Run run)

{

if (_delete)

run.Remove();

return VisitorAction.Continue;

}

}

Please let me know in case of any issues. I will be glad to help you.

Best regards,

HI Andrey

I recently tested your routine (
RemoveIFFields) and realized that it don’t work correctly in my case because I have nested two levels of IF’s. Result is that inner text comes repeated before outer.

This is corect (without
routine RemoveIFFields) :

<span style=“font-size: 10pt; font-family: “Courier New”; color: black;”> <!–[if supportFields]> IF 83333333,25 >=
63,25 " Visina komunalne naknade po m2 obračunske
površine poslovnog prostora utvrđuje se u iznosu od 2,30 kn
IF 0,00 > 0 ", za građevno
zemljište koje služi u svrhu obavljanja djelatnosti 0,23 kn
IF 0,00 > 0
" a za neizgrađeno građevinsko zemljište 0,01
kn
, pa mjesečna
visina komunalne naknade iznosi 63,25 kn
IF 0,00

0 ", a vodne naknade 0,00 kn<![endif]–><span style=“font-size: 10pt; font-family: “Courier New”; color: black;”> Visina
komunalne naknade po m2 obračunske površine poslovnog prostora utvrđuje se u
iznosu od 2,30 kn, pa mjesečna visina komunalne naknade iznosi 63,25 kn<span style=“font-size: 10pt; font-family: “Courier New”;”>.

<span style=“font-size: 10pt; font-family: “Courier New”;”>

This is incorect (with routine RemoveIFFields) : <span style=“font-size: 10pt; font-family: “Courier New”;”>

<span style=“font-size: 10pt; font-family: “Courier New”;”>

<span style=“font-size: 10pt; font-family: “Courier New”;”> ,pa mjesečna visina komunalne naknade iznosi 63,25 kn Visina komunalne naknade po m2 obračunske površine poslovnog prostora utvrđuje se u iznosu od 2,30 kn, pa mjesečna visina komunalne naknade iznosi 63,25 kn.


<span style=“font-size: 10pt; font-family: “Courier New”;”>


<span style=“font-size: 10pt; font-family: “Courier New”;”><span style=“font-size: 10pt; font-family: “Courier New”; color: black;”><o:p></o:p>

Regards

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. This is just simple code example which shows you the technique of removing IF fields. Please see the following link to learn more about DocumentVisitor:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/aspose.words.documentvisitor.html

You can modify this code to fit your needs. Each field in MS Word consists of FieldStart, FieldSeparator, FieldEnd nodes and Run nodes, which represent field code and field value (displayed text). So in MS Word document field looks like the following:

[FieldStart] here is field code [FieldSeparator] here is field value [FieldEnd]

Hope this helps. Please let me know in case of any issues. I will be glad to help you.

Best regards,

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Also, may I ask you, why you need to remove IF fields from your document?

Best regards,

Hi

There are two reasons.
First, it occurs that word crashes when I upload document from web page and second my users want to use document as skeleton and change or add something.

Regards

Hi

Is there
any possibility to eliminate Ifs automatically from document as they are
eliminated when I save document in PDF document format<o:p></o:p>

Regards


Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for additional information.

1. Could you please provide me a sample document and code, which will allow me to reproduce the document, which crashes MS Word? I will check the issue and provide you more information.

2. Unfortunately, there is no other way to remove IF fields than removing they manually. Maybe in your case, if you would not like to have IF fields in your document, you can just remove them from your template and move conditional logic into the code.

Best regards.

Hi

In that case it could not be possible to create and test template in MS Word with it’s native Mail-Merge!

Regards

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks you for additional information. If it would not be required to allow users to edit the output document, you could just protect the document. In this case, end users will not see any fields in the document.

Best regards,

I had some trouble doing this lately and it took me quite a bit of time to locate the answer but just thought I would post it in this thread for others to use if they find this thread.

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/howto-replace-fields-with-static-text.html


Hi

Thank you for sharing your experience. It is perfect that you managed to resolve your issue. Please feel free to ask in case of any question, we are always glad to help you.

Best regards,