Reputation: 269
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();
}
Upvotes: 0
Views: 14648
Reputation: 1
link for pdfsharp dll 'import this
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
Upvotes: 0
Reputation: 55467
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)
.
Upvotes: 3