Hello,
Thanks for your inquiry.
Our Spire.PDF just draws the text to the PDF page for rendering. Thus, it doesn't support moving the position of the text.
As a workaround, first you could get the position of the second paragraph, and cover it with a white rectangle. Then draw the table at the position of the second paragraph, and finally redraw the second paragraph below the table. Below is the code I used, and I also attched my input file as well as the output file for your reference.
Any question, just feel free to contact us.
- Code: Select all
static void Main(string[] args)
{
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("template.pdf");
PdfPageBase page = pdf.Pages[0];
//Find the second paragraph
string para2 = "Text paragraph 2";
PdfTextFindCollection collection = page.FindText(para2, TextFindParameter.IgnoreCase);
PdfTextFind find = collection.Finds[0];
PdfBrush brush = new PdfSolidBrush(Color.Black);
// Gets the bound of the found text in page
RectangleF rec = find.Bounds;
//Cover the second paragraph with a white rectangle
page.Canvas.DrawRectangle(PdfBrushes.White, rec);
//Draw table
float y = DrawTable(find.SearchPage, new PointF(rec.X,rec.Y));
//Redraw the second paragraph
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font(find.FontName, 16f, FontStyle.Regular));
page.Canvas.DrawString(para2, font, brush, new PointF(rec.X,y));
pdf.SaveToFile("result.pdf", FileFormat.PDF);
}
public static float DrawTable(PdfPageBase page, PointF p)
{
String[] data
= {
"Name;Capital;Continent;Area;Population",
"Argentina;Buenos Aires;South America;2777815;32300003",
"Bolivia;La Paz;South America;1098575;7300000",
"Brazil;Brasilia;South America;8511196;150400000",
"Canada;Ottawa;North America;9976147;26500000",
};
String[][] dataSource = new String[data.Length][];
for (int i = 0; i < data.Length; i++)
{
dataSource[i] = data[i].Split(';');
}
PdfTable table = new PdfTable();
table.Style.CellPadding = 2;
table.Style.HeaderSource = PdfHeaderSource.Rows;
table.Style.HeaderRowCount = 1;
table.Style.ShowHeader = true;
table.DataSource = dataSource;
PdfLayoutResult result = table.Draw(page, p);
return p.Y + result.Bounds.Height + 15;
}
Sincerely,
Rachel
E-iceblue support team
Login to view the files attached to this post.