Ok, I noticed that I recieve an FormatInfo[] from the GetRenderInfoFromPage(page) method.
So, my question now is, if the array docObjects[] that I get from .GetDocumentObjectsFromPage(page), is 'parallel' with the FormatInfo[].
With this I mean that the FormatInfo[i] belongs to the docObjects[i], is this true and an acceptable form of doing this?
For now I'll try this and take it as a fact, but I just want confirmation if possible!
I also would like to know, of possible, how could I add a textframe to a Specific page, as to, when I find the last row of the table (in the page in question), add the textframe, just below the row, or do I really have to rewrite the document and add the textframe at the right time?
[EDIT]
This is the code I came up with:
Code:
/// this class hold the transported value and the transporting value in a paragraph,
/// it has the value of the page to know where to add, and 2 flags to tell me if it is transporting or being transported.
public class ContinuedTextInfo
{
private Paragraph text = null;
private int pageNumber = 0;
private bool toTransport = false;
private bool beingTransported = false;
public Paragraph text { get { return text; } }
public int PageNumber { get { return pageNumber; } }
public bool ToTransport { get { return isATransportar; } }
public bool BeingTransported { get { return isTransportado; } }
public ContinuedTextInfo(int pageNumber, bool toTransport, Paragraph text)
{
this.text = text;
this.pageNumber = pageNumber;
this.toTransport = IsATransportar;
this.beingTransported = !beingTransported;
}
}
public static class Extensions
{
public static List<ContinuedTextInfo> GetContinuedText(this Document document, int ColWithValuesIndex)
{
List<ContinuedTextInfo> continuedTextList = new List<ContinuedTextInfo>();
DocumentRenderer _renderer = new DocumentRenderer(document.Clone());
_renderer.PrepareDocument();
// Paragraph used to reuse the style of a paragraph
Paragraph styledParagraph = new Paragraph();
styledParagraph.Format.Alignment = ParagraphAlignment.Right;
styledParagraph.Format.Font.Size = 5;
styledParagraph.Format.Font.Name = PdfFontNames.SegoeUI;
styledParagraph.Font.Italic = true;
// Values to help with the data lookup
int pageCount = _renderer.FormattedDocument.PageCount;
bool isTransporting = false;
double totalBeingTransported = 0;
// Objects to hold the value, so they don't need to be re-created every cycle.
TableFormatInfo tableInfo = null;
Table table = null;
Cell cell = null;
// Loop through all of the pages
for (int page = 1; page <= pageCount; ++page)
{
DocumentObject[] docObjects = _renderer.GetDocumentObjectsFromPage(page);
RenderInfo[] info = _renderer.GetRenderInfoFromPage(page);
// If there are objects in this page
if (docObjects != null && docObjects.Length > 0)
{
// Loop through them
for (int i = 0; i < docObjects.Length; ++i)
{
// Each time I found a table
if (docObjects[i].GetType() == typeof(Table))
{
tableInfo = (TableFormatInfo)info[i].FormatInfo;
table = (Table)docObjects[i];
if (isTransporting) // If value is being transported from previous page...
{
Paragraph valorATransportar = styledParagraph;
valorATransportar.Add(string.Format("Transporte: {0:N2} *", totalBeingTransported));
isTransporting = false;
continuedTextList.Add(new ContinuedTextInfo(page, isTransporting, valorATransportar));
}
// If the table has more rows than the ones on the page
if (table.Rows.Count >= tableInfo.EndRow)
{
isTransporting = true; // If the table is continuing to another page, I have to transport
// Cycle through the rows and lookand sum the values on a column
for (int r = tableInfo.StartRow; r < tableInfo.EndRow; ++r)
{
// If the row is header of the table, it doesn't have values
if (table.Rows[r].HeadingFormat)
continue;
cell = table[r, ColWithValuesIndex]; // Get the cell that should have a value
if (cell.Tag != null && cell.Tag.ToString().Contains("Valor a Transportar:")) // If the tag has the right content
{
double valor = Convert.ToDouble(cell.Tag.ToString().Split(':')[1].Trim());
totalBeingTransported += valor;
}
}
if (isTransporting) // If it will transport, create a new paragraph to be added enventually
{
Paragraph valorATransportar = styledParagraph;
valorATransportar.Add(string.Format("A Transportar: {0:N2} *", totalBeingTransported));
continuedTextList.Add(new ContinuedTextInfo(page, isTransporting, valorATransportar));
}
}
else // Else, reset the variable to be reused for another table.
{
totalBeingTransported = 0;
}
}
}
}
}
But now I don't know how to add the paragraphs to an absolute location on the page.
here's what I tried, altough it didn't work:
Code:
PdfDocumentRenderer renderer = new PdfDocumentRenderer();
renderer.Document = doc;
renderer.DocumentRenderer.PrepareDocument();
renderer.PrepareRenderPages();
renderer.RenderDocument();
for(int i = 0; i < continuedCellList.Count; ++i)
{
int page = continuedCellList[i].PageNumber;
if (continuedCellList[i].IsATransportar)
{
var gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(renderer.PdfDocument.Pages[page]);
renderer.DocumentRenderer.RenderObject(gfx, 350, 200, 200, continuedCellList[i].Texto);
}
else if(continuedCellList[i].IsTransportado)
{
var gfx = PdfSharp.Drawing.XGraphics.FromPdfPage(renderer.PdfDocument.Pages[page]);
renderer.DocumentRenderer.RenderObject(gfx, 350, 500, 200, continuedCellList[i].Texto);
}
}
renderer.PdfDocument.Save("1_" + "Teste_de_Encontrar_Objetos" + ".pdf");
Thank you in advance.