如何使用iTextSharp从c#windows窗体在pdf报告中的现有表格顶部添加整个新行 [英] how to add entire new row at top of existing table in pdf report from c# windows forms using iTextSharp

查看:188
本文介绍了如何使用iTextSharp从c#windows窗体在pdf报告中的现有表格顶部添加整个新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的10天里,我一直在讨论如何使用iTextSharp从c#windows表格添加pdf报告中创建的全新表格。



第一:我能够从c#windows窗体的pdf报告中以表格的形式创建/导出sql server数据。下面是c#中的代码。

hi for past 10 days i have been breaking my head on how to add entire new at top table created in pdf report from c# windows forms with iTextSharp.

First: I was able to create/export sql server data in form of table in pdf report from c# windows forms. Given below is the code in c#.

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Data;
 using System.IO;
using System.Data.SqlClient;
using System.Windows.Forms;
using iTextSharp.text;
using iTextSharp.text.pdf;
namespace DRRS_CSharp
{
    public partial class frmPDFTechnician : Form
    {
        public frmPDFTechnician()
        {
            InitializeComponent();
        }
private void btnExport_Click(object sender, EventArgs e)
        {
                Document doc = new Document(PageSize.A4.Rotate());
                var writer=  PdfWriter.GetInstance(doc, new FileStream("Technician22.pdf", FileMode.Create));
                doc.SetMargins(50, 50, 50, 50);
                doc.SetPageSize(new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.LETTER.Width, iTextSharp.text.PageSize.LETTER.Height));
                doc.Open();
                 PdfPTable table = new PdfPTable(7);
                table.TotalWidth=585f;
                table.LockedWidth = true;
                PdfPTable inner = new PdfPTable(1);
                inner.WidthPercentage = 115;
                PdfPCell celt=new PdfPCell(new Phrase(new Paragraph("Institute/Hospital:AIIMS,NEW DELHI",FontFactory.GetFont("Arial",14,iTextSharp.text.Font.BOLD,BaseColor.BLACK))));
                inner.AddCell(celt);
               
                Paragraph para = new Paragraph("DCS Clinical Report-Technician wise", FontFactory.GetFont("Arial", 14, iTextSharp.text.Font.BOLD, BaseColor.BLACK));
                para.Alignment = iTextSharp.text.Element.TITLE;
                iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance("logo5.png");
                png.ScaleToFit(95f, 95f);
                png.Alignment = Element.ALIGN_RIGHT;
                SqlConnection conn=new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
                SqlCommand cmd = new SqlCommand("Select t.technician_id,td.Technician_first_name,td.Technician_middle_name,td.Technician_last_name,t.technician_dob,t.technician_sex,td.technician_type from Techniciandetail td,Technician t where td.technician_id=t.technician_id and td.status=1", conn);
                conn.Open();
                SqlDataReader dr;
                dr = cmd.ExecuteReader();
                
                table.AddCell("ID");
                table.AddCell("First Name");
                table.AddCell("Middle Name");
                table.AddCell("Last Name");
                table.AddCell("DOB" );
                table.AddCell("Gender");
                table.AddCell("Designation");
                while (dr.Read())
                {
                    table.AddCell(dr[0].ToString());
                    table.AddCell(dr[1].ToString());
                    table.AddCell(dr[2].ToString());
                    table.AddCell(dr[3].ToString());
                    table.AddCell(dr[4].ToString());
                    table.AddCell(dr[5].ToString());
                    table.AddCell(dr[6].ToString());
                }
                dr.Close();
                    table.SpacingBefore = 15f;
                    
                    doc.Add(para);
                    doc.Add(png);
                    doc.Add(inner);
                    doc.Add(table);
                doc.Close();
            }
The code executes well with no problem and get all datas from tables into table in PDF report from c# windows forms.

But here is my problem how can i align Title(DCS Clinical Report-Technician wise) center of pdf report with image named:logo5.png immediately coming to it's right?.

As the problem i am facing is my title or Header(DCS Clinical Report-Technician wise) is at top of my image named:logo5.png and not coming to it's center position of my image.

Second the problem i am facing is how to add new entire row to top of existing table in pdf report from c# windows form using iTextSharp?.

As you can see how i add new row or table with text:Institute/Hospital:AIIMS,NEW DELHI through given below code in c#:
<pre lang="c#">
 PdfPTable table = new PdfPTable(7);
                table.TotalWidth=585f;
                table.LockedWidth = true;
                PdfPTable inner = new PdfPTable(1);
                inner.WidthPercentage = 115;
                PdfPCell celt=new PdfPCell(new Phrase(new Paragraph("Institute/Hospital:AIIMS,NEW DELHI",FontFactory.GetFont("Arial",14,iTextSharp.text.Font.BOLD,BaseColor.BLACK))));
                inner.AddCell(celt);
Paragraph para = new Paragraph("DCS Clinical Report-Technician wise", FontFactory.GetFont("Arial", 14, iTextSharp.text.Font.BOLD, BaseColor.BLACK));
                para.Alignment = iTextSharp.text.Element.TITLE;
                iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance("logo5.png");
                png.ScaleToFit(95f, 95f);
                png.Alignment = Element.ALIGN_RIGHT;
                SqlConnection conn=new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
                SqlCommand cmd = new SqlCommand("Select t.technician_id,td.Technician_first_name,td.Technician_middle_name,td.Technician_last_name,t.technician_dob,t.technician_sex,td.technician_type from Techniciandetail td,Technician t where td.technician_id=t.technician_id and td.status=1", conn);
                conn.Open();
                SqlDataReader dr;
                dr = cmd.ExecuteReader();
                
                table.AddCell("ID");
                table.AddCell("First Name");
                table.AddCell("Middle Name");
                table.AddCell("Last Name");
                table.AddCell("DOB" );
                table.AddCell("Gender");
                table.AddCell("Designation");
                while (dr.Read())
                {
                    table.AddCell(dr[0].ToString());
                    table.AddCell(dr[1].ToString());
                    table.AddCell(dr[2].ToString());
                    table.AddCell(dr[3].ToString());
                    table.AddCell(dr[4].ToString());
                    table.AddCell(dr[5].ToString());
                    table.AddCell(dr[6].ToString());
                }
                dr.Close();
                    table.SpacingBefore = 15f;
                    
                    doc.Add(para);
                    doc.Add(png);
                    doc.Add(inner);
                    doc.Add(table);
                doc.Close();



但是我如何将名为:inner的表正确地放在我名为table的表的顶部?

,你可以看到我如何在pdf报告中创建表中的列,并用sql server数据填充它。鉴于以下代码:


But how do i place table named:inner correctly on top of my table named:table?
as you can see how i create my columns in table in pdf report and populate it with sql server data. Given the code below:

Document doc = new Document(PageSize.A4.Rotate());
                var writer=  PdfWriter.GetInstance(doc, new FileStream("Technician22.pdf", FileMode.Create));
                doc.SetMargins(50, 50, 50, 50);
                doc.SetPageSize(new iTextSharp.text.Rectangle(iTextSharp.text.PageSize.LETTER.Width, iTextSharp.text.PageSize.LETTER.Height));
                doc.Open(); 
PdfPTable table = new PdfPTable(7);
                table.TotalWidth=585f;
                table.LockedWidth = true;
 Paragraph para = new Paragraph("DCS Clinical Report-Technician wise", FontFactory.GetFont("Arial", 14, iTextSharp.text.Font.BOLD, BaseColor.BLACK));
                para.Alignment = iTextSharp.text.Element.TITLE;
                iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance("logo5.png");
                png.ScaleToFit(95f, 95f);
                png.Alignment = Element.ALIGN_RIGHT;
SqlConnection conn=new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
                SqlCommand cmd = new SqlCommand("Select t.technician_id,td.Technician_first_name,td.Technician_middle_name,td.Technician_last_name,t.technician_dob,t.technician_sex,td.technician_type from Techniciandetail td,Technician t where td.technician_id=t.technician_id and td.status=1", conn);
                conn.Open();
                SqlDataReader dr;
                dr = cmd.ExecuteReader();
                
                table.AddCell("ID");
                table.AddCell("First Name");
                table.AddCell("Middle Name");
                table.AddCell("Last Name");
                table.AddCell("DOB" );
                table.AddCell("Gender");
                table.AddCell("Designation");
                while (dr.Read())
                {
                    table.AddCell(dr[0].ToString());
                    table.AddCell(dr[1].ToString());
                    table.AddCell(dr[2].ToString());
                    table.AddCell(dr[3].ToString());
                    table.AddCell(dr[4].ToString());
                    table.AddCell(dr[5].ToString());
                    table.AddCell(dr[6].ToString());
                }
                dr.Close();
                    table.SpacingBefore = 15f;
                    
                    doc.Add(para);
                    doc.Add(png);
 doc.Add(table);
                doc.Close();
            }



所以我的问题是如何使我的列标题以粗体显示?



所以这些是我的问题。



1.我如何将标题(DCS临床报告 - 技术人员明智的)pdf报告中心与图像命名:logo5.png立即对齐它是对的吗?



2.如何使用itextsharp从c#windows窗体中添加下面给定的行和它的数据到我的桌面pdf报告中?



3.如何以粗体显示我的列标题?



我知道我必须做一些修改我的代码,但我不知道该怎么做。任何人都可以帮助我。



任何帮助或指导解决这个问题将不胜感激。


So my questions are how to make my column headers in bold?

So these are my questions.

1. how can i align Title(DCS Clinical Report-Technician wise) center of pdf report with image named:logo5.png immediately coming to it's right?.

2. how do i add the given below row and it's data to my top my table in pdf report from c# windows forms using itextsharp?

3.how to make my column headers in bold?

I know that i have to do some modifications to my code but i dont know how to do it. Can anyone help me please.

Any help or guidance in solving this problem would be greatly appreciated.

推荐答案

这篇关于如何使用iTextSharp从c#windows窗体在pdf报告中的现有表格顶部添加整个新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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