使用Itextsharp进行打印 [英] Printing using Itextsharp

查看:93
本文介绍了使用Itextsharp进行打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人:

我有一个带有datagrid的表格,我已经通过将Itextsharp导出到PDF来完成表格和表格的打印,但是我在这里面临的问题是我无法创建自定义表格并定义表格字体的大小.但是我可以更改颜色并修改表单的其他内容,但无法访问table属性.在Pdf上,我仅获得默认字体和颜色.有人可以帮忙吗?


这是我的代码段:

此代码写在打印"按钮单击事件上.用打印一切都好,只有要实现的是填充和表格边距.

注意:: Itextsharp需要单独的"dll",名为itextsharp.dll


Dear All:

I have a form with datagrid I have accomplished printing of the grid and form using Itextsharp by exporting it to PDF but the problem I faced here is I am not being able to make custom table and define the size of the font of table .However I can change colors and modify other content of form but not being able to access the table property. On Pdf i get the default font and color only. Can anybody help here?


Here is my code snippet:

This code is written on print button click event. Every thing is fine with print only the thing to achieve is padding and margin of table.

Note::Itextsharp requires seprate "dll" called itextsharp.dll


// FontFactory.RegisterDirectory("C:\\WINDOWS\\Fonts");
           BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
           iTextSharp.text.Font times = new iTextSharp.text.Font(bfTimes, 8, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.RED);
           try
           {

               SqlConnection con = new SqlConnection(connectionstring);
               con.Open();

            //Query to populate values in datagrid

               string query = " select [Title] as [Book Title],Type.TypeName as [Type],Category.Name as [Category],ISBN,Author,\n"
            + "[Publisher] as [Publisher Name],Edition,Quantity,\n"
            + "[Requested Date]= CONVERT(char(12), Request_Book.entereddate, 113), [Expected Date]= CONVERT(char(12),\n"
            + " Request_Book.ExpectedDate, 113),Request_Book.Enteredby as [Requested By],Processed as Status \n"
            + "from Request_Book,Type,Category where  Type.TypeId=Request_Book.TypeId and \n"
            + "Category.CategoryId =Request_Book.CategoryId";

               SqlCommand cmd = new SqlCommand(query, con);
               SqlDataAdapter abc = new SqlDataAdapter(cmd);
               SqlDataReader dr = cmd.ExecuteReader();
               count = dr.FieldCount;

               string path = "D:/test.pdf";
               pdffunc(path);

               //  doc.Add(new Paragraph(textBox1.Text));

               doc.Add(new Paragraph("From:", times));//this is content of form
               doc.Add(new Paragraph(dtpDatePicker.Value.ToShortDateString(), times));
               doc.Add(new Paragraph(" "));
               doc.Add(new Paragraph("To:", times));//this is content of form
               doc.Add(new Paragraph(dateTimePicker1.Value.ToShortDateString(), times));
               doc.Add(new Paragraph(" "));

               PdfPTable table = new PdfPTable(count);
               PdfPCell cell = new PdfPCell(new Phrase("Print Request Book", times));
               cell.Colspan = 13;
               cell.HorizontalAlignment = 1;
               // table.AddCell(cell);
               table.AddCell("S.No");
               table.AddCell("Book Title");
               table.AddCell("Type");
               table.AddCell("Category");
               table.AddCell("ISBN");
               table.AddCell("Author");
               table.AddCell("Quantity");
               table.AddCell("Publisher Name");
               table.AddCell("Edition");
               table.AddCell("Requested Date");
               table.AddCell("Expected Date");
               table.AddCell("Requested By");
               table.AddCell("Status");

               while (dr.Read() && dr.HasRows)
               {
                   for (int i = 0; i < count; i++)
                   {
                       table.AddCell(dr[i].ToString());
                   }
               }
               dr.Close();
               //  table.SetWidthPercentage(50);
               doc.Add(table);

              doc.Close();

               System.Diagnostics.Process.Start("D:/test.pdf");

           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }

推荐答案

解决方案:

更改
solution:

change
table.AddCell("S.No");
                table.AddCell("Book Title");











to

PdfPCell = new PdfPCell(new Phrase(new Chunk("Book Title", times)));
                table.AddCell(PdfPCell);
                PdfPCell = new PdfPCell(new Phrase(new Chunk("Type", times)));
                table.AddCell(PdfPCell);



并且在datareader中,因为记录是从此处的数据库更改中获取的.




and in datareader ,since it the records are fetched from database change here.


while (dr.Read() && dr.HasRows)
               {
                   for (int i = 0; i < count; i++)
                   {
                       //table.AddCell(dr[i].ToString());
                       //table.AddCell(PdfPCell);
                       PdfPCell = new PdfPCell(new Phrase(new Chunk(dr[i].ToString(), times)));
                       table.AddCell(PdfPCell);
                   }


这篇关于使用Itextsharp进行打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆