C# 怎么把画在picture控件图形保存为PDF格式的文件
新做的一个绘图软件,需要在上面加上将图形保存为PDF格式。求指教
[解决办法]
用iTextSharp
Document document = null;
PdfWriter writer = null;
string fileSavePath = "d:\";
System.Drawing.Image sourceImg = null;
iTextSharp.text.Image pdfImage = null;
try
{
// filePath = fileSavePath + "\temp" + ".pdf";
filePath = @"d:\temp.pdf";
iTextSharp.text.Rectangle rectPageSize = new iTextSharp.text.Rectangle(PageSize.A4);
// rectPageSize = rectPageSize.Rotate();
document = new Document(rectPageSize, 0, 0, 0, 0);
writer = PdfWriter.GetInstance(document, new FileStream(filePath, FileMode.Create));
document.Open();
// sourceImg = System.Drawing.Image.FromFile(fileSavePath + "\" + fileInputName);
sourceImg =System.Drawing.Image.FromFile( fileInputName);
pdfImage = iTextSharp.text.Image.GetInstance(sourceImg, GetImageFormat(GetFileExtendName(fileInputName)));
pdfImage.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
float height = document.Top - document.TopMargin;
//图片原始大小
pdfImage.ScaleToFit(sourceImg.Width > document.Right ? document.Right : sourceImg.Width, sourceImg.Height > height ? height : sourceImg.Height);
//pdfImage.ScaleToFit(document.Right ,height);
document.Add(pdfImage);
document.NewPage();
sourceImg = System.Drawing.Image.FromFile("2.jpg");
pdfImage = iTextSharp.text.Image.GetInstance(sourceImg, GetImageFormat(GetFileExtendName("2.jpg")));
pdfImage.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
pdfImage.ScaleToFit(sourceImg.Width > document.Right ? document.Right : sourceImg.Width, sourceImg.Height > height ? height : sourceImg.Height);
document.Add(pdfImage);
sourceImg = null;
pdfImage = null;
document.Close();
writer.Close();