C# word生成pdf报告
AxAcroPDFLib.AxAcroPDF axAcroPDF1; axAcroPDF1.LoadFile(filePath); axAcroPDF1.Visible = !string.IsNullOrEmpty(filePath); this.setZoom(null); private void setZoom(object sender) { if (sender != null) { ToolStripDropDownItem tsmi = sender as ToolStripDropDownItem; tsbZoom.Text = tsmi.Text; } string val = tsbZoom.Text == "页宽" ? "100" : tsbZoom.Text.Trim('%', '%', '%').Trim(); this.axAcroPDF1.setZoom(val.ToFloat()); }
安装Adobe Reader XI_11.0.0.379
1.在工具箱中添加Adobe提供的ActiveX控件;TOOLBOX---->COM组件
2.在工具箱最下面就会有一个Adobe PDF Reader控件出现, 拖一个Adobe PDF Reader控件到窗体上,双击窗体,在窗体加载时,弹出对话框,加载PDF文件:
3、用另一个窗体打开需用到LoadFile来加载PDF显示内容;
支持.doc和.docx;.xls和.xlsx
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(FilePath); workbook.Save(pdfFile, Aspose.Cells.SaveFormat.Pdf);
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Windows.Forms; using Aspose.Words; using iTextSharp.text.pdf; namespace ZrReportCommon { public class ReportTool { public static string TempFileName = "Temp"; /// <summary> /// 返回新的word文件路径名 /// </summary> /// <returns></returns> public static string GetNewFilePath(string FileName) { createTempFolder(); string result = Path.Combine(Application.StartupPath + @"\" + TempFileName, FileName); return result; } /// <summary> /// 返回新的图片文件路径名 /// </summary> /// <returns></returns> public static string GetNewImageFilePath() { createTempFolder(); string result = Path.Combine(Application.StartupPath + @"\" + TempFileName, Guid.NewGuid().ToString() + ".png"); return result; } /// <summary> /// 返回新的word文件路径名 /// </summary> /// <returns></returns> public static string GetNewWordFilePath() { createTempFolder(); string result = Path.Combine(Application.StartupPath + @"\" + TempFileName, Guid.NewGuid().ToString() + ".doc"); return result; } /// <summary> /// 返回新的word文件路径名 /// </summary> /// <returns></returns> public static string GetNewExcelFilePath() { createTempFolder(); string result = Path.Combine(Application.StartupPath + @"\" + TempFileName, Guid.NewGuid().ToString() + ".xls"); return result; } /// <summary> /// 返回新的pdf文件路径名 /// </summary> /// <returns></returns> public static string GetNewPdfFilePath() { createTempFolder(); string result = Path.Combine(Application.StartupPath + @"\" + TempFileName, Guid.NewGuid().ToString() + ".pdf"); return result; } /// <summary> /// 合并word文件 /// </summary> /// <param name="wordInputs"></param> /// <returns></returns> public static string MergeWordFiles(List<string> wordInputs) { if (wordInputs.Count == 1) { return wordInputs[0]; } string filePath = GetNewWordFilePath(); Document doc = new Document(); doc.RemoveAllChildren(); for (int i = 1; i <= wordInputs.Count; i++) { Document srcDoc = new Document(wordInputs[i - 1]); doc.AppendDocument(srcDoc, ImportFormatMode.UseDestinationStyles); if (i > 1) doc.Sections[i - 1].HeadersFooters.LinkToPrevious(false); } doc.Save(filePath); return filePath; } /// <summary> /// 合并PDF文件 /// </summary> /// <param name="ListFile"></param> /// <returns></returns> public static string MergePdfFiles(List<string> ListFile) { if (ListFile.Count == 1) return ListFile[0]; List<byte[]> pdfInputs = new List<byte[]>(); foreach (var file in ListFile) { FileInfo fi = new FileInfo(file); FileStream fs = fi.OpenRead(); byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, Convert.ToInt32(fs.Length)); pdfInputs.Add(bytes); fs.Close(); } var mergebytes = MergePdfFiles(pdfInputs); string tempPath = GetNewPdfFilePath(); File.WriteAllBytes(tempPath, mergebytes); return tempPath; } private static byte[] MergePdfFiles(List<byte[]> pdfInputs) { using (MemoryStream stream = new MemoryStream()) { iTextSharp.text.Document document = new iTextSharp.text.Document(); iTextSharp.text.Rectangle re; PdfWriter writer = PdfWriter.GetInstance(document, stream); document.Open(); PdfContentByte cb = writer.DirectContent; PdfImportedPage newPage; PdfReader reader; foreach (var p in pdfInputs) { reader = new PdfReader(p); int iPageNum = reader.NumberOfPages; for (int i = 1; i <= iPageNum; i++) { re = reader.GetPageSize(reader.GetPageN(iPageNum)); document.SetPageSize(re); document.NewPage(); newPage = writer.GetImportedPage(reader, i); cb.AddTemplate(newPage, 0, 0); } } document.Close(); writer.Close(); return stream.ToArray(); } } /// <summary> /// word文件转pdf /// </summary> /// <param name="WordFilePath"></param> /// <returns></returns> public static string WordToPdf(string WordFilePath) { if (!string.IsNullOrEmpty(WordFilePath)) { string pdf = Path.Combine(Path.GetDirectoryName(WordFilePath), Path.GetFileNameWithoutExtension(WordFilePath) + ".pdf"); Document doc = new Document(WordFilePath); doc.Save(pdf, SaveFormat.Pdf); return pdf; } else return ""; } /// <summary> /// word文件转pdf /// </summary> /// <param name="WordFilePath"></param> /// <returns></returns> public static string ExcelToPdf(string ExcelFilePath) { if (!string.IsNullOrEmpty(ExcelFilePath)) { string pdf = Path.Combine(Path.GetDirectoryName(ExcelFilePath), Path.GetFileNameWithoutExtension(ExcelFilePath) + ".pdf"); Document doc = new Document(ExcelFilePath); doc.Save(pdf, SaveFormat.Pdf); return pdf; } else return ""; } /// <summary> /// 创建临时文件加 /// </summary> private static void createTempFolder() { if (!Directory.Exists(Application.StartupPath + @"\" + TempFileName)) { Directory.CreateDirectory(Application.StartupPath + @"\" + TempFileName); } } } }