Raymond Dumalaog
Raymond Dumalaog

Reputation: 353

Winforms, how to update pages in pdf using a thread

I have a Main.cs and a Pdf.cs. My Main.cs has 3 buttons, everytime you click on each button it navigates to the page of a pdf file in Pdf.cs. Ex. you click button 1, a new window will popup and displays the pdf file at page 1. If you click button 2, it will display a new pdf popup window page 2 and so on.

My question is, is there a way that the pdf file will only open 1 pdf file and everytime the user click the button it will just update/invoke the selected page?

Here's the sample code, Main.cs:

public Main()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    ThreadStartSOP = new Thread(new ThreadStart(() => ThreadScreen(pageNumber)));
    if(ThreadStartSOP.IsAlive)
    {
        //Update the page
    }
    else
    {
        ThreadStartSOP.SetApartmentState(ApartmentState.STA);
        ThreadStartSOP.Start();
    }
}

private void ThreadScreen(int pageNumber)
{
    Application.Run(new pdf(pageNumber));
}

Pdf.cs

public pdf(int page)
{
    this.axAcroPDF1.src = @"c:\example.pdf";
    this.axAcroPDF1.setCurrentPage(page);
}

public void UpdatePDFPage(int page)
{
    this.axAcroPDF1.setCurrentPage(page); //Not updating..
    //I tried creating delegate, then invoking the method to it 
    //and still no luck in updating the pdf pages..
}

Upvotes: 0

Views: 144

Answers (1)

zxed
zxed

Reputation: 336

I cant tell if Pdf.cs is a form - or something else; but basically there is no reference to the form you launched; with a reference to the winform, you can then invoke the methods on that form from the main form.

Something like this should do the trick

    Pdf pdfReference;

    private void dummyPage2()
    {
        if (pdfReference != null)
        pdfReference.UpdatePDFPage(2);
    }


    private void ThreadScreen(int pageNumber)
    {
        pdfReference = new Pdf(1);
        Application.Run(pdfReference);
    }

Upvotes: 1

Related Questions