Hi,
Thanks for your help, but it doesn’t change anything on the performance;
I try another approach : compute “remaining space” in the current paragraph, and insert a bunch of character in one call, in order to reduce the number of render.
Unfortunately, I’m lost in units translation…
I use this code to compute a caracter measure :
var formattedText = new Aspose.Pdf.Facades.FormattedText("-", Color.Black, paragraph.ParagraphFormat.Style.Font.Name,
Aspose.Pdf.Facades.EncodingType.Winansi, false, (float)paragraph.ParagraphFormat.Style.Font.Size);
var charSpace = formattedText.TextWidth;
Then in the while loop, I remove margin and location to the page width, in order to compute the number of characters I need to insert.
double remainingSpace = paragraph.ParentSection.PageSetup.PageWidth - paragraph.ParentSection.PageSetup.RightMargin - location.Right;
double nbChar = remainingSpace / charSpace;
// insert
paragraph.AppendChild(new Run(doc, String.Concat(Enumerable.Repeat("-", Math.Max((int)nbChar, 1)))));
This is creating twice as much caracters, but I don’t understand whats wrong.
Here is my current code :
Document doc = new Document(inputPath);
int worked = 0;
//Get the third paragraph of document
object verrou = new object();
int paragraphIndex = 0;
bool run = true;
while (run)
// ParallelUtils.While(() => run, () =>
{
Paragraph paragraph = null;
DocumentBuilder builder = new DocumentBuilder(doc);
lock (verrou)
{
paragraphIndex++;
paragraph = (Paragraph)doc.GetChild(NodeType.Paragraph, paragraphIndex, true);
}
if (paragraph == null)
{
run = false;
break;
//return;
}
if (paragraph.ParentStory.NodeType == NodeType.Body // dans le corps du texte
&& paragraph.Runs.Count > 0 // on ignore les lignes vides
&& paragraph.ParagraphFormat.Style.Font.Underline == Underline.None // on ignore les éléments sousligné, sans doutes des titres
&& // on ne prends que les alignements gauche et justifiés
(paragraph.ParagraphFormat.Alignment == ParagraphAlignment.Left || paragraph.ParagraphFormat.Alignment == ParagraphAlignment.Justify))
{
//detect current last char position
builder.MoveTo(paragraph.Runs[paragraph.Runs.Count - 1]);
// insert empty bookmark
BookmarkStart bmStart = builder.StartBookmark("bm");
builder.EndBookmark("bm");
// render
LayoutCollector layoutCollector = new LayoutCollector(doc);
LayoutEnumerator layoutEnumerator = new LayoutEnumerator(doc);
var renderObject = layoutCollector.GetEntity(bmStart);
if (renderObject == null)
continue; //return;
layoutEnumerator.Current = renderObject;
// detect item position
RectangleF location = layoutEnumerator.Rectangle;
float topPosition = location.Top;
var formattedText = new Aspose.Pdf.Facades.FormattedText("-", Color.Black, paragraph.ParagraphFormat.Style.Font.Name,
Aspose.Pdf.Facades.EncodingType.Winansi, false, (float)paragraph.ParagraphFormat.Style.Font.Size);
var charSpace = formattedText.TextWidth + 1;
// paragraph.ParentStory.
int i = 1;
// while previous top position == current top position, insert BM
while (topPosition == location.Top)
{
double remainingSpace = paragraph.ParentSection.PageSetup.PageWidth - paragraph.ParentSection.PageSetup.RightMargin - location.Right;
double nbChar = remainingSpace / charSpace;
// insert
paragraph.AppendChild(new Run(doc, String.Concat(Enumerable.Repeat("-", Math.Max((int)nbChar, 1)))));
// calc again
builder.MoveTo(paragraph.Runs[paragraph.Runs.Count - 1]);
bmStart = builder.StartBookmark("bm" + i);
builder.EndBookmark("bm" + i);
i++;
layoutCollector = new LayoutCollector(doc);
layoutEnumerator = new LayoutEnumerator(doc);
renderObject = layoutCollector.GetEntity(bmStart);
layoutEnumerator.Current = renderObject;
location = layoutEnumerator.Rectangle;
}
paragraph.LastChild.Remove();
// del inserted bookmarks
doc.Range.Bookmarks.Clear();
}
paragraphIndex++;
worked++;
Debug.WriteLine(DateTime.Now.ToString("H:mm:ss zzz") + " : " + worked);
}
//);
doc.Save(inputPath);