翻譯|使用教程|編輯:李顯亮|2019-09-26 10:07:56.833|閱讀 1415 次
概述:當打印PDF文檔的時候,默認情況下紙張大小是A4。但是有些情況下原文檔不是A4大小,我們需要保持原來的頁面大小怎么辦呢?
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.PDF是一個專業的PDF組件,能夠獨立地創建、編寫、編輯、操作和閱讀PDF文件,支持 .NET、Java、WPF和Silverlight。Spire.PDF的PDF API擁有豐富的功能,如安全設置(包括數字簽名)、PDF文本/附件/圖片提取、PDF文件合并/拆分、元數據更新、章節和段落優化、圖形/圖像描繪和插入、表格創建和處理、數據導入等等。
>>Spire.PDF更新至最新版v5.9.6,歡迎下載體驗
當打印PDF文檔的時候,默認情況下紙張大小是A4。但是有些情況下原文檔不是A4大小,我們需要保持原來的頁面大小怎么辦呢?這個時候就需要自定義紙張大小來實現這個功能。
情況1:使用虛擬打印機自定義紙張大小打印,比如保持原來頁面大小打印到PDF。
//加載需要打印的PDF文檔 Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument(); doc.LoadFromFile(FileName); //獲取原文檔第一頁的紙張大小,這里的單位是Point SizeF size = doc.Pages[0].Size; //實例化PaperSize對象,設置其寬高 //需要特別注意的是這里涉及到單位的轉換,PaperSize的寬高參數默認單位是百英寸 PaperSize paper = new PaperSize("Custom", (int)size.Width/72*100, (int)size.Height/72*100); paper.RawKind = (int)PaperKind.Custom; //設置打印的紙張大小為原來文檔的大小 doc.PrintSettings.PaperSize = paper; //需要選擇FitSize打印模式 doc.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.FitSize, true); //打印 doc.Print();
情況2:使用真實打印機設置紙盒中的紙張大小打印,比如原來A4的文檔打印成A3的大小。
//加載需要打印的PDF文檔 Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument(); doc.LoadFromFile(FileName); PaperSize p = null; //實例化一個PrintDocument對象來獲取當前打印機的紙盒信息 PrintDocument printDoc = new PrintDocument(); //遍歷打印機紙盒里面的紙張,找到需要的A3 foreach (PaperSize ps in printDoc.PrinterSettings.PaperSizes) { if (ps.PaperName.Equals("A3")) { p = ps; break; } } //設置打印的紙張大小為A3 doc.PrintSettings.PaperSize = p; //打印 doc.PrintSettings.SelectSinglePageLayout(PdfSinglePageScalingMode.FitSize, true); doc.Print();
代碼:
//加載PDF文檔 PdfDocument doc = new PdfDocument("Input.pdf"); doc.AllowCreateForm = true; //在第一頁創建一個PdfButtonField實例,并為按鈕設置屬性 PdfPageBase page = doc.Pages[0]; PdfButtonField button = new PdfButtonField(page, "Print"); //設置按鈕屬性 button.Bounds = new RectangleF(280, 600, 50, 20); button.BorderColor = new PdfRGBColor(Color.AliceBlue); button.BorderStyle = PdfBorderStyle.Solid; button.ForeColor = new PdfRGBColor(Color.White); button.BackColor = new PdfRGBColor(Color.Blue); button.ToolTip = "Print"; button.Text = "Print"; button.Font = new PdfFont(PdfFontFamily.Helvetica, 9f); //將打印功能添加到按鈕中 button.AddPrintAction(); //添加按鈕 doc.Form.Fields.Add(button); //保存文檔 doc.SaveToFile("Output.pdf");
效果圖:
*國慶大放價 ,不限量超值優惠券免費領取,更多活動詳情可了解哦~
掃描關注“慧聚IT”微信公眾號,及時獲取更多產品最新動態及最新資訊
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn