2013-07-18 62 views
0

所以,我有我的基本iTextSharp的代码与iTextSharp的使用e.DrawString保存为.PDF

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

吧?

+0

我不这么认为iTextSharp对你来说是必须的吗?如果不是,那么PDfSharp将是很好的选择,可以做这样的事情。如果你想这样做,请告诉我。 – ridoy

+0

@ridoy iTextSharp不是必须的。 Haw可以在PDFSharp中使用e.Graphics.DrawString吗? – user2162570

+0

是你的问题“我如何将WinForms标签的内容写入PDF?” –

回答

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()行为像.NETGraphics.DrawString()

+0

我该如何使用它来标签或文本框? – user2162570

+0

你想绘制文本框/标签或它的文本?请明确指出你想做什么事情? – ridoy

+0

我有一个窗体与标签和文本框。我用printDocument使用了e.Graphics.DrawString和e.Graphics.DrawRectangle,但现在我想将它保存为pdf。我知道我可以使用像Bullzip PDF打印机这样的程序,但我希望它能够为最终用户提供更多的支持。 – user2162570