0

我生成一个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 文件中,水印添加正确,但是当彩色交替表行时,水印被彩色行覆盖。

4

1 回答 1

1

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

// 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 类提供了创建基于图像的图章所需的属性,例如高度、宽度、不透明度等。您可以访问在 PDF 文件中添加图章以获取有关此主题的更多信息。如果您发现使用此代码生成的文件有任何问题,请向我们提供源代码和生成的文件,以便我们继续为您提供帮助。

我与 Aspose 一起担任开发人员宣传员。

于 2018-02-10T22:57:50.253 回答