c#保存位图为PDF与iTextSharp

本文关键字:iTextSharp PDF 保存 位图 | 更新日期: 2023-09-27 18:18:46

下面的代码从窗体上的控件创建一个位图,然后显示一个保存对话框以保存为JPEG。谁能帮助代码保存位图bm为PDF与iTextSharp?

 Bitmap bm = null;
 bm = new Bitmap(this.RCofactorTBS.SelectedTab.Width, this.RCofactorTBS.SelectedTab.Height);
 this.RCofactorTBS.SelectedTab.DrawToBitmap(bm, this.RCofactorTBS.SelectedTab.ClientRectangle);
 SaveFileDialog dialog = new SaveFileDialog();
 dialog.Filter = "JPEG|*.jpeg";
 dialog.Title = "Save Test As Jpeg";
 dialog.ShowDialog();
 if (dialog.FileName != "" && bm != null)
 {
    bm.Save(dialog.FileName);
 }

c#保存位图为PDF与iTextSharp

你可以试试这个

System.Drawing.Image image = System.Drawing.Image.FromFile("Your image file path");
Document doc = new Document(PageSize.A4);
PdfWriter.GetInstance(doc, new FileStream("image.pdf", FileMode.Create));
doc.Open();
iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg);
doc.Add(pdfImage);
doc.Close();

从这里引用

public void exportarPDF(Bitmap img){           
    // System.Drawing.Image image = System.Drawing.Image.FromFile("C://snippetsource.jpg"); // Here it saves with a physical file
    System.Drawing.Image image = img;  //Here I passed a bitmap
    Document doc = new Document(PageSize.A4);
    PdfWriter.GetInstance(doc, new FileStream("C://image.pdf", FileMode.Create));
    doc.Open();
    iTextSharp.text.Image pdfImage = iTextSharp.text.Image.GetInstance(image,
            System.Drawing.Imaging.ImageFormat.Jpeg);
    doc.Add(pdfImage);
    doc.Close();
}