我正在编写一个系统,该系统针对不同的个人修改模板字母(通过OpenXml Wordprocessing),然后将其转换为pdf以进行打印。但是,在转换为pdf时,该地址丢失了,它的间距从普通地址行切换到
mrs1 Test2 Name2
那
房子
下
inr32m
到固定地址行
mrs1 Test2 Name2thathousedowninr32m
用词写同样的词时产生的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文档和生成的pdf Image of word doc and resulting pdf
以及一个手动编写的单词doc和生成的pdf Manually genned word doc and resulting 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的副本
我需要它以正常格式输出地址,但我不确定是什么原因导致
答案 0 :(得分:0)
使用换行符不起作用,必须将其更改为段落样式。感谢Kevin给我这个提示。下面是生成地址的更新代码。
/// <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;
}