通过openxml创建空格,在Word Document中使用AltChunk将文本替换为rtf文件内容

时间:2018-12-28 06:55:49

标签: c# openxml

我试图用主Word Document(目标)中的textToReplaceByRTFContent替换rtf文件内容。但是,rtf文件中的内容会插入到目标文件中。但是在目标文件中创建了一个空间。从rtf文件插入的内容的左缩进变为0.56。 我想摆脱空格(左缩进应为0)

我已在以下链接中复制了rtf文件和CriteriaTest.docx文件。 我需要用Test.rtf内容替换CriteriaTest.docx中的“ CRITERIA-1”文本。 以下代码可以很好地解决缩进问题。请帮我摆脱这个问题。

Input Files Link

请在此链接上找到预期和实际结果的屏幕截图 ActualvsExpected

public class RTFInserter
{

    public static void InsertRTF(string rtfFilePath, string destinationFileName, string textToReplaceByRTFContent)
    {

        using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(destinationFileName, true))
        {
            MainDocumentPart mainPart = wordDoc.MainDocumentPart;

            var holders = mainPart.Document.Body.Descendants<Text>()
             .Where((x) => x.Text == textToReplaceByRTFContent);
            if(holders != null && holders.Count() > 0)
            {
                foreach(var textPlaceHolder in holders)
                {
                    if (textPlaceHolder != null)
                    {
                        var parent = textPlaceHolder.Parent;

                        if (!(parent is Run))  // Parent should be a run element.
                        {

                            // Console.Out.WriteLine("Parent is not run");
                        }
                        else
                        {

                            string altChunkId = "AltChunkId" + Guid.NewGuid().ToString().Replace("-", "");

                            // MainDocumentPart mainDocPart = wordDocument.MainDocumentPart;
                            AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
                                  AlternativeFormatImportPartType.Rtf, altChunkId);


                            // Read RTF document conte
                            using (FileStream fileStream = File.OpenRead(rtfFilePath))
                            {
                                MemoryStream memStream = new MemoryStream();
                                memStream.SetLength(fileStream.Length);
                                fileStream.Read(memStream.GetBuffer(), 0, (int)fileStream.Length);

                                memStream.Position = 0;
                                chunk.FeedData(memStream);
                            }


                            AltChunk altChunk = new AltChunk();
                            altChunk.Id = altChunkId;


                            textPlaceHolder.Parent.InsertAfter(altChunk, textPlaceHolder);

                            // Remove text place holder.
                            textPlaceHolder.Remove();
                            wordDoc.Save();

                        }
                    }
                }
            }

            //Text textPlaceHolder = mainPart.Document.Body.Descendants<Text>()
            // .Where((x) => x.Text == textToReplaceByRTFContent).FirstOrDefault();


        }
    }
}

0 个答案:

没有答案