C#之展示Word、Excel、PDF并添加水印

       

目录

附件说明

Excel处理

PDF处理


        C#文章第三篇之附件展示以及添加水印,主要涉及Word、Excel、PDF。主要环境以及控件:

  • 基于Vistual Studio 2017
  • .net framework 4.0
  • DevExpress v18.1
  • Aspose.Cells 8.3.2.1
  • Aspose.Words 15.1.0.0
  • itextsharp 5.5.4.0
  • O2S.Components.PDFView4NET 4.7.4.0

附件说明

        附件展示分为Word、Excel以及PDF,在上面的控件中DevExpress可以加载Word、Excel和PDF,PDFView可以展示PDF文件。

        DevExpress是全球知名的控件开发公司里面集成了winform,web,wpf等相关的控件,界面美观,在整个项目中也使用了很多相关的控件如日期输入框,界面布局,DataGrid等,但是在加载PDF过程发现展示的速度较慢偶尔引起假死,因此这里引入了另外一个控件O2S.Components.PDFView4NET。

        PDFView4NET是一个面向.NET中增加PDF补偿和打印支持的工具包。它包括了一个针对Windows窗体的浏览控件以及相关的打印函数库。

        同时对于Word文件通过Aspose.Words转换成PDF进行处理。

    //加载文件   
    Document document = new Document(file);
    document.Save(this.exePath + fileName + "_tem.pdf", Aspose.Words.SaveFormat.Pdf);
    text = this.exePath + fileName + "_tem.pdf";

        因此,展示部分只需要考虑Excel和PDF两种形式。

这里Excel没有考虑转换的原因主要有两点,1是有多个sheet,2是如果Excel过宽转换成PDF会换页。

Excel处理

Excel的水印利用Aspose.Cells处理,添加艺术字来实现水印效果。可以实现,例如:拉伸标题,修饰文本,并使文本符合预设形状,或者是为图表添加水印效果等。同时需要考虑到excel下拉的情况:

Workbook workbook = new Workbook(file);
int i = 0;
int count = workbook.Worksheets.Count;
//处理多个sheet
while (i < count)
{
    Aspose.Cells.Worksheet worksheet = workbook.Worksheets[i];
    Int32 row = worksheet.Cells.MaxDataRow + 100;
    Int32 col = worksheet.Cells.MaxDataColumn + 20;
    //计算水印的位置,确保有文字的地方都会被水印覆盖
    int j = row / 25;
    if (j <= 0) j = 1;
    int m = col / 6;
    if (m <= 0) m = 1;
    for (int s = 0; s < j; s++)
    {
        for (int l = 0; l < m; l++)
        {
            //艺术字水印
            Aspose.Cells.Drawing.Shape shape = worksheet.Shapes.AddTextEffect(MsoPresetTextEffect.TextEffect1, strWarterMark, "Arial Black", 22, false, true, 8 + 25 * (s), 0, 1 + 6 * l, 1, 30, 200);
            MsoFillFormat fillFormat = shape.FillFormat;
            fillFormat.ForeColor = Color.FromArgb(100, 100, 100);
            fillFormat.Transparency = 0.5;
            shape.LineFormat.IsVisible = false;
            shape.RotationAngle = 330;
        }
    }
    i++;
}

代码主要实现了填充方式、颜色、透明度以及选装角度。效果如下:

C#之展示Word、Excel、PDF并添加水印_第1张图片

 最后应用Devexpress控件进行展示:

//展示控件
this.spreadsheetControl1.Visible = true;

IWorkbook document2 = this.spreadsheetControl1.Document;
document2.LoadDocument(this.exePath + lastFileName + ".xlsx");

PDF处理

相对Excel,PDF的处理则简单很多,itextsharp的一行代码则可以解决问题:

ItextShape.setWatermark(text, this.exePath + fileName + ".pdf", strWarterMark, "");

同样在展示上也更容易:

this.pdfPageView1.Visible = true;
this.pdfDocument1.Load(this.exePath + lastFileName + ".pdf");

效果图:

C#之展示Word、Excel、PDF并添加水印_第2张图片

 完整代码

String lastFileName = "";
private void process(object obj)
{
    try
    {
        IList fileNames = obj as List;

        //处理文件
        DataRowView user = this.comboBox1.SelectedItem as DataRowView;
        String strWarterMark = "苜蓿花乐园 QQ:562058537";
        int f = 1;
        foreach (String file in fileNames)
        {
            String text = file;
            string fileName = Path.GetFileName(file);
            string extension = Path.GetExtension(file);
            bool flag4 = extension == ".doc" || extension == ".docx" || extension == ".pdf";
            if (flag4)
            {
                if (extension != ".pdf")
                {
                    Document document = new Document(file);
                    document.Save(this.exePath + fileName + "_tem.pdf", Aspose.Words.SaveFormat.Pdf);
                    text = this.exePath + fileName + "_tem.pdf";
                }
                //添加水印
                Console.WriteLine(this.exePath + fileName + ".pdf");
                ItextShape.setWatermark(text, this.exePath + fileName + ".pdf", strWarterMark, "");
            }
            else
            {
                //excel处理
                bool flag6 = extension == ".xls" || extension == ".xlsx";
                if (flag6)
                {
                    Workbook workbook = new Workbook(file);
                    int i = 0;
                    int count = workbook.Worksheets.Count;
                    while (i < count)
                    {
                        Aspose.Cells.Worksheet worksheet = workbook.Worksheets[i];
                        Int32 row = worksheet.Cells.MaxDataRow + 100;
                        Int32 col = worksheet.Cells.MaxDataColumn + 20;
                        int j = row / 25;
                        if (j <= 0) j = 1;
                        int m = col / 6;
                        if (m <= 0) m = 1;
                        for (int s = 0; s < j; s++)
                        {
                            for (int l = 0; l < m; l++)
                            {
                                Aspose.Cells.Drawing.Shape shape = worksheet.Shapes.AddTextEffect(MsoPresetTextEffect.TextEffect1, strWarterMark, "Arial Black", 22, false, true,
                                8 + 25 * (s), 0, 1 + 6 * l, 1, 30, 200);
                                MsoFillFormat fillFormat = shape.FillFormat;
                                fillFormat.ForeColor = Color.FromArgb(100, 100, 100);
                                fillFormat.Transparency = 0.5;
                                shape.LineFormat.IsVisible = false;
                                shape.RotationAngle = 330;
                            }
                        }
                        i++;
                    }
                    workbook.Save(this.exePath + fileName + ".xlsx", Aspose.Cells.SaveFormat.Xlsx);
                }
            }
            p.SetValue(f * 100);
            f++;
        }

        //加载最后一个文件
        string ext = Path.GetExtension(lastFileName);
        if (ext == ".doc" || ext == ".docx" || ext == ".pdf")
        {
            this.spreadsheetControl1.Visible = false;
            this.pdfPageView1.Visible = true;
            this.pdfDocument1.Load(this.exePath + lastFileName + ".pdf");
        }
        else
        {
            this.spreadsheetControl1.Visible = true;
            this.pdfPageView1.Visible = false;

            IWorkbook document2 = this.spreadsheetControl1.Document;
            document2.LoadDocument(this.exePath + lastFileName + ".xlsx");
        }
        p.Close();
        p = null;
    }
    catch (Exception e)
    {
        p.Close();
        p = null;
        MessageBoxEx mes = new MessageBoxEx("处理文件出错,请重新尝试!");
        mes.ShowDialog();
    }
}

你可能感兴趣的:(学习笔记,知识分享,C#,c#,pdf,开发语言,visual,studio)