Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

c# - itextsharp modify existing pdf (no new source pdf) and add watermark

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

    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...