Problems defining a document from scratch

Hello there.

First of all, a great product. Has helped us a lot in our organization.

I don’t know if the problems I have can be solved, but somehow I think I use Aspose.Words in the “wrong way”. I suspect there are possibilities to do more things.

My problem is basically to define a document from scratch with styles, headers, etc. I am not using a template, I’m building the doc from the beginning and have been using the aspose tutorials without any luck.

This is how I start off:

// CODE START

// Generate stream with an empty document in HTML format.

Document doc = new Document();
MemoryStream stream = new MemoryStream();
doc.Save(stream, SaveFormat.Html);

// Load the empty HTML document setting the base uri to application path.

doc = new Document(stream, path, LoadFormat.Html, null);

doc.RemoveAllChildren();

Section section = new Section(doc);

doc.AppendChild(section);

section.PageSetup.SectionStart = SectionStart.NewPage;
section.PageSetup.PaperSize = PaperSize.Letter;

Body body = new Body(doc);
section.AppendChild(body);

Paragraph para = new Paragraph(doc);
body.AppendChild(para);

// CODE END

I’ve taken this from the tutorials.

Then I insert different headers like this:

builder.ParagraphFormat.StyleName = "Header 1";

builder.Writeln("1 Test");

builder.ParagraphFormat.StyleName = "Header 2";

builder.Writeln("1.1 Test2");

And below each header I do this:

builder.ParagraphFormat.StyleName = "Normal";

builder.InsertHtml("TEST");

----

Questions:

  1. How can I edit my generated document and put my own styles into “Header 1” and “Header 2”? For example, I don’t want them to be Arial and I don’t want “Header 2” to be italic. There must be a way to do this? Remember, I don’t use a template…

  2. Whenever I use InsertHtml, and the string contains tables and other basic html junk, builder.ParagraphFormat.LeftIndent does not work!!! If I insert something simple it works, like for example builder.InsertHtml(“blabla”).

I think both of my problems above are because I’ve probably misunderstood something or I am creating my document(or using it?) in the wrong way. Somehow it seems corrupted.

Could someone point me in the right direction here?

best regards

koki

Thanks for your remark. In the near future I will try to compose a working code example for you and post it here.

Hi
Try use following code examples to solve your problem with styles. I hope that it will help you.

// You can edit existing styles
doc.Styles[StyleIdentifier.Heading1].Font.Color = Color.Red;
doc.Styles[StyleIdentifier.Heading1].Font.Italic = true;
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("existing style test");
// You can add new styles to document and edit it
doc.Document.Styles.Add(StyleType.Paragraph, "Header 1");
doc.Document.Styles["Header 1"].Font.Bold = true;
doc.Document.Styles["Header 1"].Font.Color = Color.Blue;
doc.Document.Styles["Header 1"].Font.Italic = true;
builder.ParagraphFormat.StyleName = "Header 1";
builder.Writeln("new style test");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;
builder.Writeln("normal style test");

As for HTML controls try to use

builder.ParagraphFormat.Style.ParagraphFormat.LeftIndent = 100;