Printing to PDF - file empty when needed

Posted by: heidi on 8 September 2017, 2:33 pm EST

  • Posted 8 September 2017, 2:33 pm EST

    Hi,

    I’m using the "FarPoint Spread for Windows Forms 5.0 and .Net Framework 3.5 - 5.0.3514.2008" in a C# app which is being built in Visual Studio 2008. My OS is Windows 7 Ultimate. I want to print the active sheet to pdf and then attach the pdf to an email, but at the point when I attach the pdf to the email, the pdf is empty. It appears that the pdf only gets written ‘after’ the email is generated. I’ve created a basic app that contains just the fpSpread and a button on a form and I get the same behaviour. I’ve included the code from my basic app below.

    …and before I get jumped on for not using “System.Net.Mail”, I can’t use it as I need to fire-up the active mail client (so users have access to their own address book) and the email that they send also needs to reside in their sent items folder.

    So…can anyone help?
    <FONT color=#0000ff size=2><FONT color=#0000ff size=2>
    using</FONT></FONT><FONT size=2> System;
    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>using</FONT></FONT><FONT size=2> System.Diagnostics;
    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>using</FONT></FONT><FONT size=2> System.Windows.Forms;
    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>using</FONT></FONT><FONT size=2> FarPoint.Win.Spread;

    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>namespace</FONT></FONT><FONT size=2> fpSpreadPDFPrinting
    {</FONT>


    <BLOCKQUOTE>

    <FONT color=#0000ff size=2><FONT color=#0000ff size=2>public</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>partial</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>class</FONT></FONT><FONT size=2> </FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>Form1</FONT></FONT><FONT size=2> : </FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>Form
    </FONT></FONT><FONT size=2>{</FONT>


    <BLOCKQUOTE>

    <FONT color=#0000ff size=2><FONT color=#0000ff size=2>public</FONT></FONT><FONT size=2> Form1()
    {</FONT>


    <BLOCKQUOTE>

    <FONT size=2>InitializeComponent();</FONT>

    </BLOCKQUOTE>

    <FONT size=2>}

    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>private</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>void</FONT></FONT><FONT size=2> button1_Click(</FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>object</FONT></FONT><FONT size=2> sender, </FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>EventArgs</FONT></FONT><FONT size=2> e)
    {</FONT>


    <BLOCKQUOTE>

    <FONT color=#0000ff size=2><FONT color=#0000ff size=2>string</FONT></FONT><FONT size=2> strPDFFile = </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>string</FONT></FONT><FONT size=2>.Format(</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>@"c:\fpSpread{0}.pdf"</FONT></FONT><FONT size=2>,</FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>DateTime</FONT></FONT><FONT size=2>.Now.ToString().Replace(</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"/"</FONT></FONT><FONT size=2>,</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"-"</FONT></FONT><FONT size=2>).Replace(</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>":"</FONT></FONT><FONT size=2>,</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"-"</FONT></FONT><FONT size=2>));
    </FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>//Output the pdf
    </FONT></FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>try
    </FONT></FONT><FONT size=2>{</FONT>


    <BLOCKQUOTE style="MARGIN-RIGHT:0px;" dir=ltr>

    <FONT size=2>FarPoint.Win.Spread.</FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>PrintInfo</FONT></FONT><FONT size=2> printset = </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>new
    </FONT></FONT><FONT size=2>FarPoint.Win.Spread.</FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>PrintInfo</FONT></FONT><FONT size=2>();
    printset.PrintToPdf = </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>true</FONT></FONT><FONT size=2>;
    printset.Orientation = </FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>PrintOrientation</FONT></FONT><FONT size=2>.Landscape;
    printset.ZoomFactor = 1;
    printset.PdfFileName = strPDFFile;
    fpSpread1.ActiveSheet.PrintInfo = printset;
    fpSpread1.PrintSheet(0);</FONT>

    </BLOCKQUOTE>

    <FONT size=2>}
    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>catch</FONT></FONT><FONT size=2> (</FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>Exception</FONT></FONT><FONT size=2> ex1)
    {</FONT>


    <BLOCKQUOTE>

    <FONT color=#2b91af size=2><FONT color=#2b91af size=2>MessageBox</FONT></FONT><FONT size=2>.Show(ex1.Message, </FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"Generating PDF"</FONT></FONT><FONT size=2>, </FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>MessageBoxButtons</FONT></FONT><FONT size=2>.OK, </FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>MessageBoxIcon</FONT></FONT><FONT size=2>.Exclamation);
    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>return</FONT></FONT><FONT size=2>;</FONT>

    </BLOCKQUOTE>

    <FONT size=2>}
    </FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>//Email the pdf
    </FONT></FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>string</FONT></FONT><FONT size=2> toList = "</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>me@you.com"</FONT></FONT><FONT size=2>;
    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>string</FONT></FONT><FONT size=2> emailSubject = </FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"Testing pdf printing"</FONT></FONT><FONT size=2>;
    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>string</FONT></FONT><FONT size=2> emailMessage = </FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"See attached pdf file."</FONT></FONT><FONT size=2>;
    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>string</FONT></FONT><FONT size=2> processCommand = </FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>""</FONT></FONT><FONT size=2>;
    </FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>Process</FONT></FONT><FONT size=2> myProcess = </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>new</FONT></FONT><FONT size=2> </FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>Process</FONT></FONT><FONT size=2>();

    </FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>//Construct Command
    </FONT></FONT><FONT size=2>processCommand = </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>string</FONT></FONT><FONT size=2>.Format(</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"mailto:{0}"</FONT></FONT><FONT size=2>, toList);
    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>if</FONT></FONT><FONT size=2> (!</FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>String</FONT></FONT><FONT size=2>.IsNullOrEmpty(emailSubject)) processCommand = </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>string</FONT></FONT><FONT size=2>.Format(</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"{0}?subject={1}"</FONT></FONT><FONT size=2>, processCommand, emailSubject.Replace(</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"&"</FONT></FONT><FONT size=2>, </FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"%26"</FONT></FONT><FONT size=2>));
    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>if</FONT></FONT><FONT size=2> (!</FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>String</FONT></FONT><FONT size=2>.IsNullOrEmpty(strPDFFile)) processCommand = </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>string</FONT></FONT><FONT size=2>.Format(</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"{0}&Attachment=\"{1}\""</FONT></FONT><FONT size=2>, processCommand, strPDFFile);
    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>if</FONT></FONT><FONT size=2> (!</FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>String</FONT></FONT><FONT size=2>.IsNullOrEmpty(emailMessage)) processCommand = </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>string</FONT></FONT><FONT size=2>.Format(</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"{0}&body={1}"</FONT></FONT><FONT size=2>, processCommand, emailMessage.Replace(</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"&"</FONT></FONT><FONT size=2>, </FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"%26"</FONT></FONT><FONT size=2>));

    </FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>//Start the Process
    </FONT></FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>try
    </FONT></FONT><FONT size=2>{</FONT>


    <BLOCKQUOTE style="MARGIN-RIGHT:0px;" dir=ltr>

    <FONT size=2>myProcess.StartInfo.FileName = processCommand;
    myProcess.StartInfo.UseShellExecute = </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>true</FONT></FONT><FONT size=2>;
    myProcess.StartInfo.RedirectStandardOutput = </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>false</FONT></FONT><FONT size=2>;
    </FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>//If I break on the line below and check the pdf file size it is 0 KB.
    </FONT></FONT><FONT size=2>myProcess.Start();
    </FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>//The process generates an email with the pdf as an attachement but the
    </FONT></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>//attachement is empty.
    </FONT></FONT><FONT color=#008000 size=2><FONT color=#008000 size=2>//However, once the above line has executed, the pdf file is written.</FONT></FONT>

    </BLOCKQUOTE>

    <FONT size=2>}
    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>catch</FONT></FONT><FONT size=2> (</FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>Exception</FONT></FONT><FONT size=2> ex2)
    {</FONT>


    <BLOCKQUOTE style="MARGIN-RIGHT:0px;" dir=ltr>

    <FONT color=#0000ff size=2><FONT color=#0000ff size=2>if</FONT></FONT><FONT size=2> (ex2.Message == </FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"Access is Denied"</FONT></FONT><FONT size=2>)
    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>    throw</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>new</FONT></FONT><FONT size=2> </FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>InvalidOperationException</FONT></FONT><FONT size=2>(</FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>String</FONT></FONT><FONT size=2>.Format(</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"Unable to Open your default Email program to create an Email due to an unexpected Security related error: \r\n\r\n{0} "</FONT></FONT><FONT size=2>, ex2.Message));
    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>else
    </FONT></FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>    throw</FONT></FONT><FONT size=2> </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>new</FONT></FONT><FONT size=2> </FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>InvalidOperationException</FONT></FONT><FONT size=2>(</FONT><FONT color=#2b91af size=2><FONT color=#2b91af size=2>String</FONT></FONT><FONT size=2>.Format(</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"Unable to Open your default Email program to create an Email: \r\n\r\n{0} "</FONT></FONT><FONT size=2>, ex2.Message));</FONT>

    </BLOCKQUOTE>

    <FONT size=2>}
    </FONT><FONT color=#0000ff size=2><FONT color=#0000ff size=2>finally
    </FONT></FONT><FONT size=2>{</FONT>


    <BLOCKQUOTE>

    <FONT size=2>myProcess.Dispose();</FONT>

    </BLOCKQUOTE>

    <FONT size=2>}</FONT>

    </BLOCKQUOTE>

    <FONT size=2>}</FONT>

    </BLOCKQUOTE>

    <FONT size=2>}</FONT>

    </BLOCKQUOTE>

    <FONT size=2>}

    </FONT>
  • Replied 8 September 2017, 2:33 pm EST

    Hi Heidi,

    The printing happens in another thread, so it is not finished yet when your code to attach the file happens.  There is a way to make it print in the current thread, but it is not documented, and the XML summary tag  appears to be incorrect.  The method is called SafePrint and takes the FpSpread and sheet index as arguments.  The summary incorrect says that it makes the Spread print in another thread, but it actually does the opposite and makes it print in the current thread and finish before returning.  Here is some sample code that worked for me:

          string pdfFileName = Application.StartupPath + "\\test.pdf";

          if (File.Exists(pdfFileName))
            File.Delete(pdfFileName);
         
          fpSpread1_Sheet1.SetText(5, 5, "testing");
          fpSpread1_Sheet1.PrintInfo.PrintToPdf = true;
          fpSpread1_Sheet1.PrintInfo.PdfFileName = pdfFileName;
         
          fpSpread1.SafePrint(fpSpread1, 0);

          FileStream fs = new FileStream(pdfFileName, FileMode.Open);
          StreamReader sr = new StreamReader(fs);
          MessageBox.Show(sr.ReadToEnd());
          sr.Close();
          sr.Dispose();
          fs.Dispose();

  • Replied 8 September 2017, 2:33 pm EST

    Hi Seanl,

    Many thanks for your reply. We figured it was a thread issue so we devised our own intermediate fix. We refactored the code that does the emailing into it’s own method SendEmail(). We then added a timer which we start at the end of the button click event. When the timer fires, it calls SendEmail(). It works fine.

    I've just tried to change my code to use SafePrint but I don't seem to have the SafePrint method on my fpSpread object!  I’m using "FarPoint Spread for Windows Forms 5.0 and .Net Framework 3.5 - 5.0.3514.2008"

    Heidi

  • Replied 8 September 2017, 2:33 pm EST

    Hello,

    It should work, SafePrint() may not show as one of the methods with intellisense. But the given code should work fine as I have checked it with "FarPoint Spread for Windows Forms 5.0 and .Net Framework 3.5 - 5.0.3514.2008"

     

    Thanks,

     

  • Replied 8 September 2017, 2:33 pm EST

    Hi,

    I tried it and it works perfectly!

    Many thanks for your help,
    Heidi


     

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels