iTextSharp的修改现有的PDF(没有新源PDF)和添加水印 [英] itextsharp modify existing pdf (no new source pdf) and add watermark

查看:1064
本文介绍了iTextSharp的修改现有的PDF(没有新源PDF)和添加水印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改现有的PDF文档,并添加水印的图像。我怎样才能做到这一点没有创建一个新的文件?

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

下面我举的例子code,但有我创建一个新的目标文件。

问候

 私有静态无效PdfApplication(字符串文件路径){        PdfReader pdfReader =新PdfReader(文件路径);
        流的OutputStream =新的FileStream(newFilePath,FileMode.Open,FileAccess.Write,FileShare.None);        PdfStamper pdfStamper =新PdfStamper(pdfReader,OutputStream中,'1',真);        对于(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 =新PdfGState();
            graphicsState.FillOpacity = 0.4F;
            pdfData.SetGState(graphicsState);
            pdfData.BeginText();            的FileStream fileStreamImage =新的FileStream(watermark.jpg,FileMode.Open);
            iTextSharp.text.Image JPEG = iTextSharp.text.Image.GetInstance(System.Drawing.Image.FromStream(fileStreamImage),ImageFormat.Jpeg);            浮宽度= pageRectangle.Width;
            浮动高度= pageRectangle.Height;
            jpeg.ScaleToFit(宽度,高度);
            jpeg.SetAbsolutePosition(宽度/ 2 - jpeg.Width / 2,高度/ 2 - jpeg.Height / 2);
            jpeg.SetAbsolutePosition(50,50);
            jpeg.Rotation = 250;            pdfData.AddImage(JPEG);
            pdfData.EndText();
        }
        pdfStamper.Close();
        outputStream.Close();
        outputStream.Dispose();    }


解决方案

iTextSharp的不打算使用就地编辑文件。如果同时在写你的变化有一个例外?你会失去在这个过程中您的新旧和文件。即使iTextSharp的100%无bug,用户可code仍然可以打破它。再有就是边缘情况下,像在那里你通过添加一些图片,长出了1MB的文件,10GB和驱动器上运行的空间。对于iTextSharp的唯一的方法可靠地测试这些情况是实际写入文件。

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

因此​​,这些都是一些原因的的。但是,有一种方法做你想做的事情。其中一个构造函数 PdfReader 是一个字节数组,只是通过 System.IO.File.ReadAllBytes(文件路径)来它。由于这些字节是不依赖于磁盘了你现在可以写了。

第二个选项是写入的MemoryStream 来代替,调用 .ToArray()上,然后关闭之后在 PdfReader 通话 System.IO.File.WriteAllBytes(文件路径,字节)

I would like to modify a existing pdf document and add a watermark image. How can I do this without to create a new file?

I think it's a stupid solution to create a temp pdf. Delete the source file and rename the temp pdf like the source file!?

Here my example code but there I'm creating a new destination file.

Regards

        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();

    }

解决方案

iTextSharp isn't intended to be used to edit files in-place. What if while it was writing your changes there was an exception? You'd lose both your old and new file in the process. Even if iTextSharp was 100% bug free, user-code could still break it. And then there's the edge cases like where you grow a 1MB file to 10GB by adding a bunch of images and run out of space on a drive. The only way for iTextSharp to reliably test these cases is to actually write a file.

There's also testing. Whenever I'm editing a file I always want to compare my input file to my output file. If iTextSharp kept erasing my input file I'd constantly have to copy it from another location which I might have to do dozens of times an hour.

So those are some of the why's. But there is a way to do what you want to do. One of the constructors for PdfReader is a byte array, just pass System.IO.File.ReadAllBytes(filePath) to it. Since those bytes aren't tied to the disk anymore you can now write to it.

A second option is write to a MemoryStream instead, call .ToArray() on it and then after closing the PdfReader call System.IO.File.WriteAllBytes(filePath, bytes).

这篇关于iTextSharp的修改现有的PDF(没有新源PDF)和添加水印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆