We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Set Font.Color from PHP/COM

Hi,

I would like to know how to set the Font.Color of a DocumentBuilder using the COM/PHP interop.

$this->oBuilder->Font->Name = "Courier New";
$this->oBuilder->Font->Color = 255;

The second line here rises an error.

Regards,

Vincent

Hi

Thanks for your inquiry. You should create your own COM visible class that will contain method that sets color of Font.

  1. Open Visual Studio and create new ClassLibrary

  2. Put the following simple code

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ColorHelper
{
    [ComVisible(true)]
    [ProgId("ColorHelper.Helper")]
    public class Helper
    {
        public Helper() { }

        public void SetColor(object builder, int argb)
        {
            (builder as Aspose.Words.DocumentBuilder).Font.Color = System.Drawing.Color.FromArgb(argb);
        }
    }
}
  1. Set strong name of assembly.

  2. Compile library.

  3. Open Visual Studio Command Prompt and register library using the following command

regasm D:\Aleksey\_projects\Tets\_projects\ColorHelper\ColorHelper\bin\Debug\ColorHelper.dll /codebase

Here is PHP code:

<?php 
//Create Document object
$doc = new COM("Aspose.Words.Document");
//Create DocumentBuilder
$builder = new COM("Aspose.Words.DocumentBuilder");
//Set document
$builder->Document = $doc;
//Create ColorHelper instance
$colorHelper = new COM("ColorHelper.Helper");
//Set color of font 
$colorHelper->SetColor($builder, 255); 
//Write "Hello world!" in word document
$builder->Write("Hello world!!!");
//Save created document
$doc->Save("C:\\Temp\\out.doc");
?>

I hope this could help you.

Best regards.