在 iText 7 段落中拆分溢出文本
Split overflow text in iText 7 paragraph
我有一份文档,其中第一页分为三个部分。顶部和底部部分必须在第一页上。中间部分有动态文本。我正在尝试找到一种方法来获取适合中心部分的所有文本,对其进行设置,然后将其余部分添加到延续第二页(如果需要)。
我看过这个S.O。 ,但它似乎不允许将底部部分放在需要去的地方。
这是我想要做的。
using (var workStream = new MemoryStream())
{
using (var pdfWriter = new PdfWriter(workStream))
{
using (var pdfDoc = new PdfDocument(pdfWriter))
{
var document = new Document(pdfDoc);
var pageSize = pdfDoc.GetDefaultPageSize();
var width = pageSize.GetWidth() - document.GetLeftMargin() - document.GetRightMargin();
// *** First Top Section ***
// ... variable creations removed for brevity ...
document.Add(paragraph);
document.Add(table);
// *** CENTER SECTION ***
// This section has a required float height of 325f
// *** Third Bottom Section ***
// ... table creation removed for brevity ...
document.Add(table);
// *** Create the second page if needed and add the overflow text ***
}
}
}
我找到了适合我的解决方案。我确定我可以简化它,但是暴风雨逃逸。
我是这样得到第 1 部分的文本的:
// Set the height of section 2 paragraph
var section2Height = 325f;
// Create a Text element with the desired text
var tempText = new Text(myDesiredText).SetFont(font);
// Create rectangle for the allowable space for section 2
var rectangle = new Rectangle(width, section2Height);
// Create PDFCanvas for the document
var pdfCanvas = new PdfCanvas(pdfDoc, 1);
// Create canvas space for adding the temp paragraph to for splitting
var canvas = new Canvas(pdfCanvas, rectangle);
// Set the temp text to a paragraph
var synopsis = new Paragraph(tempText).SetFixedLeading(12f);
// Get the paragraph renderer
var renderer = synopsis.CreateRendererSubTree();
// Set the paragraph renderer parent to the canvas renderer
renderer.SetParent(canvas.GetRenderer());
// Process the renderer and get the layout result
var result = renderer.Layout(new LayoutContext(new LayoutArea(1, rectangle)));
// If the renderer is full, get all text, otherwise get the text that fits
var page1 = result.GetStatus() == LayoutResult.FULL ? renderer : result.GetSplitRenderer();
// Create a new paragraph for Section 2 (SYNOPSIS)
var newParagraph = new Paragraph().SetFixedLeading(12f);
// Set the Paragraph height and width
newParagraph.SetWidth(width);
newParagraph.SetHeight(section2Height);
// Get the count of child renderers. Each one is a new line
var page1Count = page1.GetChildRenderers().Count;
// Instantiate a string for the last renderer text
var lastText = string.Empty;
// Iterate through the child renderers
for (var i = 0; i < page1Count; i++)
{
// Get the string value
var text = page1.GetChildRenderers()[i].ToString();
// Create a text element
var t1 = new Text(text + Environment.NewLine).SetFont(font);
// Add the Text element to the new paragraph
newParagraph.Add(t1);
// If this is the last line, store the text for later use
if (i + 1 == page1Count)
lastText = text;
}
// Get the index of the last string of text
index = myDesiredText.IndexOf(lastText);
// Get the remaining text base off the index and the last text string length
var page2 = myDesiredText.Substring(index + lastText.Length);
// If there is a string value...
if (!string.IsNullOrEmpty(page2))
{
// Create a list of line returns
var returnCharList = new List<string> { "\r", "\n" };
// Remove the beginning line returns
while (returnCharList.Contains(page2.Substring(0, 1)))
page2 = page2.Substring(2);
}
// Add the new paragraph to the section
document.Add(newParagraph);
这是我如何将其余文本设置到下一页
// If there is a second page, build it out
if (!string.IsNullOrEmpty(page2))
{
// Create the text element
var page2Text = new Text(page2).SetFont(font);
// Get the workable height for the paragraph minus the space for the page footer
var page2Height = height - 30f;
// Create page 2 paragraph
var page2Paragraph = new Paragraph(page2Text).SetHeight(page2Height).SetFixedLeading(12f);
// Add the paragraph to the document
document.Add(page2Paragraph);
// Create new pager
pager = new Paragraph().AddTabStops(tabStops);
// Set the page string for page 2
pageBill = $"EBM - { RemoveBillSpaces(bsd.Bill.BillNumber)} - 2 of { totalPages }";
// Create the text element
pagerText = new Text(pageBill).SetFont(font).SetFontSize(fontSize_Small);
// Add the tabs and text for the pager
pager
.Add(new Tab())
.Add(new Tab())
.Add(pagerText);
// Add the pager to page 2
document.Add(pager);
}
我有一份文档,其中第一页分为三个部分。顶部和底部部分必须在第一页上。中间部分有动态文本。我正在尝试找到一种方法来获取适合中心部分的所有文本,对其进行设置,然后将其余部分添加到延续第二页(如果需要)。
我看过这个S.O。
这是我想要做的。
using (var workStream = new MemoryStream())
{
using (var pdfWriter = new PdfWriter(workStream))
{
using (var pdfDoc = new PdfDocument(pdfWriter))
{
var document = new Document(pdfDoc);
var pageSize = pdfDoc.GetDefaultPageSize();
var width = pageSize.GetWidth() - document.GetLeftMargin() - document.GetRightMargin();
// *** First Top Section ***
// ... variable creations removed for brevity ...
document.Add(paragraph);
document.Add(table);
// *** CENTER SECTION ***
// This section has a required float height of 325f
// *** Third Bottom Section ***
// ... table creation removed for brevity ...
document.Add(table);
// *** Create the second page if needed and add the overflow text ***
}
}
}
我找到了适合我的解决方案。我确定我可以简化它,但是暴风雨逃逸。
我是这样得到第 1 部分的文本的:
// Set the height of section 2 paragraph
var section2Height = 325f;
// Create a Text element with the desired text
var tempText = new Text(myDesiredText).SetFont(font);
// Create rectangle for the allowable space for section 2
var rectangle = new Rectangle(width, section2Height);
// Create PDFCanvas for the document
var pdfCanvas = new PdfCanvas(pdfDoc, 1);
// Create canvas space for adding the temp paragraph to for splitting
var canvas = new Canvas(pdfCanvas, rectangle);
// Set the temp text to a paragraph
var synopsis = new Paragraph(tempText).SetFixedLeading(12f);
// Get the paragraph renderer
var renderer = synopsis.CreateRendererSubTree();
// Set the paragraph renderer parent to the canvas renderer
renderer.SetParent(canvas.GetRenderer());
// Process the renderer and get the layout result
var result = renderer.Layout(new LayoutContext(new LayoutArea(1, rectangle)));
// If the renderer is full, get all text, otherwise get the text that fits
var page1 = result.GetStatus() == LayoutResult.FULL ? renderer : result.GetSplitRenderer();
// Create a new paragraph for Section 2 (SYNOPSIS)
var newParagraph = new Paragraph().SetFixedLeading(12f);
// Set the Paragraph height and width
newParagraph.SetWidth(width);
newParagraph.SetHeight(section2Height);
// Get the count of child renderers. Each one is a new line
var page1Count = page1.GetChildRenderers().Count;
// Instantiate a string for the last renderer text
var lastText = string.Empty;
// Iterate through the child renderers
for (var i = 0; i < page1Count; i++)
{
// Get the string value
var text = page1.GetChildRenderers()[i].ToString();
// Create a text element
var t1 = new Text(text + Environment.NewLine).SetFont(font);
// Add the Text element to the new paragraph
newParagraph.Add(t1);
// If this is the last line, store the text for later use
if (i + 1 == page1Count)
lastText = text;
}
// Get the index of the last string of text
index = myDesiredText.IndexOf(lastText);
// Get the remaining text base off the index and the last text string length
var page2 = myDesiredText.Substring(index + lastText.Length);
// If there is a string value...
if (!string.IsNullOrEmpty(page2))
{
// Create a list of line returns
var returnCharList = new List<string> { "\r", "\n" };
// Remove the beginning line returns
while (returnCharList.Contains(page2.Substring(0, 1)))
page2 = page2.Substring(2);
}
// Add the new paragraph to the section
document.Add(newParagraph);
这是我如何将其余文本设置到下一页
// If there is a second page, build it out
if (!string.IsNullOrEmpty(page2))
{
// Create the text element
var page2Text = new Text(page2).SetFont(font);
// Get the workable height for the paragraph minus the space for the page footer
var page2Height = height - 30f;
// Create page 2 paragraph
var page2Paragraph = new Paragraph(page2Text).SetHeight(page2Height).SetFixedLeading(12f);
// Add the paragraph to the document
document.Add(page2Paragraph);
// Create new pager
pager = new Paragraph().AddTabStops(tabStops);
// Set the page string for page 2
pageBill = $"EBM - { RemoveBillSpaces(bsd.Bill.BillNumber)} - 2 of { totalPages }";
// Create the text element
pagerText = new Text(pageBill).SetFont(font).SetFontSize(fontSize_Small);
// Add the tabs and text for the pager
pager
.Add(new Tab())
.Add(new Tab())
.Add(pagerText);
// Add the pager to page 2
document.Add(pager);
}