如何在C#.net中编写.PDF文件
问题描述:
如何在c#.net中编写.pdf文件?
How to write .pdf file in c#.net?
答
PDfSharp或ITextSharp可以解决问题。
PdfSharp上的
是这样的:
PDfSharp or ITextSharp would do the trick.
on PdfSharp is like this:
class Program
{
static void Main(string[] args)
{
// Create a new PDF document
PdfDocument document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";
// Create an empty page
PdfPage page = document.AddPage();
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
// Create a font
XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
// Draw the text
gfx.DrawString("Hello, World!", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height),
XStringFormats.Center);
// Save the document...
const string filename = "HelloWorld.pdf";
document.Save(filename);
// ...and start a viewer.
Process.Start(filename);
}
}
ITextSharp上的
是这样的:
on ITextSharp is like this:
iTextSharp.text.Document oDoc = new iTextSharp.text.Document();
PdfWriter.GetInstance(oDoc, new FileStream("HelloWorld.pdf", FileMode.Create));
oDoc.Open();
oDoc.Add(new Paragraph("Hello World!"));
oDoc.Close();
我已经使用过它们两者,并且必须说两者都很棒,但iTextSharp也可以将PDF渲染为图像(栅格化)。 PdfSharp也更快更小。
希望它有所帮助。
I have used them both, and must say both are great, but iTextSharp can also render a PDF to an image (rasterize). PdfSharp is faster and smaller also.
hope it helps.
PDFSharp [ ^ ]。好的图书馆将帮助您轻松创建PDF格式。
PDFSharp[^]. Nice library will help you create pdf easily.