日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

c#endread怎么打印出来_C#教程之打印和打印预览

發布時間:2025/3/15 C# 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c#endread怎么打印出来_C#教程之打印和打印预览 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近研究一了一下關于PDF打印和打印預覽的功能,在此小小的總結記錄一下學習過程。

實現打印和打印預覽的方法,一般要實現如下的菜單項:打印、打印預覽、頁面設置、

PrintDocument類

PrintDocument組件是用于完成打印的類,其常用的屬性、方法事件如下:

屬性DocumentName:字符串類型,記錄打印文檔時顯示的文檔名(例如,在打印狀態對話框或打印機隊列中顯示),即用戶填寫生成pdf文件名時的默認值為DocumentName

方法Print:開始文檔的打印。

事件BeginPrint:在調用Print方法后,在打印文檔的第一頁之前發生。

事件PrintPage:需要打印新的一頁時發生。

事件EndPrint:在文檔的最后一頁打印后發生。

若要打印,首先創建PrintDocument組建的對象,然后使用頁面上設置對話框PageSetupDialog設置頁面打印方式,這些設置作為打印頁的默認設置、使用打印對話框PrintDialog設置對文檔進行打印的打印機的參數。在打開兩個對話框前,首先設置對話框的屬性Document為指定的PrintDocument類對象,修改的設置將保存到PrintDocument組件對象中。

第三步是調用PrintDocument.Print方法來實際打印文檔,調用該方法后,引發下列事件:BeginPrint、PrintPage、EndPrint。其中每打印一頁都引發PrintPage事件,打印多頁,要多次引發PrintPage事件。完成一次打印,可以引發一個或多個PrintPage事件。

///

/// 打印紙設置

///

///

///

protected void FileMenuItem_PageSet_Click(object sender, EventArgs e)

{

PageSetupDialog pageSetupDialog = new PageSetupDialog();

pageSetupDialog.Document = printDocument;

pageSetupDialog.ShowDialog();

}

///

/// 打印機設置

///

///

///

protected void FileMenuItem_PrintSet_Click(object sender, EventArgs e)

{

PrintDialog printDialog = new PrintDialog();

printDialog.Document = printDocument;

printDialog.ShowDialog();

}

///

/// 預覽功能

///

///

///

protected void FileMenuItem_PrintView_Click(object sender, EventArgs e)

{

PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog {Document = printDocument};

lineReader = new StreamReader(@"f:\新建文本文檔.txt");

try

{ // 腳本學堂 www.jbxue.com

printPreviewDialog.ShowDialog();

}

catch (Exception excep)

{

MessageBox.Show(excep.Message, "打印出錯", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

///

/// 打印功能

///

///

///

protected void FileMenuItem_Print_Click(object sender, EventArgs e)

{

PrintDialog printDialog = new PrintDialog {Document = printDocument};

lineReader = new StreamReader(@"f:\新建文本文檔.txt");

if (printDialog.ShowDialog() == DialogResult.OK)

{

try

{

printDocument.Print();

}

catch (Exception excep)

{

MessageBox.Show(excep.Message, "打印出錯", MessageBoxButtons.OK, MessageBoxIcon.Error);

printDocument.PrintController.OnEndPrint(printDocument, new PrintEventArgs());

}

}

}

程序員應為這3個事件編寫事件處理函數。BeginPrint事件處理函數進行打印初始化,一般設置在打印時所有頁的相同屬性或共用的資源,例如所有頁共同使用的字體、建立要打印的文件流等。PrintPage事件處理函數負責打印一頁數據。EndPrint事件處理函數進行打印善后工作。這些處理函數的第2個參數System.Drawing.Printing.PrintEventArgs e提供了一些附加信息,主要有:

l e.Cancel:布爾變量,設置為true,將取消這次打印作業。

l e.Graphics:所使用的打印機的設備環境,參見第五章。

l e.HasMorePages:布爾變量。PrintPage事件處理函數打印一頁后,仍有數據未打印,退出事件處理函數前設置HasMorePages=true,退出PrintPage事件處理函數后,將再次引發PrintPage事件,打印下一頁。

l e.MarginBounds:打印區域的大小,是Rectangle結構,元素包括左上角坐標:Left和Top,寬和高:Width和Height。單位為1/100英寸。

l e.MarginBounds:打印紙的大小,是Rectangle結構。單位為1/100英寸。

l e.PageSettings:PageSettings類對象,包含用對話框PageSetupDialog設置的頁面打印方式的全部信息。可用幫助查看PageSettings類的屬性。

注意:本例打印或預覽RichTextBox中的內容,增加變量:StringReader streamToPrint=null。如果打印或預覽文件,改為:StreamReader streamToPrint,

接下來用winform的例子具體實現一個小功能:

首先我們要生成的pdf 文件中的數據來源有:從其他文本中獲得,用戶將現有的數據按照某只格式輸出為pdf文件

首先介紹一下,讀取txt文件中的內容,生成pdf文件的具體代碼:

PrintDocument printDocument;

StreamReader lineReader = null;

public Form1()

{

InitializeComponent();

// 這里的printDocument對象可以通過將PrintDocument控件拖放到窗體上來實現,注意要設置該控件的PrintPage事件。

printDocument=new PrintDocument();

printDocument.DocumentName = "張海倫測試";

printDocument.PrintPage += new PrintPageEventHandler (this.printDocument_PrintPage);

}

///

/// 打印內容頁面布局

///

///

///

private void printDocument_PrintPage(object sender, PrintPageEventArgs e)

{

var g = e.Graphics; //獲得繪圖對象

float linesPerPage = 0; //頁面的行號

float yPosition = 0; //繪制字符串的縱向位置

var count = 0; //行計數器

float leftMargin = e.MarginBounds.Left; //左邊距

float topMargin = e.MarginBounds.Top; //上邊距

string line = null;

System.Drawing.Font printFont = this.textBox.Font; //當前的打印字體

BaseFont baseFont = BaseFont.CreateFont("f:\\STSONG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

var myBrush = new SolidBrush(Color.Black); //刷子

linesPerPage = e.MarginBounds.Height / printFont.GetHeight(g); //每頁可打印的行數

//逐行的循環打印一頁

while (count < linesPerPage && ((line = lineReader.ReadLine()) != null))

{

yPosition = topMargin + (count * printFont.GetHeight(g));

g.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());

count++;

}

}

///

/// 打印紙設置

///

///

///

protected void FileMenuItem_PageSet_Click(object sender, EventArgs e)

{

PageSetupDialog pageSetupDialog = new PageSetupDialog();

pageSetupDialog.Document = printDocument;

pageSetupDialog.ShowDialog();

}

///

/// 打印機設置

///

///

///

protected void FileMenuItem_PrintSet_Click(object sender, EventArgs e)

{

PrintDialog printDialog = new PrintDialog();

printDialog.Document = printDocument;

printDialog.ShowDialog();

}

///

/// 預覽功能

///

///

///

protected void FileMenuItem_PrintView_Click(object sender, EventArgs e)

{

PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog {Document = printDocument};

lineReader = new StreamReader(@"f:\新建文本文檔.txt");

try

{ // 腳本學堂 www.jbxue.com

printPreviewDialog.ShowDialog();

}

catch (Exception excep)

{

MessageBox.Show(excep.Message, "打印出錯", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}

///

/// 打印功能

///

///

///

protected void FileMenuItem_Print_Click(object sender, EventArgs e)

{

PrintDialog printDialog = new PrintDialog {Document = printDocument};

lineReader = new StreamReader(@"f:\新建文本文檔.txt");

if (printDialog.ShowDialog() == DialogResult.OK)

{

try

{

printDocument.Print();

}

catch (Exception excep)

{

MessageBox.Show(excep.Message, "打印出錯", MessageBoxButtons.OK, MessageBoxIcon.Error);

printDocument.PrintController.OnEndPrint(printDocument, new PrintEventArgs());

}

}

}

其次,根據現有數據數據某種文本樣式的pdf文件具體代碼如下:

///GetPrintSw方法用來構造打印文本,內部StringBuilder.AppendLine在Drawstring時單獨占有一行。

public StringBuilder GetPrintSW()

{

StringBuilder sb = new StringBuilder();

string tou = "測試管理公司名稱";

string address = "河南洛陽";

string saleID = "2010930233330"; //單號

string item = "項目";

decimal price = 25.00M;

int count = 5;

decimal total = 0.00M;

decimal fukuan = 500.00M;

sb.AppendLine(" " + tou + " \n");

sb.AppendLine("--------------------------------------");

sb.AppendLine("日期:" + DateTime.Now.ToShortDateString() + " " + "單號:" + saleID);

sb.AppendLine("-----------------------------------------");

sb.AppendLine("項目" + " " + "數量" + " " + "單價" + " " + "小計");

for (int i = 0; i < count; i++)

{

decimal xiaoji = (i + 1) * price;

sb.AppendLine(item + (i + 1) + " " + (i + 1) + " " + price + " " + xiaoji);

total += xiaoji;

}

sb.AppendLine("-----------------------------------------");

sb.AppendLine("數量:" + count + " 合計: " + total);

sb.AppendLine("付款:" + fukuan);

sb.AppendLine("現金找零:" + (fukuan - total));

sb.AppendLine("-----------------------------------------");

sb.AppendLine("地址:" + address + "");

sb.AppendLine("電話:123456789 123456789");

sb.AppendLine("謝謝惠顧歡迎下次光臨 ");

sb.AppendLine("-----------------------------------------");

return sb;

}

最后我們在軟件中,經常使用的是將現有的某條記錄生成一個pdf文件表格,里面有用戶從數據庫中獲取的值。具體代碼如下:

private void printDocument_PrintPage(object sender, PrintPageEventArgs e)

{

Font titleFont = new Font("宋體", 9, FontStyle.Bold);//標題字體

Font font = new Font("宋體", 9, FontStyle.Regular);//正文文字

Brush brush = new SolidBrush(Color.Black);//畫刷

Pen pen = new Pen(Color.Black); //線條顏色

Point po = new Point(10, 10);

try

{

e.Graphics.DrawString(GetPrintSW().ToString(), titleFont, brush, po); //DrawString方式進行打印。

int length = 500;

int height = 500;

Graphics g = e.Graphics;//利用該圖片對象生成“畫板”

Pen p = new Pen(Color.Red, 1);//定義了一個紅色,寬度為的畫筆

g.Clear(Color.White); //設置黑色背景

//一排數據

g.DrawRectangle(p, 100, 100, 80, 20);//在畫板上畫矩形,起始坐標為(10,10),寬為80,高為20

g.DrawRectangle(p, 180, 100, 80, 20);//在畫板上畫矩形,起始坐標為(90,10),寬為80,高為20

g.DrawRectangle(p, 260, 100, 80, 20);//

g.DrawRectangle(p, 340, 100, 80, 20);//

g.DrawString("目標", font, brush, 12, 12);//

g.DrawString("完成數", font, brush, 92, 12);

g.DrawString("完成率", font, brush, 172, 12);//進行繪制文字。起始坐標為(172, 12)

g.DrawString("效率", font, brush, 252, 12);//關鍵的一步,進行繪制文字。

g.DrawRectangle(p, 10, 30, 80, 20);

g.DrawRectangle(p, 90, 30, 80, 20);

g.DrawRectangle(p, 170, 30, 80, 20);

g.DrawRectangle(p, 250, 30, 80, 20);

g.DrawString("800", font, brush, 12, 32);

g.DrawString("500", font, brush, 92, 32);//關鍵的一步,進行繪制文字。

g.DrawString("60%", font, brush, 172, 32);//關鍵的一步,進行繪制文字。

g.DrawString("50%", font, brush, 252, 32);//關鍵的一步,進行繪制文字。

g.DrawRectangle(p, 10, 50, 80, 20);

g.DrawRectangle(p, 90, 50, 80, 20);

g.DrawRectangle(p, 170, 50, 160, 20);//在畫板上畫矩形,起始坐標為(170,10),寬為160,高為20

g.DrawString("總查數", font, brush, 12, 52);

g.DrawString("不良數", font, brush, 92, 52);

g.DrawString("合格率", font, brush, 222, 52);

g.Dispose();//釋放掉該資源

}

catch (Exception ex)

{

MessageBox.Show(this, "打印出錯!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

}

效果圖如:

上面這3個例子,均是在winform中實現的,最后一個功能的實現比較復雜,不是很好,

下面是倆個wpf實現打印的例子,

簡單的一個具體代碼有:

public MainWindow()

{

InitializeComponent();

}

///

/// 我得第一個Pdf程序

///

private void CreatePdf()

{

string fileName = string.Empty;

Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();

dlg.FileName = "我的第一個PDF";

dlg.DefaultExt = ".pdf";

dlg.Filter = "Text documents (.pdf)|*.pdf";

Nullable result = dlg.ShowDialog();

if (result == true)

{

fileName = dlg.FileName;

Document document = new Document();

PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));

document.Open();

iTextSharp.text.Paragraph paragraph = new iTextSharp.text.Paragraph("Hello World");

document.Add(paragraph);

document.Close();

}//end if

}

///

/// 設置頁面大小、作者、標題等相關信息設置

///

private void CreatePdfSetInfo()

{

string fileName = string.Empty;

Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();

dlg.FileName = "我的第一個PDF";

dlg.DefaultExt = ".pdf";

dlg.Filter = "Text documents (.pdf)|*.pdf";

Nullable result = dlg.ShowDialog();

if (result == true)

{

fileName = dlg.FileName;

//設置頁面大小

iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(216f, 716f);

pageSize.BackgroundColor = new iTextSharp.text.BaseColor(0xFF, 0xFF, 0xDE);

//設置邊界

Document document = new Document(pageSize, 36f, 72f, 108f, 180f);

PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));

// 添加文檔信息

document.AddTitle("PDFInfo");

document.AddSubject("Demo of PDFInfo");

document.AddKeywords("Info, PDF, Demo");

document.AddCreator("SetPdfInfoDemo");

document.AddAuthor("焦濤");

document.Open();

// 添加文檔內容

for (int i = 0; i < 5; i++)

{

document.Add(new iTextSharp.text.Paragraph("Hello World! Hello People! " +"Hello Sky! Hello Sun! Hello Moon! Hello Stars!"));

}

document.Close();

}//end if

}

///

/// 創建多個Pdf新頁

///

private void CreateNewPdfPage()

{

string fileName = string.Empty;

Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();

dlg.FileName = "創建多個Pdf新頁";//生成的pdf文件名

dlg.DefaultExt = ".pdf";//pdf的默認后綴名

dlg.Filter = "Text documents (.pdf)|*.pdf";

Nullable result = dlg.ShowDialog();

if (result == true)

{

fileName = dlg.FileName;

Document document = new Document(PageSize.NOTE);

PdfWriter writer= PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));

document.Open();

// 第一頁

document.Add(new iTextSharp.text.Paragraph("PDF1, PDF1, PDF1, PDF1, PDF1"));

document.Add(new iTextSharp.text.Paragraph("PDF1, PDF1, PDF1, PDF1, PDF1"));

document.Add(new iTextSharp.text.Paragraph("PDF1, PDF1, PDF1, PDF1, PDF1"));

document.Add(new iTextSharp.text.Paragraph("PDF1, PDF1, PDF1, PDF1, PDF1"));

// 添加新頁面

document.NewPage();

// 第二頁

// 添加第二頁內容

document.Add(new iTextSharp.text.Paragraph("PDF2, PDF2, PDF2, PDF2, PDF2"));

document.Add(new iTextSharp.text.Paragraph("PDF2, PDF2, PDF2, PDF2, PDF2"));

document.Add(new iTextSharp.text.Paragraph("PDF2, PDF2, PDF2, PDF2, PDF2"));

document.Add(new iTextSharp.text.Paragraph("PDF2, PDF2, PDF2, PDF2, PDF2"));

document.Add(new iTextSharp.text.Paragraph("PDF2, PDF2, PDF2, PDF2, PDF2"));

document.Add(new iTextSharp.text.Paragraph("PDF2, PDF2, PDF2, PDF2, PDF2"));

// 添加新頁面

document.NewPage();

// 第三頁

// 添加新內容

document.Add(new iTextSharp.text.Paragraph("PDF3, PDF3, PDF3, PDF3, PDF3"));

document.Add(new iTextSharp.text.Paragraph("PDF3, PDF3, PDF3, PDF3, PDF3"));

document.Add(new iTextSharp.text.Paragraph("PDF3, PDF3, PDF3, PDF3, PDF3"));

document.Add(new iTextSharp.text.Paragraph("PDF3, PDF3, PDF3, PDF3, PDF3"));

// 重新開始頁面計數

document.ResetPageCount();

// 新建一頁

document.NewPage();

// 第四頁

// 添加第四頁內容

document.Add(new iTextSharp.text.Paragraph("PDF4, PDF4, PDF4, PDF4, PDF4"));

document.Add(new iTextSharp.text.Paragraph("PDF4, PDF4, PDF4, PDF4, PDF4"));

document.Add(new iTextSharp.text.Paragraph("PDF4, PDF4, PDF4, PDF4, PDF4"));

document.Add(new iTextSharp.text.Paragraph("PDF4, PDF4, PDF4, PDF4, PDF4"));

document.Close();

}//end if

}

///

/// 生成圖片pdf頁(pdf中插入圖片)

///

public void ImageDirect()

{

string imagePath = AppDomain.CurrentDomain.BaseDirectory + @"Image\1.jpg"; //臨時文件路徑

string fileName = string.Empty;

Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();

dlg.FileName = "我的第一個PDF";

dlg.DefaultExt = ".pdf";

dlg.Filter = "Text documents (.pdf)|*.pdf";

Nullable result = dlg.ShowDialog();

if (result == true)

{

fileName = dlg.FileName;

Document document = new Document();

PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));

document.Open();

iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imagePath);

img.SetAbsolutePosition((PageSize.POSTCARD.Width - img.ScaledWidth) / 2, (PageSize.POSTCARD.Height - img.ScaledHeight) / 2);

writer.DirectContent.AddImage(img);

iTextSharp.text.Paragraph p = new iTextSharp.text.Paragraph("Foobar Film Festival", new iTextSharp.text.Font(Font.FontFamily.HELVETICA, 22f));

p.Alignment = Element.ALIGN_CENTER;

document.Add(p);

document.Close();

}//end if

}

private void ReadPdf()

{

Console.WriteLine("讀取PDF文檔");

try

{

// 創建一個PdfReader對象

PdfReader reader = new PdfReader(@"D:\我的第一個PDF.pdf");

// 獲得文檔頁數

int n = reader.NumberOfPages;

// 獲得第一頁的大小

iTextSharp.text.Rectangle psize = reader.GetPageSize(1);

float width = psize.Width;

float height = psize.Height;

// 創建一個文檔變量

Document document = new Document(psize, 50, 50, 50, 50);

// 創建該文檔

PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(@"d:\Read.pdf", FileMode.Create));

// 打開文檔

document.Open();

// 添加內容

PdfContentByte cb = writer.DirectContent;

int i = 0;

int p = 0;

Console.WriteLine("一共有 " + n + " 頁.");

while (i < n)

{

document.NewPage();

p++;

i++;

PdfImportedPage page1 = writer.GetImportedPage(reader, i);

cb.AddTemplate(page1, .5f, 0, 0, .5f, 0, height / 2);

Console.WriteLine("處理第 " + i + " 頁");

if (i < n)

{

i++;

PdfImportedPage page2 = writer.GetImportedPage(reader, i);

cb.AddTemplate(page2, .5f, 0, 0, .5f, width / 2, height / 2);

Console.WriteLine("處理第 " + i + " 頁");

}

if (i < n)

{

i++;

PdfImportedPage page3 = writer.GetImportedPage(reader, i);

cb.AddTemplate(page3, .5f, 0, 0, .5f, 0, 0);

Console.WriteLine("處理第 " + i + " 頁");

}

if (i < n)

{

i++;

PdfImportedPage page4 = writer.GetImportedPage(reader, i);

cb.AddTemplate(page4, .5f, 0, 0, .5f, width / 2, 0);

Console.WriteLine("處理第 " + i + " 頁");

}

cb.SetRGBColorStroke(255, 0, 0);

cb.MoveTo(0, height / 2);

cb.LineTo(width, height / 2);

cb.Stroke();

cb.MoveTo(width / 2, height);

cb.LineTo(width / 2, 0);

cb.Stroke();

BaseFont bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);

cb.BeginText();

cb.SetFontAndSize(bf, 14);

cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p + " of " + ((n / 4) + (n % 4 > 0 ? 1 : 0)), width / 2, 40, 0);

cb.EndText();

}

// 關閉文檔

document.Close();

}

catch (Exception de)

{

Console.Error.WriteLine(de.Message);

Console.Error.WriteLine(de.StackTrace);

}

}

///

/// 創建表格

///

public void CreateFirstTable()

{

string imagePath = AppDomain.CurrentDomain.BaseDirectory + @"Image\1.pm"; //臨時文件路徑

string fileName = string.Empty;

Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();

dlg.FileName = "我的第一個PDF";

dlg.DefaultExt = ".pdf";

dlg.Filter = "Text documents (.pdf)|*.pdf";

Nullable result = dlg.ShowDialog();

BaseFont baseFont = BaseFont.CreateFont("D:\\STSONG.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

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

if (result == true)

{

fileName = dlg.FileName;

Document document = new Document();

PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileName, FileMode.Create));

document.Open();

iTextSharp.text.Paragraph p;

p = new iTextSharp.text.Paragraph("中華人民共和國海關出口貨物打單", font);

p.Alignment = Element.ALIGN_CENTER;//設置標題居中

p.SpacingAfter = 12;//設置段落行 通過設置Paragraph的SpacingBefore和SpacingAfter屬性調整Paragraph對象與之間或之后段落的間距

p.SpacingBefore = 1;

document.Add(p);//添加段落

p = new iTextSharp.text.Paragraph(GetBlank(5)+"預錄入編號:" +"編號代碼"+GetBlank(15)+"海關編號:"+GetBlank(5),font);

//p.IndentationLeft = 20;

//p.IndentationLeft = 20;

//p.IndentationRight = 20;

//p.FirstLineIndent = 20;

//IndentationLeft屬性設置左側縮進。

//IndentationRight屬性設置右側縮進。

p.SpacingAfter = 12;

document.Add(p);//添加段落

PdfPTable table = new PdfPTable(10);//幾列

PdfPCell cell;

cell=new PdfPCell(new Phrase("收發貨人"+GetBlank(5)+"具體值"));

cell.Colspan = 4;

table.AddCell(cell);

cell = new PdfPCell(new Phrase("出關口岸"+GetBlank(10)+"具體值"));

cell.Rowspan = 2;

table.AddCell(cell);

cell = new PdfPCell(new Phrase("出口日期" + GetBlank(10) + "具體值"));

cell.Rowspan = 2;

table.AddCell(cell);

cell = new PdfPCell(new Phrase("申報日期" + GetBlank(10) + "具體值"));

cell.Rowspan = 2;

table.AddCell(cell);

cell = new PdfPCell(new Phrase("收發貨人" + GetBlank(5) + "具體值"));

cell.Colspan = 4;

table.AddCell(cell);

cell = new PdfPCell(new Phrase("出關口岸" + GetBlank(10) + "具體值"));

cell.Rowspan = 2;

table.AddCell(cell);

cell = new PdfPCell(new Phrase("出口日期" + GetBlank(10) + "具體值"));

cell.Rowspan = 2;

table.AddCell(cell);

cell = new PdfPCell(new Phrase("申報日期" + GetBlank(10) + "具體值"));

cell.Rowspan = 2;

table.AddCell(cell);

//table.AddCell("row 1; cell 1");

//table.AddCell("row 1; cell 2");

//table.AddCell("row 2; cell 1");

//table.AddCell("row 2; cell 2");

document.Add(table);

document.Close();

}//end if

}

///

/// 獲得空格

///

///

///

private static string GetBlank(int num)

{

StringBuilder blank = new StringBuilder();

for (int i = 0; i < num; i++)

{

blank.Append(" ");

}

return blank.ToString();

}

private void button1_Click(object sender, RoutedEventArgs e)

{

//CreatePdf();

//CreatePdfPageSize();

CreateNewPdfPage();

}

private void button2_Click(object sender, RoutedEventArgs e)

{

CreateFirstTable();

}

private void button3_Click(object sender, RoutedEventArgs e)

{

ImageDirect();

}

private void button4_Click(object sender, RoutedEventArgs e)

{

ReadPdf();

}

在這里用到了iTextSharp ,需要先先下載dll文件,然后引用,總結一下其中常用的用法和屬性之類的知識點,

PdfWriter的setInitialLeading操作用于設置行間距

Font font = new Font(Font.FontFamily.COURIER, 12, Font.BOLD, BaseColor.WHITE);

設置縮進

iTextSharp中,Paragraph有三個屬性可以設置縮進:

//設置Paragraph對象的縮進

contentPara1.IndentationLeft = 20;

contentPara1.IndentationRight = 20;

contentPara1.FirstLineIndent = 20;

IndentationLeft屬性設置左側縮進。

IndentationRight屬性設置右側縮進。

FirstLineIndent屬性設置首行左側縮進。

三個值都可設為正負值。

設置對齊方式

設置Alignment屬性可以調整Paragraph對象中文字的對齊方式。如:

//設置Paragraph對象的對齊方式為兩端對齊

contentPara1.Alignment = Element.ALIGN_JUSTIFIED;

默認情況使用左對齊。

Paragraph之間的間距

iTextSharp中,通過設置Paragraph的SpacingBefore和SpacingAfter屬性調整Paragraph對象與之間或之后段落的間距。例如:

//設置Paragraph對象與后面Paragraph對象之間的間距

contentPara1.SpacingAfter = 36;

文字分行問題

iText默認的規則是盡可能多的將完整單詞放在同一行內。iText當遇到空格或連字符才會分行,可以通過重新定義分隔符(split character)來改變這種規則。

分隔符(the split character)

使用nonbreaking space character,(char)160代替普通空格(char)32放入兩個單詞中間從而避免iText將它們放到不同行中。

最好的是自己設計界面和功能當做模板使用,綁定數據實現如winform第三個例子樣的功能。

總結

以上是生活随笔為你收集整理的c#endread怎么打印出来_C#教程之打印和打印预览的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。