TextBox positioning in OpenXML

2019-03-05 04:43发布

I am new in OpenXML. I am trying to create the word(.docx) file having simple text and images in it using openxml. I have already mentioned all this in my earlier post:

My Earlier post

I am using this code

WordprocessingDocument doc = WordprocessingDocument.Create("E:\\test11.docx", DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
        {
            MainDocumentPart mainPart = doc.AddMainDocumentPart();
            mainPart.Document = new Document();
            Body body = mainPart.Document.AppendChild(new Body());
            Paragraph para = body.AppendChild(new Paragraph());
            ParagraphProperties oParagraphProperties = para.AppendChild(new ParagraphProperties());
            Run run = para.AppendChild(new Run());
            Text tt = new Text(str);
            run.AppendChild(tt);
            RunProperties runProp = new RunProperties(); // Create run properties.
            RunFonts runFont = new RunFonts() { Ascii = "Cambria(Headings)", HighAnsi = "Cambria(Headings)" };
            Bold bold = new Bold();
            DocumentFormat.OpenXml.Wordprocessing.Color Color1 = new DocumentFormat.OpenXml.Wordprocessing.Color() { Val = "0EBFE9" };
            Italic ita = new Italic();
            runProp.Append(bold);
            runProp.Append(Color1);
            runProp.Append(ita);
            FontSize size = new FontSize();
            size.Val = new StringValue((fontSize * 2).ToString());  // 48 half-point font size
            runProp.Append(runFont);
            runProp.Append(size);
            run.PrependChild<RunProperties>(runProp);
        }

That works fine for creating simple .docx file but i am having some coordinates and width height issues in text boxes. You can have a look on attached screenshots for understanding properly the issue.

My Output My output file

Original file

Original File

I want to create a file having text on same position like Original file. Thanks.

1条回答
乱世女痞
2楼-- · 2019-03-05 05:17

Issues like writing the correct C# code that will produce a certain Open XML document can be solved easily using a sample document and the Document Reflector that comes with the Open XML SDK:

  1. Download and install the latest version of the Open XML SDK (OpenXMLSDKToolV25.msi)
  2. Open the Open XML SDK Productivity Tool (OpenXmlSdkTool.exe) located in C:\Program Files (x86)\Open XML SDK\V2.5\tool (when installed to the default location).
  3. Create a sample document with the desired formatting and open it in the tool.
  4. Click on Reflect Code
  5. Copy the generated code to your project and adjust where needed, e.g. by replacing the static text with your content.
查看更多
登录 后发表回答