Spire.PDF is a professional PDF library applied to creating, writing, editing, handling and reading PDF files without any external dependencies. Get free and professional technical support for Spire.PDF for .NET, Java, Android, C++, Python.

Mon Feb 03, 2020 1:53 pm

Hello,
I have existing pdf doc with 2 text paragraphs. I would like to draw a PdfTable between the 2 and push the bottom text below the table.
I have created new doc from the template , located to targeted position for the table and created/draw it. The problem is the the table is being drawn on top of the bottom text and does not push it below the table. Is there a way to achieve what i am looking for?

input:
Text paragraph 1
Text paragraph 2

desired output:
Text paragraph 1
Pdf table
Text paragraph 2

kmenashe
 
Posts: 1
Joined: Mon Feb 03, 2020 1:37 pm

Tue Feb 04, 2020 6:59 am

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
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Mon Feb 17, 2020 8:04 am

Hello,

Greetings from E-iceblue!
Have you tested my code? Could you please give us some feedback at your convenience?
Thanks in advance.

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.PDF