0

我正在编写一个系统,它为不同的人修改模板字母(通过 OpenXml 文字处理),然后将它们转换为 pdf 以供打印。然而,在转换为 pdf 时,地址正在失去从正常地址行切换的间距

mrs1 Test2 Name2

房子
下来
inr32m

到一个平坦的地址行

mrs1 Test2 Name2thathousedowninr32m

用word写相同的时候产生的xml是

 <w:r>
    <w:t>Mrs</w:t>
  </w:r>
  <w:r>
    <w:br />
    <w:t>test</w:t>
  </w:r>
  <w:r>
    <w:br />
    <w:t>value</w:t>
  </w:r>
  <w:r>
    <w:br />
    <w:t>for</w:t>
  </w:r>
  <w:r>
    <w:br />
    <w:t>the</w:t>
  </w:r>
  <w:r>
    <w:br />
  </w:r>
</w:p>

我输出版本的 xml 是

<w:r>
    <w:t>
      <w:r>
        <w:t> mrs1 Test2 Name2<w:br /></w:t>
      </w:r>
      <w:r>
        <w:t> that<w:br /></w:t>
      </w:r>
      <w:r>
        <w:t> house<w:br /></w:t>
      </w:r>
      <w:r>
        <w:t> down<w:br /></w:t>
      </w:r>
      <w:r>
        <w:t> inr32m<w:br /></w:t>
      </w:r>
    </w:t>
  </w:r>

我生成 的 word doc 和结果 pdf word doc 和结果 pdf 的图像

以及手动编写的 word doc 和生成的 pdf 手动生成的 word doc 和生成的 pdf

此转换当前通过 2 个主要方法运行

private void ConvertToPdf()
    {
        try
        {
            for (int i = 0; i < listOfDocx.Count; i++)
            {
                CurrentModalText = "Converting To PDF";
                CurrentLoadingNum += 1;

                string savePath = PdfTempStorage + i + ".pdf";
                listOfPDF.Add(savePath);

                Spire.Doc.Document document = new Spire.Doc.Document(listOfDocx[i], FileFormat.Auto);
                document.SaveToFile(savePath, FileFormat.PDF);
            }

        }
        catch (Exception e)
        {
            throw e;
        }
    }

 private string ReplaceAddressBlock(string[] address, string localDocText)
    {
        //This is done to force the array to have 6 indicies (with one potentially being empty
        string[] addressSize = new string[6];
        address.CopyTo(addressSize, 0);
        //defines the new save location of the object

        //add an xml linebreak to each piece of the address
        var addressString ="";
        var counter = 0;
        foreach (var t in address)
        {
            if (counter != 0)
            {
                addressString += "<w:r><w:t> ";
            }

            addressString += t + "<w:br />";
            if (counter != 4)
            {
                addressString += "</w:r></w:t> ";
            }
            counter += 1;

        }

        //look for the triple pipes then replace everything in them and them with the address
        var regExp = @"(\|\|\|).*(\|\|\|)";
        Regex regexText = new Regex(regExp, RegexOptions.Singleline);
        localDocText = regexText.Replace(localDocText, addressString);
        return localDocText;
    }

localDocText 是完整文档 xml 的副本

我需要它将地址输出为正常格式,但我不确定是什么原因造成的

4

1 回答 1

0

使用换行符不起作用,必须将其更改为段落样式。感谢凯文给我这个提示。下面是生成地址的更新代码。

        /// <summary>
/// This replaces the address block
/// </summary>
/// <param name="address">The address array </param>
/// <param name="localDocText">the text we want to modify</param>
/// <returns></returns>
private string ReplaceAddressBlock(string[] address, string localDocText)
{
    //This is done to force the array to have 6 indicies (with one potentially being empty
    string[] addressSize = new string[6];
    address.CopyTo(addressSize, 0);
    //defines the new save location of the object

    //add an xml linebreak to each piece of the address
    var addressString ="";
    var counter = 0;
    foreach (var t in address)
    {
        if (counter != 0)
        {
            addressString += " <w:p> <w:r><w:t> ";
        }

        addressString += t ;
        if (counter != 4)
        {
            addressString += "</w:t> </w:r></w:p> ";
        }
        counter += 1;

    }

    //look for the triple pipes then replace everything in them and them with the address
    var regExp = @"(\|\|\|).*(\|\|\|)";
    Regex regexText = new Regex(regExp, RegexOptions.Singleline);
    localDocText = regexText.Replace(localDocText, addressString);
    return localDocText;
}
于 2018-09-01T09:13:16.640 回答