Free Support Forum - aspose.com

Possibility to edit and add watermarks/footers/headers to existing PDF files

Hi Aspose,

Our team has a need to edit existing pdf. ex. to add watermarks, footers, and headers, I looked at this PDF editor, https://products.aspose.app/pdf/editor#

Looks like there is no place I can edit headers, and footers, but for watermarks, I saw you have Aspose.PDF watermark,

Could you provide me some alternative solutions, or ideas, or you have already had the functionality that I didn’t know.

Thanks

@kellyzhai

You can add text/image watermark in a PDF using Aspose.PDF for .NET as well as find and replace text in PDF documents. However, if your requirements are not met using shared examples, please share a sample input and expected output PDF document for our reference so that we can further proceed to assist you accordingly.

Thanks for the solution. It works on your demo PDF document. Could you please test the solution using the attached PDF, which created by our side. Not sure there is a layer issue. it does not work whenever adding headers, footers, or watermark. Budgets-test.pdf (209.2 KB)

@kellyzhai

It looks like that you shared an output PDF document generated by Aspose.PDF. Could you please share the respective source PDF along with the sample code snippet that you used to generate it so that we can test the scenario in our environment accordingly and share our feedback with you.

PLease see attached PDF. The sample code is just your live demo https://products.aspose.app/pdf/watermark

ThanksTestSample.pdf (54.0 KB)

@kellyzhai

We used the below code snippet to add a text watermark in your PDF and did not face any issue. We used 21.5 version of the API. For your kind reference, we have attached generated output PDF as well:

using var doc = new Document(dataDir + "TestSample.pdf");
{
 foreach (var page in doc.Pages)
 {
  var bkStamp1 = new TextStamp("Hello1")
  {
   HorizontalAlignment = HorizontalAlignment.Center,
   VerticalAlignment = VerticalAlignment.Center,
   Width = 400,
   Height = 100,
   LeftMargin = 10,
   BottomMargin = 10
  };
  bkStamp1.Put(page);
  page.Dispose();
 }
 doc.Save(dataDir + "output.pdf");
}

output.pdf (55.5 KB)

Could you please try using the above code snippet and let us know in case you still face any issue.

we cannot use 21.5, we have to use 19 version as for a performance concern, can you test it on 19 version from your side. Thanks

@kellyzhai

Please check the attached PDF output that was generated using 19.10 version of the API at our end.

output.pdf (55.4 KB)

I tried above code, it gave me below error.

image.png (60.8 KB)

		Document pdfDocument = new Document(inFilePath);

@kellyzhai

We did not face this issue at our end. It seems like you are executing the code snippet in the same project that has been downloaded from GitHub. The error reason can possibly be related to some other routine of the application. Please try to create a sample console application and use the code snippet which we provided. In case you still face any issue, share that console application with us so that we can also replicate and address the issue.

Aspose.zip (35.7 KB)
No luck when created a brand new console application

image.png (118.7 KB)

Hello World!
Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=zhqdG1nHwOIixc4mIUOFpoNu0gNFFc_aLwQ==(MethodBase #=zlWj2Rcg=, Boolean #=zZzkXk34=)
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=zm$9eOMz1Fqu2P0K4PRFOgbVr2BWY4owYOQ==(#=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU= #=zlWj2Rcg=, #=qxqAie0E0yOckGKgIIT2_2aJjp_XSfFC0fdlQwUX933s= #=zZzkXk34=)
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=z3XJ$V2AfZazinB6qiWXk2mkCtJsfr7hDXg==()
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=zFGGbgMwyoLkFrqLwMYFun7FRKbB_bTbCrg==(Boolean #=zlWj2Rcg=)
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=ztCjNBATWbgzvqlVF3R0epbY=(Object #=zlWj2Rcg=)
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=zK2m931H9SqAT16DmZFQlPW5BrPtlHqQ6V2DDpJB$LSkq()
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=zgh3vitWDk9sJ5B1RMe2MFqSDA7LuSNG_Bg==(#=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU= #=zlWj2Rcg=, #=qxqAie0E0yOckGKgIIT2_2aJjp_XSfFC0fdlQwUX933s= #=zZzkXk34=)
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=z3XJ$V2AfZazinB6qiWXk2mkCtJsfr7hDXg==()
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=zFGGbgMwyoLkFrqLwMYFun7FRKbB_bTbCrg==(Boolean #=zlWj2Rcg=)
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=ztCjNBATWbgzvqlVF3R0epbY=(Object #=zlWj2Rcg=)
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=zK2m931H9SqAT16DmZFQlPW5BrPtlHqQ6V2DDpJB$LSkq()
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=z781qlal6irvZ1uPMq0Og1efTP76Pim85KQ==(Object #=zlWj2Rcg=, UInt32 #=zZzkXk34=)
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=zFGGbgMwyoLkFrqLwMYFun7FRKbB_bTbCrg==(Boolean #=zlWj2Rcg=)
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=zFGGbgMwyoLkFrqLwMYFun7FRKbB_bTbCrg==(Boolean #=zlWj2Rcg=)
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=zgO2uMYxekrvlCPRFIlrsp4H5Ay7kTiWzxA==()
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=zfaTYzQgUIc41ws9BJMNaeiLli2nwVP$D99g70L4=(Object[] #=zlWj2Rcg=, Type[] #=zZzkXk34=, Type[] #=zMNu93VY=, Object[] #=zCuBRQ3Y=)
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=z1out3DUOMyo6WU1LpefSfuTkOoidBHW_zQrIVg9yhojH(Stream #=zlWj2Rcg=, String #=zZzkXk34=, Object[] #=zMNu93VY=, Type[] #=zCuBRQ3Y=, Type[] #=zOxo$TiY=, Object[] #=z1ouOa14=)
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=zy8J3NpRPckSn2Zq2vOiPCefWLWN1(Stream #=zlWj2Rcg=, String #=zZzkXk34=, Object[] #=zMNu93VY=)
at #=qhK3OMz7WAHs8WONLEdKogPEgLogyJx1iiWL4JUrafpU=.#=zJRJUI9nliJl0FVeCB7A7K3a9$$7Z(Stream #=zlWj2Rcg=, String #=zZzkXk34=, Object[] #=zMNu93VY=)
at Aspose.Pdf.Document.#=zknqjwvMm3koH(Stream #=zhauhRpM=, SaveOptions #=z2Tk5FOg$aPIm)
at Aspose.Pdf.Document.#=z4laxV7ngq0$_(String #=zNo3Cfw2oI6lO)
at Aspose.Pdf.Document.Save(String outputFileName)
at Aspose.AddTextStamp.AddHeaders() in C:\Users\u6078022\source\repos\Aspose\Aspose\AddTextStamp.cs:line 30
at Aspose.Program.Main(String[] args) in C:\Users\u6078022\source\repos\Aspose\Aspose\Program.cs:line 10

C:\Users\u6078022\source\repos\Aspose\Aspose\bin\Debug\net5.0\Aspose.exe (process 26596) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

@kellyzhai

It seems like the issue is related to “page.Dispose();” under the Framework Version which you are using as we tested the scenario at our end using the same code under 4.7 .NET Framework and did not notice any issue. Furthermore, it also looks like that there is some issue in the 19.3 version of the API. We tested using the 19.10 version and the text stamp was added successfully. Please use the 19.10 version at your end and let us know in case you notice any issue. Also, please remove “page.Dispose();” from the code in order to prevent exception.