【问题标题】:How to add watermark to aspose pdf and word that contain table如何为包含表格的pdf和word添加水印
【发布时间】:2018-02-10 13:12:14
【问题描述】:

我生成了一个Aspose.Generate.Pdf 文件,然后通过以下方法添加一个Aspose.Pdf.Generator.Table。但是当我向这个 pdf 文件添加水印时,它会被创建的表覆盖:

   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;
    }

AddPdfWatermerk 方法是:

   private static Pdf AddPdfWatermark(Aspose.Pdf.Generator.Pdf pdfConv)
    {
        try
        {
            // Create FloatingBox with x as width and y as height
            Aspose.Pdf.Generator.FloatingBox background = new Aspose.Pdf.Generator.FloatingBox(); // width, height
            Aspose.Pdf.Generator.Image backImage = new Aspose.Pdf.Generator.Image();

            string path = HttpContext.Current.Server.MapPath("~/images/PrivateExcelBackGround.png");
            byte[] bgBuffer = File.ReadAllBytes(path);
            MemoryStream streamBack = new MemoryStream(bgBuffer, false);

            backImage.ImageInfo.ImageStream = streamBack;
            background.Paragraphs.Add(backImage);
            background.ZIndex = 1000;
            pdfConv.Watermarks.Add(background);

            pdfConv.IsWatermarkOnTop = false;
            return pdfConv;
        }
        catch
        {
            return pdfConv;
        }
    }

我尝试使用邮票而不是水印,但表格也掩盖了它。 在 aspose.words.document 文件中我也有一个问题,在带有提到表的 word 文件中,水印添加正确,但是当彩色交替表行时,水印被彩色行覆盖。

【问题讨论】:

    标签: aspose.words aspose.pdf


    【解决方案1】:

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

    // 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 合作,担任开发人员宣传员。

    【讨论】:

    • 此解决方案不适用于 aspose 9.3,我无法升级它,因为它需要对我的代码进行大量更改(aspose 18.1 不知道 Aspose.Pdf.Generator.Pdf , ...)
    • Aspose.Pdf.Generator 方法已经过时和停止,并且引入了 DOM(文档对象模型),它在性能和功能方面得到了增强和改进。请升级到最新版本,以解决您当前面临的水印问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多