iTextSharp - 不包含'getInstance'的定义

时间:2013-03-18 12:48:15

标签: c# winforms visual-studio-2008 itextsharp

我正在尝试使用iTextSharp从图像生成pdf,但我收到以下错误: iTextSharp.Image不包含'getInstance'和'iTextSharp.text的定义。文档不包含'add'和'iTextSharp.text的定义.Document不包含'newPage'的定义,而iTextSharp.text.Image不包含'scalePercent'的定义**

我已经添加了iText库(itextsharp,itextsharp.pdfa和itextshar.xtra)。这是我的代码:

       private void button3_Click_1(object sender, EventArgs e)
    {

        saveFileDialog1.FileName = "name.pdf";
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            using (Bitmap bitmap = new Bitmap(panel1.ClientSize.Width,
                                                panel1.ClientSize.Height))
            {
                panel1.DrawToBitmap(bitmap, panel1.ClientRectangle);
                bitmap.Save("C:\\" + (nPaginasPDF + 1) + ".bmp", ImageFormat.Bmp);
            }

            Document doc = new Document();
            PdfWriter.GetInstance(doc, new FileOutputStream(yourOutFile));
            doc.Open();

            for (int iCnt = 0; iCnt < nPaginasPDF; iCnt++)
            {


                iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance("C:\\" + (iCnt + 1) + ".bmp");
                image1.ScalePercent(23f);
                doc.NewPage();
                doc.Add(image1);
            }
            using (var Stream = saveFileDialog1.OpenFile())
            {
                doc.Save(Stream);
            }
            doc.Close();
        }

3 个答案:

答案 0 :(得分:2)

如果您使用iText文档或Java书籍,则需要对.NET进行一些调整。在您的示例中,由于.NET隐式getter和属性的setter,所以:

var instance = iTextSharp.Image.getInstance();

成为这个:

var instance = iTextSharp.Image.Instance;

第二个问题:Java中的方法名称是camel case,vs .NET pascal case,所以这个(camelCase):

image1.scalePercent(23f);
doc.newPage();
doc.add(image1);

成为这个(PascalCase):

image1.ScalePercent(23f);
doc.NewPage();
doc.Add(image1);

等等。只需应用 .NET代码命名约定而不是Java。

答案 1 :(得分:2)

@Nenad和@MaxStoun都是正确的,您只需要将Java约定适应.Net。此外,您还需要将Java FileOutputStream替换为.Net System.IO.FileStream对象。

修改

你需要解决一些“魔术变量”。例如,我不是100%肯定你正在使用for循环做什么,所以我只是为了这个样本删除了它。另外,我没有对c:\目录的写权限,而是保存到桌面。否则,此代码应该可以帮助您找到正确的路径。

  //I don't know what you're doing with this variable so I'm just setting it to something
  int nPaginasPDF = 10;

  //I can't write to my C: drive so I'm saving to the desktop
  string saveFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

  //Set the default file name
  saveFileDialog1.FileName = "name.pdf";

  //If the user presses "OK"
  if (saveFileDialog1.ShowDialog() == DialogResult.OK) {
      //Create a bitmap and save it to disk
      using (Bitmap bitmap = new Bitmap(panel1.ClientSize.Width, panel1.ClientSize.Height)) {
          panel1.DrawToBitmap(bitmap, panel1.ClientRectangle);
          //Path.Combine is a safer way to build file pathes
          bitmap.Save(System.IO.Path.Combine(saveFolder, nPaginasPDF + ".bmp"), ImageFormat.Bmp);
      }

      //Create a new file stream instance with some locks for safety
      using (var fs = new System.IO.FileStream(saveFileDialog1.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None)) {
          //Create our iTextSharp document
          using (var doc = new Document()) {
              //Bind a PdfWriter to the Document and FileStream
              using (var writer = PdfWriter.GetInstance(doc, fs)) {
                  //Open the document for writing
                  doc.Open();
                  //Get an instance of our image
                  iTextSharp.text.Image image1 = iTextSharp.text.Image.GetInstance(System.IO.Path.Combine(saveFolder, nPaginasPDF + ".bmp"));
                  //Sacle it
                  image1.ScalePercent(23f);
                  //Add a new page
                  doc.NewPage();
                  //Add our image to the document
                  doc.Add(image1);

                  //Close our document for writing
                  doc.Close();
              }
          }
      }
  }

答案 2 :(得分:1)

你将在方法名称中输入第一个字母(我刚从Nuget下载)

Image.getInstance(); => Image.GetInstance();    
doc.add(image1); => doc.Add(image1);