/// <summary> /// Inserts a watermark into a document. /// </summary> /// <param name="doc">The input document.</param> /// <param name="watermarkText">Text of the watermark.</param> public static void InsertWatermarkText(Aspose.Words.Document doc, string watermarkText) { // Create a watermark shape. This will be a WordArt shape. // You are free to try other shape types as watermarks. Aspose.Words.Drawing.Shape watermark = new Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText); // Set up the text of the watermark. watermark.TextPath.Text = watermarkText; watermark.TextPath.FontFamily = "Arial"; watermark.Width = 500; watermark.Height = 100; // Text will be directed from the bottom-left to the top-right corner. watermark.Rotation = -40; // Remove the following two lines if you need a solid black text. watermark.Fill.Color = System.Drawing.Color.Gray; // Try LightGray to get more Word-style watermark watermark.StrokeColor = System.Drawing.Color.Gray; // Try LightGray to get more Word-style watermark // Place the watermark in the page center. watermark.RelativeHorizontalPosition = Aspose.Words.Drawing.RelativeHorizontalPosition.Page; watermark.RelativeVerticalPosition = Aspose.Words.Drawing.RelativeVerticalPosition.Page; watermark.WrapType = Aspose.Words.Drawing.WrapType.None; watermark.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center; watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center; // Create a new paragraph and append the watermark to this paragraph. Aspose.Words.Paragraph watermarkPara = new Aspose.Words.Paragraph(doc); watermarkPara.AppendChild(watermark); // Insert the watermark into all headers of each document section. foreach (Aspose.Words.Section sect in doc.Sections) { // There could be up to three different headers in each section, since we want // the watermark to appear on all pages, insert into all headers. InsertWatermarkIntoHeader(watermarkPara, sect, Aspose.Words.HeaderFooterType.HeaderPrimary); InsertWatermarkIntoHeader(watermarkPara, sect, Aspose.Words.HeaderFooterType.HeaderFirst); InsertWatermarkIntoHeader(watermarkPara, sect, Aspose.Words.HeaderFooterType.HeaderEven); } }
/// <summary> /// Inserts a watermark into a document header. /// </summary> /// <param name="watermarkPara"></param> /// <param name="sect"></param> /// <param name="headerType"></param> public static void InsertWatermarkIntoHeader(Aspose.Words.Paragraph watermarkPara, Aspose.Words.Section sect, Aspose.Words.HeaderFooterType headerType) { Aspose.Words.HeaderFooter header = sect.HeadersFooters[headerType]; if (null == header) { // There is no header of the specified type in the current section, create it. header = new Aspose.Words.HeaderFooter(sect.Document, headerType); sect.HeadersFooters.Add(header); } // Insert a clone of the watermark into the header. header.AppendChild(watermarkPara.Clone(true)); }
/// <summary> /// 转换到PDF文件 /// </summary> /// <param name="destDocFile"></param> private void convertToPDF(string projectNumber, string destDocFile) { try { //Document文档对象 Aspose.Words.Document xDoc = new Aspose.Words.Document(destDocFile); //获得附件表格 Aspose.Words.Tables.Table tablee = (Aspose.Words.Tables.Table)xDoc.GetChild(Aspose.Words.NodeType.Table, xDoc.GetChildNodes(Aspose.Words.NodeType.Table, true).Count - 1, true); //行与 int rowIndex = 0; //查找需要替换位置 if (tablee != null && tablee.Rows != null) { //表格有效 foreach (Aspose.Words.Tables.Row row in tablee.Rows) { if (row.Cells[0].GetText().Contains("序号")) { continue; } else { //行号+1 rowIndex++; //清理单位格内容 row.Cells[0].Paragraphs.Clear(); //写行号 Aspose.Words.Paragraph p = new Aspose.Words.Paragraph(xDoc); p.AppendChild(new Aspose.Words.Run(xDoc, rowIndex.ToString())); row.Cells[0].AppendChild(p); } } } else { //表格无效 MainForm.writeLog("没有找到附件清单,当前文件" + destDocFile + ",表格数量:" + xDoc.GetChildNodes(Aspose.Words.NodeType.Table, true).Count); } //保存为PDF xDoc.Save(Path.Combine(new FileInfo(destDocFile).DirectoryName, "项目申报书.pdf"), Aspose.Words.SaveFormat.Pdf); } catch (Exception ex) { MainForm.writeLog(ex.ToString()); //尝试取文件名称 string fileName = destDocFile; try { fileName = new FileInfo(destDocFile).Name; } catch (Exception exxx) { } //检查是不是出现了保存成PDF时内存溢出 addErrorFile(projectNumber, fileName, "PDF转换失败"); } //删除Doc文档 try { File.Delete(destDocFile); } catch (Exception ex) { MainForm.writeLog(ex.ToString()); } }