Free Support Forum - aspose.com

Auto resize problem

Hi there,

I am using aspose word to build a document. I am trying to create a header for a section. it works fine when the header text is short, but when the text gets longer, it gets cut off. If I set heightRule to auto, it shows a bigger box rather than auto grow, here is the code snippet.

InsertHeader(DocumentBuilder builder, string sometext)
{
double pageWidth = builder.CurrentSection.PageSetup.PageWidth - builder.CurrentSection.PageSetup.RightMargin - builder.CurrentSection.PageSetup.LeftMargin;

pageWidth = pageWidth * 2 / 3;

builder.StartTable();
builder.RowFormat.Height = 18;
builder.RowFormat.HeightRule = HeightRule.Exactly; // change to HeightRule.Auto or AtLeast gives a weird results, see attached screenshot
builder.RowFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;

builder.InsertCell();
builder.CellFormat.Width = pageWidth;
builder.CellFormat.WrapText = true;
builder.Font.Bold = true;
builder.Font.Size = 15;
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Yellow;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.ParagraphFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Yellow;

builder.Writeln(sometext);
builder.Font.ClearFormatting();
builder.CellFormat.ClearFormatting();
builder.ParagraphFormat.ClearFormatting();

builder.EndRow();
builder.RowFormat.ClearFormatting();
builder.EndTable();
}

Hi,

Thanks for your request. Please see the following code to add section header:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

Section currentSection = builder.CurrentSection;

PageSetup pageSetup = currentSection.PageSetup;

// Specify if we want headers/footers of the first page to bedifferent from other pages.

pageSetup.DifferentFirstPageHeaderFooter = true;

// --- Create header for the first page. ---

pageSetup.HeaderDistance = 20;

builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);

builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

// Set font properties for header text.

builder.Font.Bold = true;

builder.Font.Size = 15;

builder.ParagraphFormat.Alignment=ParagraphAlignment.Cente builder.ParagraphFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Yellow;

// Specify header title for the first page.

builder.Write(HearderText);


Regards,

Nabeel Ahma

Support Developer,

Aspose Sialkot Team

http://www.aspose.com/

Aspose - Your File Format Experts

sorry for the confusion, I should’ve mentioned in the first place, I am not adding headers and footers to the document, I call it header but it really is just some elements in the document. you can copy/paste my code to your program and call the function, switch heightRule to auto, then you can see the difference

thx

Hi

Thanks for your request. Actually the problem occurs because you are using Writeln method instead of Write. This method inserts a paragraph break at the end of the inserted text. That is why you see that the table row is higher than needed. Please try using the following modified code:

private void InsertHeader(DocumentBuilder builder, string sometext)

{

double pageWidth = builder.CurrentSection.PageSetup.PageWidth - builder.CurrentSection.PageSetup.RightMargin - builder.CurrentSection.PageSetup.LeftMargin;

pageWidth = pageWidth * 2 / 3;

builder.StartTable();

builder.RowFormat.Height = 18;

// Use AtLeast rule insted of Exactly.

builder.RowFormat.HeightRule = HeightRule.AtLeast;

builder.RowFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;

builder.InsertCell();

builder.CellFormat.Width = pageWidth;

builder.CellFormat.WrapText = true;

builder.Font.Bold = true;

builder.Font.Size = 15;

builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Yellow;

builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

builder.ParagraphFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Yellow;

// Use Write method insted of Writeln.

builder.Write(sometext);

builder.EndRow();

builder.EndTable();

// Move clear formating here.

builder.RowFormat.ClearFormatting();

builder.Font.ClearFormatting();

builder.CellFormat.ClearFormatting();

builder.ParagraphFormat.ClearFormatting();

}

Hope this helps.

Best regards,

thank you for the reply, now auto resize works fine, but there is a minor problem. Before the row height was just about the height of the text, see my attachment in the first post. now the height is almost doubled, leaving some empty spaces under the text. see attached. I even tried to set min height to 1, but still didn’t work. am I missing anything?

builder.RowFormat.Height = 1;
builder.RowFormat.HeightRule = HeightRule.AtLeast;

thx

Hi

Thanks for your request. I cannot reproduce this on my side. Could you please attach your output document here for testing? Probably, there is a paragraph break at the end of the string that you are inserting.

Best regards,

sorry I can’t provide the real code and document, but I use exact code you provided in the previous post.
if I set HeightRule to AtLeast, it renders extra line under the text, if I set it to Exactly, it renders correctly.
I took screenshots for above scenarios and put them in one attachment. the upper one is with HeightRule set to AtLeast, the lower one is with HeightRule set to Exactly

thanks

Hi

Thank you for additional information. Please try to modify the code as shown below and let me know if this helps:

private void InsertHeader(DocumentBuilder builder, string sometext)

{

double pageWidth = builder.CurrentSection.PageSetup.PageWidth - builder.CurrentSection.PageSetup.RightMargin - builder.CurrentSection.PageSetup.LeftMargin;

pageWidth = pageWidth * 2 / 3;

builder.StartTable();

builder.RowFormat.Height = 18;

// Use AtLeast rule insted of Exactly.

builder.RowFormat.HeightRule = HeightRule.AtLeast;

builder.RowFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;

builder.InsertCell();

builder.CellFormat.Width = pageWidth;

builder.CellFormat.WrapText = true;

builder.Font.Bold = true;

builder.Font.Size = 15;

builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Yellow;

builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

builder.ParagraphFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Yellow;

// Use Write method insted of Writeln.

builder.Write(sometext.Trim());

builder.EndRow();

builder.EndTable();

// Move clear formating here.

builder.RowFormat.ClearFormatting();

builder.Font.ClearFormatting();

builder.CellFormat.ClearFormatting();

builder.ParagraphFormat.ClearFormatting();

}

Best regards,

still didn’t work, when I did my test, I hardcoded the output text like this
builder.Write(“sometext”);

below is how I get the document. and the ReportTemplate.doc is just an empty doc file created with OFFICE2007

Document doc;
Aspose.Words.LoadOptions opt = new LoadOptions(LoadFormat.Doc, string.Empty, string.Empty);

using (StreamReader reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(“ReportTemplate.doc”)))
{
doc = new Aspose.Words.Document(reader.BaseStream, opt);
}

return doc;

Hi

Thanks for your request. But unfortunately, I still cannot reproduce the problem on my side. Here is code I used for testing:

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

InsertHeader(builder, "TEST");

doc.Save(@"Test001\out.doc");

The output document is attached.

Best regards,

I can reproduce it, I’ve attached a complete demo program for your reference, looks like something wrong with my template but I am not quite convince that the template is just an empty file. please check it out

thx

Hi

<o:p> </o:p>

Thanks for your request. Actually the problem occurs because of the following line of code in InsertHeader function.


builder.RowFormat.HeightRule = HeightRule.Atleast;


Change this line as follows


builder.RowFormat.HeightRule = HeightRule.Exactly;


This will solve your problem.

Adnan Mujahid

That goes back to my very first question, I know that changing to Exactly will solve my problem, but if i have a long text in the box, the text will be cut and won’t wrap to the next line, please refer to my first post.

thx

Hi,

Thanks for clarification.
In my opinion, there is something wrong with your template file. I have replaced your template file with mine (attached) and the results are absolutely fine (out.pdf). I further opened both empty word files (your template and mine) in a binary editor (UEStudio) and both files were different.

Thanks
Adnan Mujahid


Hi

Thank you for additional information. The problem is caused by paragraph space after. Please see the following modified code:

static void InsertHeader(DocumentBuilder builder, string sometext)

{

double pageWidth = builder.CurrentSection.PageSetup.PageWidth - builder.CurrentSection.PageSetup.RightMargin - builder.CurrentSection.PageSetup.LeftMargin;

pageWidth = pageWidth * 2 / 3;

builder.StartTable();

builder.RowFormat.Height = 10;

builder.RowFormat.HeightRule = HeightRule.AtLeast;

builder.RowFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;

builder.InsertCell();

builder.CellFormat.Width = pageWidth;

builder.CellFormat.WrapText = true;

builder.Font.Bold = true;

builder.Font.Size = 15;

builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Yellow;

builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

builder.ParagraphFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Yellow;

builder.ParagraphFormat.SpaceAfter = 0;

builder.Write(sometext);

builder.EndRow();

builder.EndTable();

builder.RowFormat.ClearFormatting();

builder.Font.ClearFormatting();

builder.CellFormat.ClearFormatting();

builder.ParagraphFormat.ClearFormatting();

}

Hope this helps.

Best regards,

Hi,


Here is another work around:

static void InsertHeader(DocumentBuilder builder, string sometext)
{
/double pageWidth = builder.CurrentSection.PageSetup.PageWidth - builder.CurrentSection.PageSetup.RightMargin - builder.CurrentSection.PageSetup.LeftMargin;

pageWidth = pageWidth * 2 / 3;

builder.StartTable();
builder.RowFormat.Height = 18;
builder.RowFormat.HeightRule = HeightRule.AtLeast;
builder.RowFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;

builder.InsertCell();
builder.CellFormat.Width = pageWidth;
builder.CellFormat.WrapText = true;
builder.Font.Bold = true;
builder.Font.Size = 15;
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Yellow;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.ParagraphFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Yellow;

builder.Write(sometext);

builder.EndRow();
builder.EndTable();

builder.RowFormat.ClearFormatting();
builder.Font.ClearFormatting();
builder.CellFormat.ClearFormatting();
builder.ParagraphFormat.ClearFormatting();/
double pageWidth = builder.CurrentSection.PageSetup.PageWidth - builder.CurrentSection.PageSetup.RightMargin - builder.CurrentSection.PageSetup.LeftMargin;

pageWidth = pageWidth * 2 / 3;

builder.StartTable();
builder.RowFormat.Height = 18;
builder.RowFormat.HeightRule = HeightRule.AtLeast;// HeightRule.Exactly;
builder.RowFormat.Borders.LineStyle = Aspose.Words.LineStyle.Single;


builder.CellFormat.Width = pageWidth;
builder.CellFormat.WrapText = true;
builder.Font.Bold = true;
builder.Font.Size = 15;
builder.CellFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Yellow;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
//builder.
builder.ParagraphFormat.Shading.BackgroundPatternColor = System.Drawing.Color.Yellow;
builder.CellFormat.BottomPadding = 0;
builder.InsertCell();
builder.Write(sometext);
//builder.Writeln(sometext);
builder.EndRow();

builder.InsertCell();
builder.Write(sometext);
builder.EndTable();

builder.RowFormat.ClearFormatting();
builder.Font.ClearFormatting();
builder.CellFormat.ClearFormatting();
builder.ParagraphFormat.ClearFormatting();


}

Thats my thought too, just don’t know why that happened since I just created a new doc file in office 2007 and saved it. anyway, problem went away, thanks for the great support.

thanks for all the workaround, I replaced the template file now everything works fine.