Aspose.Words操作word生成PDF文档

Posted 无恨星晨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Aspose.Words操作word生成PDF文档相关的知识,希望对你有一定的参考价值。

Aspose.Words操作word生成PDF文档

using Aspose.Words;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TempAspose.Words.Function
{
    public class WordToPdf
    {
        private string _TempPath;   //模版路径

        private Aspose.Words.Document doc = null;

        public WordToPdf(string TempPath)
        {
            this._TempPath = TempPath;
            doc = new Aspose.Words.Document(TempPath);  //new一个对象
        }

        /// <summary>
        /// 在书签处插入值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool InsertValue(string key, string value)
        {
            Bookmark bookmark=doc.Range.Bookmarks[key];
            //判断是否存在此标签
            if (bookmark != null)
            {
                bookmark.Text = value;
                return true;
            }
            else {
                return false;
            }
        }

        /// <summary>
        /// 在书签处插入图片
        /// </summary>
        /// <param name="key">书签</param>
        /// <param name="img">图片</param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public bool InsertImage(string key, string img,double width,double height)
        {
            DocumentBuilder dBuilder = new DocumentBuilder(doc);
            bool pic = dBuilder.MoveToBookmark(key);
            //判断书签是否存在
            if (pic)
            {
                dBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                dBuilder.InsertImage(img, width, height);
                return true;
            }
            else {
                return false;
            }           
        }

        #region 插入表格,未测试
        public bool InsertDataSet()
        {
            try
            {
                System.Data.DataSet dataSet = new System.Data.DataSet();
                doc.MailMerge.ExecuteWithRegions(dataSet);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public bool InsertTable(string key,System.Data.DataTable dt)
        {
            try
            {
                DocumentBuilder dBuilder = new DocumentBuilder(doc);
                bool table = dBuilder.MoveToBookmark(key);
                dBuilder.StartTable();//开始画Table  
                dBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                //添加Word表格  
                dBuilder.InsertCell();
                dBuilder.CellFormat.Width = 600.0;
                dBuilder.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPoints(115);
                dBuilder.CellFormat.Borders.LineStyle = LineStyle.None;

                dBuilder.StartTable();//开始画Table  
                dBuilder.RowFormat.Height = 20.2;
                dBuilder.InsertCell();
                dBuilder.CellFormat.Borders.LineStyle = LineStyle.Single;
                dBuilder.Font.Size = 10.5;
                dBuilder.Bold = false;
                dBuilder.Write("评卷人");

                dBuilder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐  
                dBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                dBuilder.CellFormat.Width = 50.0;
                dBuilder.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPoints(50);
                dBuilder.RowFormat.Height = 20.0;
                dBuilder.InsertCell();
                dBuilder.CellFormat.Borders.LineStyle = LineStyle.Single;
                dBuilder.Font.Size = 10.5;
                dBuilder.Bold = false;
                dBuilder.Write("得分");
                dBuilder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐  
                dBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                dBuilder.CellFormat.Width = 50.0;
                dBuilder.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.FromPoints(50);
                dBuilder.EndRow();
                dBuilder.RowFormat.Height = 25.0;
                dBuilder.InsertCell();
                dBuilder.RowFormat.Height = 25.0;
                dBuilder.InsertCell();
                dBuilder.EndRow();
                dBuilder.EndTable();

                dBuilder.InsertCell();
                dBuilder.CellFormat.Width = 300.0;
                dBuilder.CellFormat.PreferredWidth = Aspose.Words.Tables.PreferredWidth.Auto;
                dBuilder.CellFormat.Borders.LineStyle = LineStyle.None;


                //dBuilder.CellFormat.VerticalAlignment = Aspose.Words.Tables.CellVerticalAlignment.Center;//垂直居中对齐  
                //dBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
                //dBuilder.Font.Size = 11;
                //dBuilder.Bold = true;
                //dBuilder.Write(handText);
                //dBuilder.EndRow();
                //dBuilder.RowFormat.Height = 28;
                dBuilder.EndTable();  
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }    
        }
        #endregion

        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="foemat">文件格式,枚举</param>
        /// <returns></returns>
        public bool SaveFile(string path, SaveFormat foemat)
        {
            try
            {
                doc.Save(path, foemat);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
Aspose.Words操作word生成PDF文档

 

以上是关于Aspose.Words操作word生成PDF文档的主要内容,如果未能解决你的问题,请参考以下文章

如何解决利用aspose把word文档转换为pdf文档时出现乱码 C#

Word处理控件Aspose.Words功能演示:使用 Aspose.Words for C++ 在 Qt 应用程序中创建 Word 文档

Aspose.Words用Aspose.Words.Range.Replace替换文本导出pdf

如何破解aspose.words

Aspose.Words doc转pdf 内容出现丢失,页码跳页,排版混乱问题

Aspose.Words doc转pdf 内容出现丢失,页码跳页,排版混乱问题