使用aspose.Words更新表格列宽

public static bool UpdateTableStyle(string filePath)

{

try

{

Document doc = new Document();

//样式

Style style = doc.Styles.Add(StyleType.Paragraph, "cellStyle");

style.Font.Name = "simsun";

PageSetup pageSet = doc.Sections[0].PageSetup;

pageSet.PaperSize = PaperSize.A4;

pageSet.LeftMargin = ConvertUtil.MillimeterToPoint(5);

pageSet.RightMargin = ConvertUtil.MillimeterToPoint(5);

//pageSet.PageStartingNumber = 1;

//pageSet.PageNumberStyle = NumberStyle.Arabic;

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);

builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

//builder.InsertField("PAGE", "");

//builder.Write("/");

//builder.InsertField("NUMPAGES", "");

double cellWidth = 18;

double portraitMinWidth = ConvertUtil.MillimeterToPoint(cellWidth);//纵向最小列宽

double pageWidth = 200d;//A4纵向可编辑区域宽度200毫米//A4横向向可编辑区域宽度290毫米

List<Document> docItemList = new List<Document>();

bool IsLandscape = false;//是否为横向

//先计算是使用横向还是纵向的A4

if (File.Exists(filePath))

{

Document docItem = new Document(filePath);

docItemList.Add(docItem);

var tableList = docItem.GetChildNodes(NodeType.Table, true);

bool isExist = false;

foreach (Table table in tableList)

{

if(table.Range != null && table.Range.Bookmarks != null)

{

foreach (Bookmark bkName in table.Range.Bookmarks)

{

if (bkName.Name == "aa")

{

isExist = true;

break;

}

}

}

if (isExist)

{

//先计算是否符合纵向A4纸要求的列宽

double width = ConvertUtil.MillimeterToPoint(200d / table.Rows[2].Cells.Count);

if (width < portraitMinWidth)

{

portraitMinWidth = width;

}

}

}

//不包含指标书签时,退出

if (!isExist)

return false;

}

//如果列宽小于一定宽度那么就将文档变为横向cellWidth

if (portraitMinWidth < ConvertUtil.MillimeterToPoint(cellWidth))

{

IsLandscape = true;

pageWidth = 150d;

}

else

{

IsLandscape = false;

pageWidth = 120d;

}

int i = 1;

foreach (Document docItem in docItemList)

{

builder.MoveToDocumentEnd();

// Document docItem = new Document(path);

PageSetup pageSet1 = docItem.Sections[0].PageSetup;

pageSet1.PaperSize = PaperSize.A4;

pageSet1.LeftMargin = ConvertUtil.MillimeterToPoint(20);

pageSet1.RightMargin = ConvertUtil.MillimeterToPoint(20);

var tableList = docItem.GetChildNodes(NodeType.Table, true);

foreach (Table table in tableList)

{

table.AutoFit(AutoFitBehavior.FixedColumnWidths);

foreach (Row row in table.Rows)

{

if (table.Range != null && table.Range.Bookmarks != null)

{

foreach (Bookmark bkName in table.Range.Bookmarks)

{

if (bkName.Name == "aa")

{

for (int j = 0; j < row.Cells.Count; j++)

{

row.Cells[j].CellFormat.WrapText = true;

double width = ConvertUtil.MillimeterToPoint((pageWidth) / row.Cells.Count);

row.Cells[j].CellFormat.Width = width;

}

break;

}

}

}

}

}

docItem.FirstSection.HeadersFooters.LinkToPrevious(true);

builder.InsertDocument(docItem, ImportFormatMode.UseDestinationStyles);

i++;

}

if (IsLandscape)

{

//文档变为横向

pageSet.Orientation = Aspose.Words.Orientation.Landscape;

}

//重新整理页面布局

doc.UpdatePageLayout();

doc.Save(filePath);

return true;

}

catch (Exception ex)

{

LogHelper.WriteLog("更新表格列宽异常,", ex);

return false;

}

}

相关推荐
咕白m62540 分钟前
C# 将多张图片转换到一个 PDF 文档
c#·.net
张人玉1 小时前
c#WPF基础知识
开发语言·c#·wpf
YuanlongWang3 小时前
C# 基础——值类型与引用类型的本质区别
java·jvm·c#
唐青枫4 小时前
C#.NET FluentValidation 全面解析:优雅实现对象验证
c#·.net
从孑开始13 小时前
ManySpeech.MoonshineAsr 使用指南
人工智能·ai·c#·.net·私有化部署·语音识别·onnx·asr·moonshine
YuanlongWang13 小时前
C# 中,依赖注入(DI)的实现方式
c#
SmartSoftHelp开发辅助优化15 小时前
C# WinForm 编程高手:程序,进程,线程。程序,窗体,UI,后台。是如何协调工作的?深度解析>SmartSoftHelp魔法精灵工作室
microsoft·ui·c#
future_studio17 小时前
聊聊 Unity(小白专享、C# 小程序 之 加密存储)
开发语言·小程序·c#
c#上位机17 小时前
MefBootstrapper在Prism引导程序中的使用
c#·wpf·prism
玩泥巴的20 小时前
.NET驾驭Word之力:基于规则自动生成及排版Word文档
c#·word·.net·com互操作