在Aspose.Pdf中,为什么给Text对象加上边框却实际上给它两个边框?如何摆脱内部边界?

问题描述

这是我的风格:

Dim sectionHeaderStyle As TextInfo = New TextInfo()
sectionHeaderStyle.FontName = "Arial"
sectionHeaderStyle.FontSize = 16
sectionHeaderStyle.Alignment = AlignmentType.Left
sectionHeaderStyle.IsTrueTypeFontBold = True
sectionHeaderStyle.IsTrueTypeFontItalic = False
sectionHeaderStyle.Color = New Aspose.Pdf.Color("Black")
' Todo: why are we getting a double border?
sectionHeaderStyle.TextBorder = New BorderInfo(15) ' 15 is binary 1111 so it means all four borders
sectionHeaderStyle.BackgroundColor = New Aspose.Pdf.Color("Silver")
sectionHeaderStyle.IsUnderline = False

这是我使用该样式创建Text对象的地方:

<Extension>
Public Function CreateBlankSection(ByVal pdf As Pdf,ByVal marginInfo As MarginInfo,ByVal sectionHeaderStyle As TextInfo,ByVal mainStyle As TextInfo,ByVal headerText As String) As Section
    ' Add a blank section into the PDF document
    Dim sec As Section = pdf.Sections.Add()
    sec.PageInfo.PageWidth = 8.5 * 72
    sec.PageInfo.PageHeight = 11 * 72
    sec.PageInfo.Margin = marginInfo
    sec.TextInfo = mainStyle

    ' Add the section title
    Dim text As Text = sec.CreateText(sectionHeaderStyle,headerText)

    ' Return the section created
    Return sec
End Function

<Extension>
Public Function CreateText(ByVal sec As Section,ByVal style As TextInfo,ByVal text As String) As Text
    Dim txt As Text = New Text(sec,text)
    txt.TextInfo = style
    sec.Paragraphs.Add(txt)
    Return txt
End Function

但是当我渲染该部分时(在此屏幕快照中,我在标题之外添加了更多表格和文本),我得到了两个边框?!

enter image description here

这是怎么回事?如何摆脱内部边界?我只想要外边界。

解决方法

根据您的问题下的注释中的建议,检查以下基于DOM(Aspose.Pdf)的示例代码片段,并在PDF文档中创建一个表格。

var doc = new Document();
var page = doc.Pages.Add();
page.PageInfo.Margin = new MarginInfo(10,10,10);

Aspose.Pdf.Table pdfTable = new Table
{
 ColumnWidths = "200 100 150"
};

pdfTable.DefaultCellBorder = new BorderInfo(BorderSide.All,1f);

page.Paragraphs.Add(pdfTable);

var headerRow = pdfTable.Rows.Add();
headerRow.DefaultCellTextState = new TextState() { FontSize = 20f,FontStyle = FontStyles.Bold };
headerRow.Cells.Add("Primary Parent Information");
headerRow.Cells[0].ColSpan = 3;

var secondRow = pdfTable.Rows.Add();
secondRow.Cells.Add("Primary Parent Name");
secondRow.Cells.Add("Date of Birth");
secondRow.Cells.Add("Current Age");

var thirdRow = pdfTable.Rows.Add();
thirdRow.Cells.Add("Clark Kentenburger");
thirdRow.Cells.Add("9/28/1978");
thirdRow.Cells.Add("42");

doc.Save("Table.pdf");

以下是上述代码段生成的示例输出。您会注意到文本内容及其边框已正确对齐。

Sample Output of the above code snippet