0

iTextSharpを使用して画像からPDFを生成しようとしていますが、次のエラーが発生します:iTextSharp.Imageに「getInstance」の定義が含まれておらず、「iTextSharp.text.Documentに「add」の定義が含まれていません」 'iTextSharp.text.Documentには'newPage'の定義が含まれておらず、iTextSharp.text.Imageには'scalePercent'の定義が含まれていません**

すでにiTextライブラリ(itextsharp、itexsharp.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();
        }
4

3 に答える 3

2

Java用のiTextドキュメントまたは書籍を使用する場合は、.NET用に少し調整する必要があります。あなたの例では、プロパティの.NET暗黙のゲッターとセッターなので、これは次のようになります。

var instance = iTextSharp.Image.getInstance();

これになります:

var instance = iTextSharp.Image.Instance;

2番目の問題:Javaのメソッド名はキャメルケースと.NETパスカルケースであるため、これ(camelCase):

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

これになります(PascalCase):

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

等々。Javaの代わりに.NETコードの命名規則を適用するだけです。

于 2013-03-18T12:52:57.263 に答える
2

@Nenadと@MaxStounはどちらも正しいので、Javaの規則を.Netに適合させる必要があります。FileOutputStreamさらに、Javaを.NetSystem.IO.FileStreamオブジェクトに交換する必要もあります。

編集

そこには、回避する必要のある「魔法の変数」がいくつかあります。たとえば、forループで何をしているのか100%わからないので、このサンプルではループを削除しました。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();
              }
          }
      }
  }
于 2013-03-18T12:59:33.783 に答える
1

メソッド名の最初の文字を上に表示します(Nugetからダウンロードしたばかりです)

Image.getInstance(); => Image.GetInstance();    
doc.add(image1); => doc.Add(image1);    
于 2013-03-18T12:53:59.330 に答える