【问题标题】:iTextSharp - does not contain a definition for 'getInstance'iTextSharp - 不包含“getInstance”的定义
【发布时间】:2013-03-06 19:31:46
【问题描述】:

我正在尝试使用 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();
        }

【问题讨论】:

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


    【解决方案1】:

    @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();
                  }
              }
          }
      }
    

    【讨论】:

    • 请看我的编辑。我需要在“yourOutFile”中写什么?
    • 您的意思是为什么 PDF 中的图像比 Panel 中的小?那是因为你的代码写着image1.ScalePercent(23f); 如果你不想缩放图像,请删除它。
    【解决方案2】:

    如果您使用 iText 文档或 Java 书籍,则需要针对 .NET 进行一些调整。在您的示例中,由于 .NET 隐式获取器和设置器用于属性,因此:

    var instance = iTextSharp.Image.getInstance();
    

    变成这样:

    var instance = iTextSharp.Image.Instance;
    

    第二个问题:Java 中的方法名是驼峰式,而 .NET 是帕斯卡式,所以这个(驼峰式):

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

    变成这个(PascalCase):

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

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

    【讨论】:

      【解决方案3】:

      方法名的首字母大写(我刚从 Nuget 下载的)

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

      【讨论】:

      • 谢谢,它有效..但是我需要写什么而不是'yourOutFile'? '文档文档=新文档(); PdfWriter.GetInstance(doc, new FileOutputStream(yourOutFile));`
      猜你喜欢
      • 2017-12-04
      • 1970-01-01
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-27
      • 1970-01-01
      相关资源
      最近更新 更多