如何添加水印aspose pdf和包含表格的单词

时间:2018-02-10 13:12:15

标签: aspose.words aspose.pdf

我生成一个 this.state.pan = new Animated.ValueXY({x: 50, y:50}) Animated.spring( this.state.pan, { toValue: { x: 0, y: 0 } } ).start(); 文件,然后通过以下方法向其中添加Aspose.Generate.Pdf。但是当我为这个pdf文件添加水印时,它覆盖了创建的表:

Aspose.Pdf.Generator.Table

和AddPdfWatermerk方法是:

   public static BasketResult<string> ExportDataTableToPdf(DataTable inputDataTable, string CaptionFilename)
    {
        List<DataColumn> listDataColumns = GetDataColumns(inputDataTable, CaptionFilename);
        BasketResult<string> returnResult = new BasketResult<string>();
        String rtnPathFile = String.Empty;
        int rowCount = inputDataTable.Rows.Count;

        if (rowCount <= 1000)
        {
            try
            {
                string dataDir = Settings.TempPath;

                if (!Directory.Exists(dataDir))
                    Directory.CreateDirectory(dataDir);

                Pdf pdfConv = new Pdf();
                pdfConv.IsRightToLeft = true;
                Aspose.Pdf.Generator.Section mainSection = pdfConv.Sections.Add();
                mainSection.TextInfo.IsRightToLeft = true;
                mainSection.IsLandscape = true;
                mainSection.TextInfo.Alignment = AlignmentType.Right;

                // header definition begin
                Aspose.Pdf.Generator.HeaderFooter header = new Aspose.Pdf.Generator.HeaderFooter(mainSection);
                mainSection.EvenHeader = header;
                mainSection.OddHeader = header;
                header.Margin.Top = 50;
                Aspose.Pdf.Generator.Table headerTable = new Aspose.Pdf.Generator.Table();
                header.Paragraphs.Add(headerTable);
                headerTable.DefaultCellBorder = new BorderInfo((int)BorderSide.All, 0.1F);
                headerTable.Alignment = AlignmentType.Right;
                headerTable.DefaultColumnWidth = "80";
                headerTable.FixedHeight = 30;
                Aspose.Pdf.Generator.Row headerRow = headerTable.Rows.Add();
                headerRow.BackgroundColor = new Aspose.Pdf.Generator.Color("#D3DFEE");

                int index = 0;
                listDataColumns.Reverse();

                foreach (DataColumn column in listDataColumns)
                {
                    string cellText = column.Caption;
                    headerRow.Cells.Add(cellText);
                    headerRow.Cells[index].DefaultCellTextInfo.FontName = "Tahoma";
                    headerRow.Cells[index].DefaultCellTextInfo.IsRightToLeft = true;
                    headerRow.Cells[index].VerticalAlignment = VerticalAlignmentType.Center;
                    headerRow.Cells[index++].Alignment = AlignmentType.Center;
                }

                Document doc = new Document();
                DocumentBuilder builder = new DocumentBuilder(doc);
                Aspose.Words.Tables.Table wordTable = ImportTableFromDataTable(builder, inputDataTable,
                    CaptionFilename, true);

                string columnWidths = "";
                for (int j = 0; j < wordTable.FirstRow.Count; j++)
                {
                    columnWidths = columnWidths + "80 ";
                }

                Aspose.Pdf.Generator.Table table = new Aspose.Pdf.Generator.Table();
                mainSection.Paragraphs.Add(table);
                table.ColumnWidths = columnWidths;
                table.DefaultCellBorder = new BorderInfo((int)BorderSide.All, 0.1F);
                table.Alignment = AlignmentType.Right;

                //fill table
                for (int i = 1; i < wordTable.Rows.Count; i++)
                {
                    Aspose.Pdf.Generator.Row row = table.Rows.Add();
                    row.BackgroundColor = i % 2 == 0
                        ? new Aspose.Pdf.Generator.Color("#D3DFEE")
                        : new Aspose.Pdf.Generator.Color("#FFFFFF");
                    var wordTableRow = wordTable.Rows[i];
                    //fill columns from end to begin because table is left to right
                    for (int c = wordTable.FirstRow.Count - 1; c >= 0; c--)
                    {
                        var cellValue = wordTableRow.ChildNodes[c];
                        string cellText = cellValue.GetText();
                        row.Cells.Add(cellText);
                    }

                    //set style to every cell
                    for (int c = 0; c < wordTable.FirstRow.Count; c++)
                    {
                        row.Cells[c].DefaultCellTextInfo.FontName = "Tahoma";
                        row.Cells[c].DefaultCellTextInfo.IsRightToLeft = true;
                        row.Cells[c].VerticalAlignment = VerticalAlignmentType.Center;
                        row.Cells[c].Alignment = AlignmentType.Center;
                    }
                }

                pdfConv.SetUnicode();

                rtnPathFile = Helper.GetTempFile() + ".pdf";
                string fileName = Helper.GetFileNameFromFilePath(rtnPathFile);
                pdfConv = AddPdfWatermark(pdfConv);
                pdfConv.Save(dataDir + fileName);
                returnResult.Result.Add(rtnPathFile);
                returnResult.IsSuccess = true;

            }
            catch (Exception ex)
            {
                rtnPathFile = String.Empty;
                returnResult.IsSuccess = false;
                returnResult.Result.Add(rtnPathFile);
                returnResult.persianErrorMessages.Add(Messages.Err_InvalidFilePath);
            }
        }
        else
        {
            returnResult.IsSuccess = false;
            returnResult.Result.Add(rtnPathFile);
            returnResult.persianErrorMessages.Add(Messages.Err_CreateFile);
        }

        return returnResult;
    }

我尝试用水印而不是水印,但是桌子也用水印。 在aspose.words.document文件中我也有一个问题,在提到表的word文件中,水印正确添加但是当有彩色的备用表行时,水印被彩色行覆盖。

1 个答案:

答案 0 :(得分:1)

您使用的是Aspose.PDF API的过时版本。请升级到Aspose.PDF for .NET 18.1,其中包含更多功能和错误修复。您可以使用环境中的以下代码段添加图像标记。

// Open document
Document pdfDocument = new Document(dataDir+ "AddImageStamp.pdf");

// Create image stamp
ImageStamp imageStamp = new ImageStamp(dataDir + "aspose-logo.jpg");
imageStamp.Background = true;
imageStamp.XIndent = 100;
imageStamp.YIndent = 100;
imageStamp.Height = 300;
imageStamp.Width = 300;
imageStamp.Rotate = Rotation.on270;
imageStamp.Opacity = 0.5;

// Add stamp to particular page
pdfDocument.Pages[1].AddStamp(imageStamp);

dataDir = dataDir + "AddImageStamp_out.pdf";

// Save output document
pdfDocument.Save(dataDir);

ImageStamp类提供创建基于图像的图章所需的属性,例如高度,宽度,不透明度等。您可以访问Adding stamp in a PDF file以获取有关此主题的更多信息。如果您发现使用此代码生成的文件有任何问题,请使用源文件和生成的文件返回给我们,以便我们继续为您提供帮助。

我与Aspose一起担任开发者布道者。