原創|其它|編輯:王香|2017-10-19 15:02:10.000|閱讀 383 次
概述:Spire.PDF 是一個專業的PDF組件,能夠獨立地創建、編寫、編輯、操作和閱讀PDF文件,支持 .NET、WPF和Silverlight三個版本,本文介紹了Spire.PDF 常見的技術問題。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Q.如何將HTML代碼轉換為PDF?
A:Spire.PDF無法加載包含html代碼的字符串。 但是Spire.Doc可以加載它,Spire.Doc支持PDF格式。 所以你可以使用Spire.Doc做這個工作。 全部代碼:
string htmlstring = "<!DOCTYPE html><html><body><h1>Header 1</h1><p>First paragraph</p></body></html>"; Document doc = new Document(); Section sec = doc.AddSection(); Paragraph para = sec.AddParagraph(); //add html code to document para.AppendHTML(htmlstring); //save document as PDF format doc.SaveToFile("result.pdf", FileFormat.PDF);
Q.如何在表的單元格中嵌入另一個表?
A:表格是一個更簡單的網格。 在網格中,您可以操縱每個單元格,為每個單元格設置不同的樣式并嵌入另一個網格。 所以你可以使用網格來完成這個工作。 全部代碼:
PdfDocument document = new PdfDocument(); PdfPageBase page = document.Pages.Add(PdfPageSize.A4); PdfGrid grid = new PdfGrid(); grid.Columns.Add(1); grid.Columns[0].Width = page.Canvas.ClientSize.Width; PdfGridRow row0 = grid.Rows.Add(); row0.Cells[0].Value = "This is the first row."; row0.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); PdfGridRow row1 = grid.Rows.Add(); PdfLayoutResult result=grid.Draw(page, new PointF(0, 20)); PdfGrid grid2 = new PdfGrid(); grid2.Columns.Add(2); PdfGridRow newrow = grid2.Rows.Add(); grid2.Columns[0].Width = grid.Columns[0].Width / 2; grid2.Columns[1].Width = grid.Columns[0].Width / 2; newrow.Cells[0].Value = "This is row two column one."; newrow.Cells[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); newrow.Cells[1].Value = "This is row two column two."; newrow.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); //assign grid2 to row1 row1.Cells[0].Value = grid2; //drwa grid2 result = grid2.Draw(page, new PointF(0, result.Bounds.Location.Y + result.Bounds.Height)); document.SaveToFile("result.pdf");
Q.如何在網格中合并單元格?
A:pire.PDF為您提供名為RowSpan和ColumnSpan的屬性來合并單元格。 全部代碼:
PdfDocument doc = new PdfDocument(); PdfPageBase page = doc.Pages.Add(); PdfGrid grid = new PdfGrid(); grid.Columns.Add(5); float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1); for (int i = 0; i < grid.Columns.Count; i++) { grid.Columns[i].Width = width * 0.20f; } PdfGridRow row0 = grid.Rows.Add(); PdfGridRow row1 = grid.Rows.Add(); row0.Style.Font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold), true); row1.Style.Font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Italic), true); row0.Cells[0].Value = "Corporation"; //merge with the downside cell row0.Cells[0].RowSpan = 2; row0.Cells[1].Value = "B&K Undersea Photo"; row0.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); //merge with the right cell row0.Cells[1].ColumnSpan = 3; row0.Cells[4].Value = "World"; row0.Cells[4].Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold | FontStyle.Italic), true); row0.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row0.Cells[4].Style.BackgroundBrush = PdfBrushes.LightGreen; row1.Cells[1].Value = "Diving International Unlimited"; row1.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle); row1.Cells[1].ColumnSpan = 4; grid.Draw(page, new PointF(0, 0)); doc.SaveToFile("result.pdf");
Q.如何將簽名添加到PDF文件?
A:首先,使用類PdfCertificate創建一個證書實例。 證書實例將用于創建PdfSignature實例。 然后使用類PdfSignature創建一個簽名實例。 并且您需要設置簽名實例的屬性。 全部代碼:
PdfDocument doc = new PdfDocument(); doc.LoadFromFile("sample.pdf"); PdfCertificate cert = new PdfCertificate("Demo.pfx", "e-iceblue"); //add signature to every page of PDF file foreach (PdfPageBase page in doc.Pages) { PdfSignature signature = new PdfSignature(page.Document, page, cert, "demo"); signature.ContactInfo = "Harry"; signature.Certificated = true; signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill; } doc.SaveToFile("result.pdf");
Q.如何重新排列PDF文件的頁面?
A:請調用ReArrange方法。 該方法以int數組為參數。 int數組表示頁面的新順序。 全部代碼:
PdfDocument document = new PdfDocument(); //sample.pdf has four pages document.LoadFromFile("sample.pdf"); //rearrange the pages of the PDF file int[] range = new int[] { 0, 2, 1, 3 }; document.Pages.ReArrange(range); document.SaveToFile("result.pdf");
Q.如何添加圖像水印?
A:Spire.PDF不直接支持圖像水印。 但您可以將圖像設置為BackgroundImage。 如果圖像足夠大,則BackgroundImage將像圖像水印。 全部代碼:
PdfDocument document = new PdfDocument(); PdfPageBase page = document.Pages.Add(PdfPageSize.A4); page.Canvas.DrawString("This is a demo about image watermark!", new PdfFont(PdfFontFamily.Helvetica, 25f), new PdfSolidBrush(Color.Green), 10, 40); //set image as BackgroundImage to make image watermark Image img = Image.FromFile("scene.bmp"); page.BackgroundImage = img; document.SaveToFile("result.pdf");
Q.如何在所有頁面上重復標題?
A:PdfDocumentTemplate中的內容將適用于PDF文件的每一頁。 所有你需要做的是創建一個方法,添加頭到PdfDocumentTemplate。 然后標題將添加到每個頁面。 全部代碼:
static void Main(string[] args) { PdfDocument doc = new PdfDocument(); PdfUnitConvertor unitCvtr = new PdfUnitConvertor(); PdfMargins margin = new PdfMargins(); margin.Top = unitCvtr.ConvertUnits(3.0f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Bottom = margin.Top; margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point); margin.Right = margin.Left; // create three page PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin); page = doc.Pages.Add(PdfPageSize.A4, margin); page = doc.Pages.Add(PdfPageSize.A4, margin); //apply template SetDocumentTemplate(doc, PdfPageSize.A4, margin); doc.SaveToFile("result.pdf"); } //method to add header to every page private static void SetDocumentTemplate(PdfDocument doc, SizeF pageSize, PdfMargins margin) { PdfPageTemplateElement leftSpace = new PdfPageTemplateElement(margin.Left, pageSize.Height); doc.Template.Left = leftSpace; PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.Width, margin.Top); topSpace.Foreground = true; doc.Template.Top = topSpace; //draw header label PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic)); PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right); String label = "Demo about Header Repeating"; //set the header style SizeF size = font.MeasureString(label, format); float y = topSpace.Height - font.Height - 40; PdfPen pen = new PdfPen(Color.Black, 0.75f); topSpace.Graphics.SetTransparency(0.5f); topSpace.Graphics.DrawLine(pen, margin.Left - 30, y, pageSize.Width - margin.Right + 30, y); y = y - 1 - size.Height; topSpace.Graphics.DrawString(label, font, PdfBrushes.Black, pageSize.Width - margin.Right, y, format); PdfPageTemplateElement rightSpace = new PdfPageTemplateElement(margin.Right, pageSize.Height); doc.Template.Right = rightSpace; PdfPageTemplateElement bottomSpace = new PdfPageTemplateElement(pageSize.Width, margin.Bottom); bottomSpace.Foreground = true; doc.Template.Bottom = bottomSpace; }
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn