0

ピクチャボックスやデータグリッドビューなどのさまざまなコンポーネントを含む c# のパネルがあります。datagridview 全体と画像ボックスを一緒に含む pdf を作成したいと考えています。今のところ、datagridview または画像ボックスのみが pdf に表示されます。両方を一緒にマージすることはできません。PDFの作成にiTextSharpを使用しています。私のコードは以下の通りです..

        string strFileName;

        string FontPath = "C:\\WINDOWS\\Fonts\\simsun.ttc,1";

        int FontSize = 12;

        ///

        Boolean cc = false;
        SaveFileDialog savFile = new SaveFileDialog();
        savFile.AddExtension = true;
        savFile.DefaultExt = "pdf";
        savFile.Filter = "PDF Document|*.pdf|*.pdf|";

        savFile.ShowDialog();

        if (savFile.FileName != "")
        {
            strFileName = savFile.FileName;
        }
        else
        {
            MessageBox.Show("export stop", "export stop", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }


        iTextSharp.text.Image jpg= iTextSharp.text.Image.GetInstance(Properties.Resources.templete3, System.Drawing.Imaging.ImageFormat.Png);

        jpg.ScaleToFit(750, 850);
        jpg.SetAbsolutePosition(0, 0);

        // Page site and margin left, right, top, bottom is defined
        Document pdfDoc = new Document(PageSize.A4);//, 10f, 10f, 10f, 0f);


        //If you want to choose image as background then,



        PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(strFileName, FileMode.Create));

        BaseFont baseFont = BaseFont.CreateFont(FontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, FontSize);

        pdfDoc.Open();

        PdfPTable table = new PdfPTable(dataGridView1.Columns.Count);


        for (int j = 0; j < dataGridView1.Columns.Count; j++)
        {
            table.AddCell(new Phrase(datagridview[j, 0].Value.ToString(), font));
        }

        table.HeaderRows = 1;

        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            for (int j = 0; j < dataGridView1.Columns.Count; j++)
            {
                try
                {
                    table.AddCell(new Phrase(dataGridView1[j, i].Value.ToString(), font));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    cc = true;
                }
            }
        }


        pdfDoc.NewPage();

        pdfDoc.Add(jpg);

        pdfDoc.Add(table);
        pdfDoc.Close();

        Process.Start(strFileName);

     }


}   
4

1 に答える 1

0

これは(まだ)答えではありません。あなたのコードはより大きなプロジェクトの一部のように見え、特定の問題に関係のない余分なコードがたくさんあります。このような問題を診断するための最初のステップは、問題を実際に再現できるように、重要でないものをすべて削除することです。以下は、それを行うための試みです。

このコードは、最初に、あなたが私たちに伝えたことに基づいてサンプル環境を作成します。まず、いくつかのサンプル データを作成し、それを に追加しますDataGridView。次に、既存のイメージを にロードしますPictureBox。次に、これら 2 つのコントロールを新しく作成した に追加しますPanel。これらの手順の後、PDF を作成し、 から画像をPictureBox取得し、テーブルからデータを取得して、それらすべてを PDF に追加します。詳細については、コード内のコメントを参照してください。このコードを実行すると、PDF に表と画像の両方が表示されます。

このコードを実行する場合は、新しいプロジェクトから始めてください - 既存のプロジェクトは使用しないでください。私はそれを十分に強調することはできません。このコードの一部を選択して実行しないでください。新しいプロジェクトを開始し、これをコードにのみ使用してください。これが機能する場合は、機能するコードと機能しないコードの違いを比較できることを願っています。

このコードは、VS Express 2012 For Windows Desktop で iTextSharp 5.4.0 に対してテストされました。

using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication20 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }
        //Used for our sample data
        public class Person {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }

        private void Form1_Load(object sender, EventArgs e) {
            //Sample image, set to a PNG
            var sampleImagePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "sample.png");
            //Full path to the PDF to export
            var exportFilePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");

            //Next we're going to create all of the basic controls per the OP's scenario

            //Create some sample data to put into our DGV
            var P1 = new Person() { FirstName = "Alice", LastName = "Cooper" };
            var P2 = new Person() { FirstName = "Bob", LastName = "Dole" };
            var People = new List<Person>(new Person[] { P1, P2 });

            //Create our sample DataGridView
            var dataGridView1 = new DataGridView();
            dataGridView1.AutoGenerateColumns = true;
            dataGridView1.DataSource = People;
            dataGridView1.Location = new Point(0, 0);

            //Create our sample PictureBox
            var PB = new PictureBox();
            PB.Load(sampleImagePath);
            PB.Location = new Point(400, 0);
            PB.SizeMode = PictureBoxSizeMode.AutoSize;


            //Create our sample panel and give it room to show everything
            var panel = new Panel();
            panel.AutoSize = true;
            panel.Dock = DockStyle.Fill;

            //Add the above controls to our DGV
            panel.Controls.Add(dataGridView1);
            panel.Controls.Add(PB);
            //Add the DGV to the form
            this.Controls.Add(panel);

            //Basic PDF creation here, nothing special
            using (var fs = new FileStream(exportFilePath, FileMode.Create, FileAccess.Write, FileShare.None)) {
                using (var pdfDoc = new Document(PageSize.A4)) {
                    using (var writer = PdfWriter.GetInstance(pdfDoc, fs)) {
                        pdfDoc.Open();

                        //Get our image (Code is mostly the OP's)
                        iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(PB.Image, System.Drawing.Imaging.ImageFormat.Png);
                        jpg.ScaleToFit(750, 850);
                        jpg.SetAbsolutePosition(0, 0);

                        //Create our table
                        var table = new PdfPTable(dataGridView1.Columns.Count);

                        //Add the headers from the DGV to the table
                        for (int j = 0; j < dataGridView1.Columns.Count; j++) {
                            table.AddCell(new Phrase(dataGridView1.Columns[j].HeaderText));
                        }

                        //Flag the first row as a header
                        table.HeaderRows = 1;

                        //Add the actual rows from the DGV to the table
                        for (int i = 0; i < dataGridView1.Rows.Count; i++) {
                            for (int j = 0; j < dataGridView1.Columns.Count; j++) {
                                table.AddCell(new Phrase(dataGridView1[j, i].Value.ToString()));
                            }
                        }

                        //Add our image
                        pdfDoc.Add(jpg);
                        //Add out table
                        pdfDoc.Add(table);
                        pdfDoc.Close();
                    }
                }
            }
        }
    }
}
于 2013-03-22T13:54:56.100 に答える