1

So I have my basic iTextSharp code

 private void button1_Click(object sender, EventArgs e)
    {
        Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
        PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream("Test.pdf", FileMode.Create));
        doc.Open();
        doc.Add();
        doc.Close();

    }

Now can I use

e.Graphics.DrawString(label1.Text, label1.Font, Brushes.Black, 13, 13);

with it??

user2162570
  • 71
  • 2
  • 10
  • I don't think so.is iTextSharp is must for you?If not,then PDfSharp would be good option to do things like that.Inform me if you want to do that. – ridoy Jul 18 '13 at 19:17
  • 1
    @ridoy iTextSharp is not a must. Haw can I use e.Graphics.DrawString in PDFSharp? – user2162570 Jul 18 '13 at 19:57
  • Is your question "How can I write the contents of a WinForms label into a PDF?" – Chris Haas Jul 19 '13 at 13:46

1 Answers1

0

Using PdfDocument you can do something like this..

  PdfDocument document = new PdfDocument();
  document.Info.Title = "Sample pdf using PDFsharp";

  PdfPage page = document.AddPage();

  XGraphics gfx = XGraphics.FromPdfPage(page);

  XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic); //set your label1.Font here

  gfx.DrawString("Sample using PdfSharp!", font, XBrushes.Black, //you can use your label1.Text here
    new XRect(0, 0, page.Width, page.Height),
    XStringFormats.Center);

  const string filename = "sample.pdf";
  document.Save(filename);

  Process.Start(filename);

Here XGraphics.DrawString() acts like Graphics.DrawString() of .NET.

ridoy
  • 6,274
  • 2
  • 29
  • 60
  • How can I use this to deaw the label or textbox? – user2162570 Jul 18 '13 at 20:18
  • Do you want to draw textbox/label or it's text? please clearly specify what things you want to do? – ridoy Jul 18 '13 at 20:19
  • I have a windows form with labels and textboxes. I used e.Graphics.DrawString and e.Graphics.DrawRectangle withe the printDocument but now I would like to save it as pdf. I know I can use programs like Bullzip PDF printer but I want it to be more simpel for the end user. – user2162570 Jul 18 '13 at 20:29
  • So you want to print your windows form as pdf with it's all content in a word? – ridoy Jul 18 '13 at 20:33
  • Yes but not as a printscreen. I have some if statements like: if (textBox72.Text == "0" && textBox80.Text == "0" && textBox40.Text == "0" ....... – user2162570 Jul 18 '13 at 20:37
  • See http://stackoverflow.com/questions/4813341/convert-windows-form-to-pdf-file, http://stackoverflow.com/questions/10618036/converting-windows-form-in-c-sharp-to-pdf-using-pdfsharp, http://www.codeproject.com/Tips/465179/Convert-a-Windows-Form-to-PDF – ridoy Jul 18 '13 at 20:41