Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# itextsharp修改现有pdf(无新源pdf)并添加水印_C#_Pdf_Itextsharp - Fatal编程技术网

C# itextsharp修改现有pdf(无新源pdf)并添加水印

C# itextsharp修改现有pdf(无新源pdf)并添加水印,c#,pdf,itextsharp,C#,Pdf,Itextsharp,我想修改现有的pdf文档并添加水印图像。如何在不创建新文件的情况下执行此操作 我认为创建临时pdf是一个愚蠢的解决方案。删除源文件并像源文件一样重命名临时pdf 这里是我的示例代码,但在那里我创建了一个新的目标文件 问候 private static void PdfApplication(String filePath) { PdfReader pdfReader = new PdfReader(filePath); Stream output

我想修改现有的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();

    }
私有静态无效PDFAApplication(字符串文件路径){
PdfReader PdfReader=新PdfReader(文件路径);
Stream outputStream=new FileStream(newFilePath,FileMode.Open,FileAccess.Write,FileShare.None);
PdfStamper PdfStamper=新的PdfStamper(pdfReader,outputStream,'1',true);

对于(int pageIndex=1;pageIndexiTextSharp不打算用于就地编辑文件。如果在编写更改时出现异常会怎么样?在这个过程中,您会丢失旧文件和新文件。即使iTextSharp是100%无错误的,用户代码仍可能破坏它。此外,还有一些边缘情况,如将1MB文件扩展到通过添加一堆图像并在驱动器上耗尽空间来实现10GB。iTextSharp可靠测试这些情况的唯一方法是实际编写一个文件

还有测试。每当我编辑一个文件时,我总是想比较我的输入文件和输出文件。如果iTextSharp一直删除我的输入文件,我就必须不断地从另一个位置复制它,我可能一小时要做几十次

这就是其中的一些原因。但有一种方法可以完成您想要做的事情。
PdfReader
的构造函数之一是字节数组,只需将
System.IO.File.ReadAllBytes(filePath)
传递给它即可。因为这些字节不再绑定到磁盘上,所以您现在可以对其进行写入


第二个选项是写入
内存流
,在其上调用
.ToArray()
,然后在关闭
PdfReader
后调用
System.IO.File.WriteAllBytes(文件路径,字节)

iTextSharp不用于就地编辑文件。如果在编写更改时出现异常会怎么样?在这个过程中,您会丢失旧文件和新文件。即使iTextSharp 100%没有bug,用户代码仍可能破坏它。此外,还有一些边缘情况,例如通过添加iTextSharp可靠测试这些情况的唯一方法是实际编写一个文件

还有测试。每当我编辑一个文件时,我总是想比较我的输入文件和输出文件。如果iTextSharp一直删除我的输入文件,我就必须不断地从另一个位置复制它,我可能一小时要做几十次

这就是其中的一些原因。但有一种方法可以完成您想要做的事情。
PdfReader
的构造函数之一是字节数组,只需将
System.IO.File.ReadAllBytes(filePath)
传递给它即可。因为这些字节不再绑定到磁盘上,所以您现在可以对其进行写入

第二个选项是写入
内存流
,在其上调用
.ToArray()
,然后在关闭
PdfReader后调用
System.IO.File.writealBytes(文件路径,字节)
"进口这个,

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
"进口这个,

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

OP似乎忘记了即使是Word在编辑时也会使用临时文件。Word是否也使用了愚蠢的解决方案?我不同意OP.ok thx,以获取您的提示。我必须重新思考我寻求的解决方案,因为您的示例是对的,不创建teamp文件不是一个好方法。OP似乎忘记了即使是Word也会使用临时文件n你编辑它。Word也使用了愚蠢的解决方案吗?我不同意OP.ok thx的建议。我必须重新思考我寻求的解决方案,因为你的示例是正确的,不创建teamp文件不是一个好方法。