我已经搜索了几个小时试图找出如何做到这一点而没有任何成功。
我确实找到了'FromGraphics'命令,该命令将'system.drawing.graphics'对象读入XGraphics类型,但我不确定如何将其添加到PDF输出中。
我发现的大多数示例都展示了如何从现有文件中获取图像并将其保存为PDF,但是如何使用实用的预制system.drawing.graphics对象来完成此操作呢?
非常感谢C#和/或VB.net示例,而不是GIF。
E.g。如何更改如下示例以使用system.drawing.graphics对象?
string pdfpath = Server.MapPath("PDFs");
string imagepath = Server.MapPath("Images");
Document doc = new Document();
try
{
PdfWriter.GetInstance(doc, new FileStream(pdfpath + "/Images.pdf", FileMode.Create));
doc.Open();
doc.Add(new Paragraph("GIF"));
Image gif = Image.GetInstance(imagepath + "/mikesdotnetting.gif");
doc.Add(gif);
}
catch (Exception ex)
{
//Log error;
}
finally
{
doc.Close();
}
答案 0 :(得分:2)
简短的例子:
Dim pdfDoc As New PdfDocument
Dim page As New PdfPage
pdfDoc.Pages.Add(page)
Dim xg = XGraphics.FromPdfPage(page)
'use the xg object to draw on the pdf page
pdfDoc.Save("path to file")
答案 1 :(得分:2)
回答主要问题"如何使用PDFsharp将system.drawing.graphics对象保存为PDF文件?"
你不能这样做。您可以使用XGraphics类在屏幕上和打印机上绘制PDF页面。您可以为PDF页面或Graphics对象创建XGraphics对象。但是,除了XGraphics例程之外,在Graphics对象上绘制的任何内容都不会出现在PDF中。
另一个问题:"如何更改以下示例以使用system.drawing.graphics对象?"
您需要一个XGraphics对象来绘制PDF。用户OneFineDay展示了如何获得一个。然后使用 .child {
background:blue;
height: 100%;
width: 50%;
position: absolute;
right: 0;
top: 0;
}
在PDF页面上绘制图像。除非您还想在除PDF页面之外的其他媒体上绘图,否则不需要Graphics对象。