Free Support Forum - aspose.com

Font Retriction in word document using aspose.words

Hi,

If there is a font other than(Courier New, 11 Point, Black font) this in the document, the user will receive an error message indicating an invalid font was encountered and the user will be required to modify the Word document accordingly. The error message should be specific in stating that "An invalid font was found in this document, please confirm all fonts used in the document are in Courier New, 11 Point, Black font."

Hi

Thanks for your request. Using Aspose.Words you can check when fonts are using the document and show the warning to the user appropriately. Please follow the link to learn how to check what fonts are using in the document:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.documentbase.fontinfos.html

Hope this helps.

Best regards,

Hi,

i have to check each and every word font(name,size,color) in a document .if it doesn't contain font like couriernew,11 pt,balck font then i have to throw error mesage.

pls let me know how to check font of each and every word in a document usign aspose.words?

for example i atatched one word document in tht i have a content which had courier new font..so now i didnt get error msg?

how to check font of each and every word in a document?

Hi

Thanks for your request. You can use DocumentVisitor to achieve this. I created a simple code example for you:

[Test]

public void Test001()

{

// Define a list of valid fonts.

List<string> fonts = new List<string>();

fonts.Add("Times New Roman");

fonts.Add("Calibri");

// Open document.

Document doc = new Document(@"Test001\in.doc");

// Create visitor and check for invalid fonts.

FontChecker checker = new FontChecker(fonts);

doc.Accept(checker);

Console.WriteLine(checker.HasInvalidFonts);

}

==========================================================

using System.Collections.Generic;

using Aspose.Words;

using Aspose.Words.Fields;

///

/// Class inherited from DocumentVisitor, that checks font of each node with fonts specified in the constructor.

///

class FontChecker : DocumentVisitor

{

public FontChecker(List<string> fontNames)

{

mFontNames = fontNames;

}

///

/// Called when a FieldEnd node is encountered in the document.

///

public override VisitorAction VisitFieldEnd(FieldEnd fieldEnd)

{

//Simply change font name

CheckFont(fieldEnd.Font);

return VisitorAction.Continue;

}

///

/// Called when a FieldSeparator node is encountered in the document.

///

public override VisitorAction VisitFieldSeparator(FieldSeparator fieldSeparator)

{

CheckFont(fieldSeparator.Font);

return VisitorAction.Continue;

}

///

/// Called when a FieldStart node is encountered in the document.

///

public override VisitorAction VisitFieldStart(FieldStart fieldStart)

{

CheckFont(fieldStart.Font);

return VisitorAction.Continue;

}

///

/// Called when a Footnote end is encountered in the document.

///

public override VisitorAction VisitFootnoteEnd(Footnote footnote)

{

CheckFont(footnote.Font);

return VisitorAction.Continue;

}

///

/// Called when a FormField node is encountered in the document.

///

public override VisitorAction VisitFormField(FormField formField)

{

CheckFont(formField.Font);

return VisitorAction.Continue;

}

///

/// Called when a Paragraph end is encountered in the document.

///

public override VisitorAction VisitParagraphEnd(Paragraph paragraph)

{

CheckFont(paragraph.ParagraphBreakFont);

return VisitorAction.Continue;

}

///

/// Called when a Run node is encountered in the document.

///

public override VisitorAction VisitRun(Run run)

{

CheckFont(run.Font);

return VisitorAction.Continue;

}

///

/// Called when a SpecialChar is encountered in the document.

///

public override VisitorAction VisitSpecialChar(SpecialChar specialChar)

{

CheckFont(specialChar.Font);

return VisitorAction.Continue;

}

private void CheckFont(Font font)

{

mHasInvalidFonts |= !mFontNames.Contains(font.Name);

}

public bool HasInvalidFonts

{

get { return mHasInvalidFonts; }

}

// Fonts valid for this document.

private List<string> mFontNames;

private bool mHasInvalidFonts;

}

Hope this helps.

Best regards,

Hi,

Thanks for Your rly. Actually the code which u have given check works fine for font name only what about font size and color i need tht also?can u please help me out

Hi

Thanks for your request. The technique is exactly the same, just modify the CheckFont method in the visitor to check also color and font size.

Best regards,

// Define a list of valid fonts.

List<string> fonts = new List<string>();
fonts.Add("Courier New");
fonts.Add("11");
int fcolor=Color.Black.ToArgb();
fonts.Add(fcolor);

from the above given code i added size and color to font.

public FontChecker(List<string> fontNames)

{

mFontNames = fontNames[0] ;

mFontSize = fontNames[1];

mFontColor = fontNames[2];

}

private void CheckFont(Font font)

{

mHasInvalidFonts |= !mFontNames.Contains(font.Name);

mHasInvalidFonts |= !mFontSize.Contains (font.Size) ;

mHasInvalidFonts |= !mFontColor.Contains(font);--how to check color?

}

is this the way r u saying?but there are some conflicts in decalring list

Hi

Thanks for your request. You can get font color using Font.Color property.

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net/aspose.words.font.html

Best regards,

Hi,

I tried this code also ....is it the right way to get result..pls let me know ur thoughts?

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

string doccontent = doc.Range.Text ;

// Select all runs in the document.

NodeCollection runs = doc.GetChildNodes(NodeType.Run, true, false);

// Use a hashtable so we will keep only unique font names.

Hashtable fontNames = new Hashtable();

foreach (Run run in runs)

{

if (!run.Text.Trim().Equals(""))

{

if (!run.Text.Contains ("Evaluation Only")&&!run.Text.Contains ("\\* MERGEFORMAT"))

{

// This adds an entry into the hashtable.

// The key is the font name. The value is null, we don't need the value.

fontNames[run.Font.Name] = null;

fontNames[run.Font.Size] = null;

fontNames[run.Font.Color.ToArgb()] = null;

if (run.Font.Name != "Courier New" || run.Font.Size != 11 || run.Font.Color.ToArgb() != Color.Black.ToArgb())

{

throw new ArgumentException("Formatting found in the document is not allowed");

}

}

}

}

Hi

Thanks for your request. Yes, you can use this code, it actually does the same as code I suggested, but my code also checks paragraph breaks, special characters, footnotes and fields.

Best regards,

Thank you for ur code..Right now iam using your suggested code snippets....... which helped me a lot....i just tried in another method as the way u said.....Yes u r rite it cheks paragraph breaks, special characters, footnotes and fields...it looks perfect and awesome...and iam using the code which u have given only....

Hi

It is perfect that the suggested code works for you. Please feel free to ask in case of any issues, we are always glad to help you.

Best regards,

///

/// Called when a SpecialChar is encountered in the document.

///

public override VisitorAction VisitSpecialChar(SpecialChar specialChar)

{

CheckFont(specialChar.Font);

return VisitorAction.Continue;

}

i have a question to ask u.how it will check whether SpecialChar,paragraph is encountered in the document r not?

Hi

Thanks for your inquiry. This method simply will not be called if the document does not contain any special characters.

Best regards,