This is a migrated thread and some comments may be shown as answers.

RadDocument - How to use table width percent?

2 Answers 328 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
C
Top achievements
Rank 1
C asked on 07 Jul 2014, 10:21 PM
How should the TableWidthUnitType.Percent be used?  The TableWidthUnitType.Percent seems to be ignored in the following simple example.
Is there some other setting that needs to be done for the table to take up 100% of the page width excluding margins?

The tables are in the header and footer and need to take up 100% of the page width on each page without regard to page orientation.

internal class Test1
{
internal static void GenerateDocumentTest1()     //this is the main method
{
RadDocument document = new RadDocument();
document.LayoutMode = DocumentLayoutMode.Paged;  

//NOTE: Sizes used by Telerix RadDocument are in Device Independent Pixels (DIP) which are 96 to the inch
//  An 8.5 inch by 11 inch paper size is (8.5 * 96) DIP wide by (11 * 96) DIP tall
//  Use the Unit.InchToDip() conversion method to convert from inches to DIP.  The Unit class does other measurement conversions

document.SectionDefaultPageSize = PaperTypeConverter.ToSize(PaperTypes.Letter);
document.SectionDefaultPageOrientation = PageOrientation.Portrait;
const double pageSizeWidthInches = 8.5;
const double pageMarginSizeInches = 0.5; //page margin on all four sides of the page

const double pageContentWidthSizeInInches = pageSizeWidthInches - (2.0*pageMarginSizeInches); //printable area width of page - used for setting table widths in header and footer
int marginSizeDip = (int) Unit.InchToDip(pageMarginSizeInches); //Note: 96 DIP to the inch
document.SectionDefaultPageMargin = new Padding(marginSizeDip); // 50/100 of an inch for all margins

//------------------------------------------------------------
//build page headers to be applied to document
Header pageHeader = CreatePageHeader();
Footer pageFooter = CreatePageFooter();
{
Section sec = new Section() { PageOrientation = PageOrientation.Portrait};
Paragraph par = new Paragraph();
par.Inlines.Add(new Span("First Page Text A"));
sec.Blocks.Add(par);
sec.Headers.Default = pageHeader;
sec.Footers.Default = pageFooter;
document.Sections.Add(sec);
}

//---------------------------------------------------------
{
Section sec = new Section() { PageOrientation = PageOrientation.Landscape };
Paragraph par = new Paragraph();
par.Inlines.Add(new Span("Second Page Text B"));
par.Inlines.Add(new Span(DocumentEnvironment.NewLine));
sec.Blocks.Add(par);
sec.Headers.Default = pageHeader;
sec.Footers.Default = pageFooter;
document.Sections.Add(sec);
}

//---------------------------------------------------------
{
Section sec = new Section() { PageOrientation = PageOrientation.Portrait };
Paragraph par = new Paragraph();
par.Inlines.Add(new Span("Second Page Text B"));
par.Inlines.Add(new Span(DocumentEnvironment.NewLine));
sec.Blocks.Add(par);
sec.Headers.Default = pageHeader;
sec.Footers.Default = pageFooter;
document.Sections.Add(sec);
}

//---------------------------------------------------------------------
document.EnsureDocumentMeasuredAndArranged();
ExportToWordDocx(document, "test_sample_doc.docx");
}

private static Footer CreatePageFooter()
{
//Page footer is a RadDocument that is inserted into the main RadDocument
RadDocument doc = new RadDocument();
Section sec = new Section();
sec.Blocks.Add(CreateHeaderFooterTable("Footer text (a)", "Footer text (b)"));
doc.Sections.Add(sec);
doc.MeasureAndArrangeInDefaultSize();
Footer footer = new Footer() { Body = doc };
return (footer);
}

private static Header CreatePageHeader()
{
//Page header is a RadDocument that is inserted into the main RadDocument
RadDocument doc = new RadDocument();
Section sec = new Section();
sec.Blocks.Add(CreateHeaderFooterTable("Header text (a)", "Header text (b)"));
doc.Sections.Add(sec);
doc.MeasureAndArrangeInDefaultSize();
Header header = new Header() {Body = doc};
return (header);
}

private static Table CreateHeaderFooterTable(string leftString, string rightString)
{
Table table = new Table();
table.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 100.0); //fit to page width
//table.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Fixed, Unit.InchToDip(tableWidthInches));

table.StyleName = RadDocumentDefaultStyles.DefaultTableGridStyleName;

//table.Borders = new TableBorders(new Border(BorderStyle.None)); //set no borders for table - borders will be done at the cell level
table.Borders = new TableBorders(new Border(BorderStyle.Single)); //diagnostic - show single line border//Must use TableBorders() default constructor and then do a .SetBottom(border type) before assigning it to table.Borders
//Border noBorder = new Border(BorderStyle.None);
//table.Borders = new TableBorders().SetLeft(noBorder).SetTop(noBorder).SetRight(noBorder).SetBottom(noBorder);

TableRow row = new TableRow();{
TableCell cell = new TableCell();
cell.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 50.0);//to set cell borders you have to create a TableCellBorders object with the default constructor and then do a SetBottom(), SetTop(), ... for each of the sides of the cell
//cell.Borders = new TableCellBorders().SetBottom(new Border(1.0f, BorderStyle.Single, Color.FromRgb(0, 0, 255)));

Paragraph par = new Paragraph();
par.Inlines.Add(new Span(leftString));
cell.Blocks.Add(par);row.Cells.Add(cell);
}

{
TableCell cell = new TableCell();
cell.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 50.0);
cell.TextAlignment = RadTextAlignment.Right;Paragraph par = new Paragraph();
par.Inlines.Add(new Span(rightString));
cell.Blocks.Add(par);
row.Cells.Add(cell);
}

table.Rows.Add(row);return (table);

}

private static void ExportToWordDocx(RadDocument document, string fileName)
{
using (Stream outStream = new FileStream(fileName, FileMode.Create))
{
DocxFormatProvider provider = new DocxFormatProvider();
provider.Export(document, outStream);
}
}
}

2 Answers, 1 is accepted

Sort by
0
Petya
Telerik team
answered on 10 Jul 2014, 08:40 AM
Hello,

The observed behavior is related to a known issue in RadRichTextBox. When headers/footers contain a table which does not have a fixed width and the document is exported to DOCX without being shown in RadRichTextBox prior that, the behavior you are seeing occurs. We already have this in our product backlog, you can track this item to be notified about our progress on the matter.

Regards,
Petya
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
C
Top achievements
Rank 1
answered on 01 Oct 2014, 10:32 PM
We ended up using fixed width tables instead of percent given the bug in percent width tables.

Tags
RichTextBox
Asked by
C
Top achievements
Rank 1
Answers by
Petya
Telerik team
C
Top achievements
Rank 1
Share this question
or