使用带有iTextSharp的e.DrawString保存为.pdf

时间:2013-07-18 18:20:09

标签: c# pdf itextsharp

所以我有基本的iTextSharp代码

 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();

    }

现在可以使用

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

用它?

1 个答案:

答案 0 :(得分:0)

使用PdfDocument你可以做这样的事情......

  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);

此处XGraphics.DrawString()的行为类似于Graphics.DrawString()的{​​{1}}。