【问题标题】:itextsharp modify existing pdf (no new source pdf) and add watermarkitextsharp修改现有pdf(无新源pdf)并添加水印
【发布时间】:2013-08-15 13:28:47
【问题描述】:

我想修改现有的 pdf 文档并添加水印图像。如何在不创建新文件的情况下执行此操作?

我认为创建临时 pdf 是一个愚蠢的解决方案。删除源文件并像源文件一样重命名临时pdf!?

这是我的示例代码,但我正在创建一个新的目标文件。

问候

        private static void PdfApplication(String filePath) {

        PdfReader pdfReader = new PdfReader(filePath);
        Stream outputStream = new FileStream(newFilePath, FileMode.Open, FileAccess.Write, FileShare.None);

        PdfStamper pdfStamper = new PdfStamper(pdfReader, outputStream,'1', true);

        for (int pageIndex = 1; pageIndex <= pdfReader.NumberOfPages; pageIndex++) {
            pdfStamper.FormFlattening = false;
            iTextSharp.text.Rectangle pageRectangle = pdfReader.GetPageSizeWithRotation(pageIndex);
            PdfContentByte pdfData = pdfStamper.GetOverContent(pageIndex);
            pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 10);
            PdfGState graphicsState = new PdfGState();
            graphicsState.FillOpacity = 0.4F;
            pdfData.SetGState(graphicsState);
            pdfData.BeginText();

            FileStream fileStreamImage = new FileStream(watermark.jpg", FileMode.Open);
            iTextSharp.text.Image jpeg = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromStream(fileStreamImage), ImageFormat.Jpeg);

            float width = pageRectangle.Width;
            float height = pageRectangle.Height;
            jpeg.ScaleToFit(width, height);
            jpeg.SetAbsolutePosition(width / 2 - jpeg.Width / 2, height / 2 - jpeg.Height / 2);
            jpeg.SetAbsolutePosition(50, 50);
            jpeg.Rotation = 250;

            pdfData.AddImage(jpeg);
            pdfData.EndText();
        }
        pdfStamper.Close();
        outputStream.Close();
        outputStream.Dispose();

    }

【问题讨论】:

    标签: c# pdf itextsharp


    【解决方案1】:

    iTextSharp 不适用于就地编辑文件。如果在编写您的更改时出现异常怎么办?在此过程中,您会丢失旧文件和新文件。即使 iTextSharp 100% 没有错误,用户代码仍然可以破坏它。然后是边缘情况,例如通过添加一堆图像将 1MB 文件增加到 10GB 并用完驱动器上的空间。 iTextSharp 可靠地测试这些情况的唯一方法是实际编写文件。

    还有测试。每当我编辑文件时,我总是想将输入文件与输出文件进行比较。如果 iTextSharp 不断擦除我的输入文件,我将不得不不断地从另一个位置复制它,而我可能需要每小时执行数十次。

    所以这些是一些原因。但是有一种方法可以做你想做的事。 PdfReader 的构造函数之一是字节数组,只需将 System.IO.File.ReadAllBytes(filePath) 传递给它。由于这些字节不再绑定到磁盘,您现在可以写入它。

    第二个选项是写入MemoryStream,而不是在其上调用.ToArray(),然后在关闭PdfReader 后调用System.IO.File.WriteAllBytes(filePath, bytes)

    【讨论】:

    • OP 似乎忘记了即使是 Word 在您编辑时也会使用临时文件。 Word 是否也在使用愚蠢的解决方案?我请求与 OP 有所不同。
    • 好的谢谢你的提示。我必须重新考虑我寻求的解决方案,因为你的例子是正确的,不创建 teamp 文件不是一个好方法。
    【解决方案2】:

    link for pdfsharp dll '导入这个

    Imports System.IO
    Imports PdfSharp.Pdf
    Imports PdfSharp.Pdf.IO
    Imports PdfSharp.Drawing
    
    Dim doc = ReturnCompatiblePdf(path_of_pdf_file)
    Dim document As New PdfDocument
    document = PdfReader.Open(doc, PdfDocumentOpenMode.Modify)
    Dim watermark As String = "This is my watermark"
    
    For Each page_ As PdfPage In document.Pages
    
        Dim gfx As XGraphics = XGraphics.FromPdfPage(page_, XGraphicsPdfPageOptions.Append)
        Dim fontx As New XFont("Trebuchet MS", 8, FontStyle.Bold)
        Dim posx, posy As Double
        posx = (page_.Width.Value - watermark.Length) / 2
        posy = page_.Height.Value - 8
        gfx.TranslateTransform(posx, posy)
        gfx.DrawString(watermark, fontx, XBrushes.Black, New XPoint(1, 1), XStringFormats.Default)
    Next
    
    If File.Exists(save_path) = False Then
        document.Save(save_path)
    End If
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-02
      • 1970-01-01
      • 2019-01-04
      • 2018-07-12
      • 2015-05-19
      • 2011-08-27
      相关资源
      最近更新 更多