从 docx 转换为 pdf 时,Spire doc 会丢失格式
Spire doc loses formatting when converting from docx to pdf
我正在编写一个系统,为不同的个人修改模板字母(通过 OpenXml 文字处理),然后将它们转换为 pdf 以供打印。然而,在转换为 pdf 后,地址丢失了它从普通地址行切换的间距
mrs1 Test2 Name2
that
house
down
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文档和生成的pdf
Image of word doc and resulting pdf
以及手写的word文档和生成的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
我需要它将地址输出为正常格式,但我不确定是什么原因导致的
换行不行,改成段落样式。感谢凯文给我这个提示。以下是生成地址的更新代码。
/// <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;
}
我正在编写一个系统,为不同的个人修改模板字母(通过 OpenXml 文字处理),然后将它们转换为 pdf 以供打印。然而,在转换为 pdf 后,地址丢失了它从普通地址行切换的间距
mrs1 Test2 Name2
that
house
down
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文档和生成的pdf Image of word doc and resulting pdf
以及手写的word文档和生成的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
我需要它将地址输出为正常格式,但我不确定是什么原因导致的
换行不行,改成段落样式。感谢凯文给我这个提示。以下是生成地址的更新代码。
/// <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;
}