PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index PDFsharp - moved to http://forum.pdfsharp.net/
Please visit the new PDFsharp forum at http://forum.pdfsharp.net/
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Closing the Pdf to fix an error

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index -> Support - moved to http://forum.pdfsharp.net/
View previous topic :: View next topic  
Author Message
stk23452



Joined: 05 Apr 2007
Posts: 1

PostPosted: Wed Apr 11, 2007 9:01 pm    Post subject: Closing the Pdf to fix an error Reply with quote

After I create a pdf I want to grab it and instantly send it out as an email attachment. It seems to attach and send the pdf perfectly. The problem is that when I open the sent attachment Acrobat says "A drawing error occurred." and displays a blank page.
I use the same file name and path that I used to create it so I know its grabbing the right file. The attachment even matches the one created in file size. My guess is that I'm not closing the created pdf out of memory before I send it. But as you can see in my code below I don't open the pdf in a viewer and I try to use document.close() but that doesn't fix it. Can anyone tell me what I'm doing wrong or how to fix this problem? Below is my code. Thanks in advance.

public void PdfCreate()
{
int y = 0; //top of the page on the y axis
//create a pdf document
PdfDocument document = new PdfDocument();
//create a pdf page
PdfPage page = document.AddPage();
//required for writing anything to a pdf
XGraphics gfx = XGraphics.FromPdfPage(page);
//required for writing text to pdf
XTextFormatter tf = new XTextFormatter(gfx);
//used to create the text boxes that the text goes into
XRect rect = new XRect();

//Draw the PDF
y = DrawHeader(gfx, tf, rect, y);
y = DrawContactBox(gfx, tf, rect, y);
y = DrawVendortBox(gfx, tf, rect, y);
y = DrawCustomerBox(gfx, tf, rect, y);
y = DrawJobInfoBox(gfx, tf, rect, y);
y = DrawConditionsBox(gfx, tf, rect, y);
y = DrawServiceBox(gfx, tf, rect, y);
y = DrawInstructionsBox(gfx, tf, rect, y);

// Save the document
document.Save(filename);
document.Close(); // doesn't seem to work
}

protected void Email()
{
try
{
MailMessage msgMail = new MailMessage();

msgMail.To = "you@mail.com";
msgMail.From = "me@mail.com";
msgMail.Subject = "Attachment Test";

msgMail.BodyFormat = MailFormat.Text;
msgMail.Body = "Check out the attachment!";
msgMail.Attachments.Add(new MailAttachment(filename));

SmtpMail.Send(msgMail);
}
catch (Exception ex)
{
}
}
Back to top
View user's profile Send private message
rivien



Joined: 13 Apr 2007
Posts: 3
Location: USA

PostPosted: Fri Apr 13, 2007 8:35 pm    Post subject: Attaching a PDF file to an email message ... this works! Reply with quote

Hi,

I looked at your code and noticed that the 'PdfCreate()' routine is missing a filename on the object assignment line. You need to define a file name variable, add a file name, and place the file for now in the bin\debug sub-directory. You also should comment out the 'document.Save(filename);' line (see modified example below). This should take care of the PDF generation error. I got both routines shown below to work properly.

Regards,
Rivien

Before:
PdfDocument document = new PdfDocument();

After:
string filename = "TestFile1.pdf";
PdfDocument document = new PdfDocument(filename);

BTW, I was able to create a PDF file and email it to myself. You should look at my email handler, since it's different from yours. I hope this helps!

/// <summary>
/// Sends an email with one PDF attachment
/// Requires namespaces: System.Net.Mail, System.Net.Mime, and System.Net
/// USAGE: CreateMessageWithAttachment("smtp.yourSMTPserver.com");
/// </summary>
public static void CreateMessageWithAttachment(string server)
{
// Specify the file to be attached and sent.
// This example assumes that a file named 'TestMyPDF.pdf' exists in the
// current working directory (probably 'bin\debug\'.
string file = "TestMyPDF.pdf";
// Create a message and set up the recipients.
MailMessage mailMessage = new MailMessage(
"toUser1@toDomain.com",
"toUser2@toDomain.com",
"My test PDF file.",
"See the attached pdf file.");

// Create the file attachment for this e-mail message.
Attachment data = new Attachment(file, MediaTypeNames.Application.Pdf);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
mailMessage.Attachments.Add(data);
//Send the message.
SmtpClient mailClient = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential("username", "password");
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = basicAuthenticationInfo;
mailClient.Send(mailMessage);
data.Dispose();
}

/// <summary>
/// Creates a PDF file
/// </summary>
public static void PdfCreate()
{
int y = 0; //top of the page on the y axis
string filename = Guid.NewGuid().ToString().ToUpper() + ".pdf";
//create a pdf document
PdfDocument document = new PdfDocument(filename);
//create a pdf page
PdfPage page = document.AddPage();
//required for writing anything to a pdf
XGraphics gfx = XGraphics.FromPdfPage(page);
//required for writing text to pdf
XTextFormatter tf = new XTextFormatter(gfx);
//used to create the text boxes that the text goes into
XRect rect = new XRect();

//Draw the PDF
XFont fontH1 = new XFont("Times New Roman", 30, XFontStyle.Bold);
gfx.DrawString("PDF TEST FILE", fontH1, XBrushes.DarkBlue, 123, 50);

//y = DrawHeader(gfx, tf, rect, y);
//y = DrawContactBox(gfx, tf, rect, y);
//y = DrawVendortBox(gfx, tf, rect, y);
//y = DrawCustomerBox(gfx, tf, rect, y);
//y = DrawJobInfoBox(gfx, tf, rect, y);
//y = DrawConditionsBox(gfx, tf, rect, y);
//y = DrawServiceBox(gfx, tf, rect, y);
//y = DrawInstructionsBox(gfx, tf, rect, y);

// Save the document
//document.Save(filename); // YOU DO NOT NEED THIS LINE ... IT AUTO SAVES ON THE CLOSE EVENT
document.Close(); // doesn't seem to work
}
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    PDFsharp - moved to http://forum.pdfsharp.net/ Forum Index -> Support - moved to http://forum.pdfsharp.net/ All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © phpBB Group. Hosted by phpBB.BizHat.com


Start Your Own YouTube Clone

Free Web Hosting | Free Forum Hosting | FlashWebHost.com | Image Hosting | Photo Gallery | FreeMarriage.com

Powered by PhpBBweb.com, setup your forum now!
For Support, visit Forums.BizHat.com