首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >打开XML Word C# -分成两列

打开XML Word C# -分成两列
EN

Stack Overflow用户
提问于 2013-01-03 18:06:54
回答 3查看 3.3K关注 0票数 2

我想知道如何将word文档分成两列。我之所以想这么做,是因为我想把所有的信息都放在一个页面上。

非常感谢你的帮助和时间!

我的代码

代码语言:javascript
运行
复制
using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(filepath,   WordprocessingDocumentType.Document))
            {

                MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
                mainPart.Document = new Document();

                var margin_size = 100;

                PageMargin pargeMargins = new PageMargin();
                pargeMargins.Top = margin_size;
                pargeMargins.Bottom = margin_size;

                SectionProperties sectionProps = new SectionProperties();
                sectionProps.Append(pargeMargins);

                Body body = mainPart.Document.AppendChild(new Body());
                body.Append(sectionProps);
                ParagraphProperties paragraphProperties = new ParagraphProperties
                   (
                       //new ParagraphStyleId() { Val = "No Spacing" },
                       new SpacingBetweenLines() { After = "0" }
                   );



                Paragraph para_main = body.AppendChild(new Paragraph(paragraphProperties));

                // Creating the Header where the Serial Number will exist

                // Serial Number
                Run run_mainHeader = para_main.AppendChild(new Run());
                RunProperties runProp_mainHeader = new RunProperties();    // Create run properties.
                FontSize size_mainHeader = new FontSize();
                size_mainHeader.Val = new StringValue("48");                     
                runProp_mainHeader.Append(size_mainHeader);
                run_mainHeader.Append(runProp_mainHeader);                              // Append all of the properties
                run_mainHeader.Append(new Text("S/N: " + sn));

                // Serial Barcode
                Run run_barcode = para_main.AppendChild(new Run());
                RunProperties runProp_barcode = new RunProperties();                    // Create run properties.
                RunFonts runFontMain_barcode = new RunFonts();                          // Create font
                runFontMain_barcode.Ascii = "Code39AzaleaNarrow1";                      // Specify font family
                FontSize size_barcode = new FontSize();
                size_barcode.Val = new StringValue("48");

                runProp_barcode.Append(runFontMain_barcode);
                runProp_barcode.Append(size_barcode);

                run_barcode.PrependChild<RunProperties>(runProp_barcode);
                sn = sn.ToUpper();                                                      // Sets all the values to uppercase to be a barcode format
                run_barcode.AppendChild(new Text("*" + sn + "*"));
                run_barcode.AppendChild(new Break());

                // Tube Type
                Run run_tubetype = para_main.AppendChild(new Run());
                RunProperties runProp_tubetype = new RunProperties();                   // Create run properties.
                FontSize size_tubetype = new FontSize();
                size_tubetype.Val = new StringValue("38");
                runProp_tubetype.Append(size_tubetype);
                run_tubetype.Append(runProp_tubetype);                                  // Append all of the properties
                run_tubetype.Append(new Text("Tube Type: " + forms[0].TubeType + " "));
                //run_tubetype.Append(new Break());

                // Tube Barcode
                Run run_barcode_tube = para_main.AppendChild(new Run());
                RunProperties runProp_barcode_tube = new RunProperties();               // Create run properties.
                RunFonts runFontMain_barcode_tube = new RunFonts();                     // Create font
                runFontMain_barcode_tube.Ascii = "Code39AzaleaNarrow1";                 // Specify font family
                FontSize size_barcode_tube = new FontSize();
                size_barcode_tube.Val = new StringValue("48");

                runProp_barcode_tube.Append(runFontMain_barcode_tube);
                runProp_barcode_tube.Append(size_barcode_tube);

                run_barcode_tube.PrependChild<RunProperties>(runProp_barcode_tube);
                sn = sn.ToUpper();                                                       // Sets all the values to uppercase to be a barcode format
                run_barcode_tube.AppendChild(new Text("*" + forms[0].TubeType + "*"));
                run_barcode_tube.AppendChild(new Break());



                // Goes through all of the forms
                foreach (var form in forms)
                {

                    // Set up a header per form
                    Run run_header = para_main.AppendChild(new Run());
                    RunProperties runProp_formHeader = new RunProperties();
                    Bold bold = new Bold();
                    Underline ul = new Underline() { Val = DocumentFormat.OpenXml.Wordprocessing.UnderlineValues.Single };
                    FontSize size_formHeader = new FontSize();
                    size_formHeader.Val = new StringValue("24");
                    runProp_formHeader.Append(size_formHeader);
                    runProp_formHeader.Append(bold);
                    runProp_formHeader.Append(ul);
                    run_header.AppendChild(new RunProperties(runProp_formHeader));
                    //run_header.AppendChild(new RunProperties(new Bold(), new Underline()));

                    string username = form.Username;
                    string proces_header = form.HeaderTitle;

                    run_header.AppendChild(new Text(proces_header));
                    run_header.AppendChild(new Break());

                    // Goes through all of the fields that each form contains.
                    for (int i = 0; i < form.FieldList.Count; i++)
                    {
                        // Do not need to print out user or serial for each form.
                        if (!(form.FieldList[i].Token == "SNT"))
                        {

                            Run run_data = para_main.AppendChild(new Run());
                            if (form.FieldList[i].Default)
                            {
                                run_data.AppendChild(new Text(form.FieldList[i].Label));
                            }
                            else
                            {
                                run_data.AppendChild(new Text(form.FieldList[i].Label + " " + form.FieldList[i].Spec + form.FieldList[i].Value));
                            }
                            run_data.AppendChild(new Break());
                        }
                    }

                }

                mainPart.Document.Save();
                wordDoc.Close();
                return "Success";
            }

目前,代码在一列上打印所有自上而下的内容。我要它有两列

EN

Stack Overflow用户

发布于 2013-01-11 12:05:17

可以为SectionProperties和ParagraphProperties类使用列类,也可以使用两列或希望的列数

http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.columns(v=office.14).aspx http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.paragraphproperties(v=office.14).aspx http://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.sectionproperties(v=office.14).aspx

这应该可以做到:

代码语言:javascript
运行
复制
            // Add a new main document part. 
            package.AddMainDocumentPart();

            // Create the Document DOM. 
            package.MainDocumentPart.Document = new Document();
            Body bd = package.MainDocumentPart.Document.Body = new Body();

            //write a first paragraph on two columns
            var paragrap1 = new Paragraph();
            var paragraphSectionProperties = new ParagraphProperties(new SectionProperties());
            var paragraphColumns = new Columns();
            paragraphColumns.EqualWidth = true;
            paragraphColumns.ColumnCount = 2;

            paragraphSectionProperties.Append(paragraphColumns);
            paragrap1.Append(paragraphSectionProperties);

            paragrap1.Append(new Run(new Text(str)));
            bd.AppendChild(paragrap1);

            //write another paragraph without paragraph properties
            bd.Append(new Paragraph(new Run(new Text(str))));

            //set the body properties default three columns
            var sectionProperties = new SectionProperties();
            var columns = new Columns();
            columns.EqualWidth = true;
            columns.ColumnCount = 3;

            sectionProperties.Append(columns);
            bd.Append(sectionProperties);

            package.MainDocumentPart.Document.Save();
票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14144599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档