Hello
Using the artifact object in my code makes it go to the first sheet with the opacity I want but after the first sheet the opacity returns to 1. Example:
watermark.pdf (865.2 KB)
This is the code I am working with:
public static void main(String[] args) throws Exception {// Main function to add watermark to PDF in Java
// Initialize License Instance
License license = new License();
// Call setLicense method to set license
license.setLicense(“src/Aspose.Pdf.Java.lic”);
long startTime = System.currentTimeMillis();
// Load PDF document
Document doc = new Document("src/marcaDeAgua/AyDSI_I_-_Clase_1.pdf");
// Create formatted text
FormattedText formattedText = new FormattedText("Restringido", java.awt.Color.BLUE, FontStyle.Courier,
EncodingType.Identity_h, true, 20);
//Crea el Watermark
WatermarkArtifact artifact = new WatermarkArtifact();
artifact.setText(formattedText);
artifact.setBackground(false);
//Setea la marca de agua en la hoja por primera vez para recuperar despues sus medidas
doc.getPages().get_Item(1).getArtifacts().add(artifact);
//Ancho y altura del artefacto
double anchoWt = artifact.getRectangle().getWidth();
double alturaWt = artifact.getRectangle().getHeight();
// For de la hoja a trabajar
for (int i = 1; i <= doc.getPages().size(); i++) {
Rectangle mediabox = doc.getPages().get_Item(i).getMediaBox();
// For de la altura para el watermark
for (double h = 0; h <= mediabox.getHeight(); h = h + (alturaWt + 10)) {
// For para el ancho para el watermark
for (double w = 0; w <= mediabox.getWidth(); w = w + (anchoWt+8)) {
// Pointer de la ubicacion del watermark
Point point = new Point(w, h);
artifact.setPosition(point);
artifact.setOpacity(0.5);
//Setea la marca de agua en la hoja
doc.getPages().get_Item(i).getArtifacts().add(artifact);
}
}
}
// Save watermarked PDF document
doc.save("src/marcaDeAgua/watermark.pdf");
long tarda = System.currentTimeMillis() - startTime;
System.out.println(tarda);
System.out.println("Done");
doc.close();
}
@FedericoK
Can you please share the respective input document as well? We will test the scenario in our environment and address it accordingly.
of course, here is the input doc:
AyDSI_I_-_Clase_1.pdf (206.6 KB)
but you can do it with any pdf and the resut will be the same.
@FedericoK
We were able to notice the issue in our environment while testing the case with 22.12 version of the API. Hence, it has been logged as PDFJAVA-42373 in our issue tracking system. We will further look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.
We are sorry for the inconvenience.
Thank you, I’ll wait for the reponse.
The issues you have found earlier (filed as PDFJAVA-42373) have been fixed in Aspose.PDF for Java 23.2.
@asad.ali
Hello, the same question all happened in c#.
Only first page get opacity 0.5. Other page still opacity 1
public byte[] AddWatermarksOnPdf2(InputFileInfoExpDto input)
{
using (var pdfOrigin = new Aspose.Pdf.Document(input.FilePath))
using (var ms = new MemoryStream())
{
Aspose.Pdf.WatermarkArtifact artifact = new Aspose.Pdf.WatermarkArtifact();
//artifact.SetTextAndState
//(
// $"{input.DisplayName} ({input.NetId}) \n" +
// $"{input.Email} \n" +
// $"{input.DownloadTime.ToString("yyyy-MM-dd HH:mm:ss")}",
// new Aspose.Pdf.Text.TextState()
// {
// FontSize = 20,
// ForegroundColor = Aspose.Pdf.Color.Gray,
// //Font = Aspose.Pdf.Text.FontRepository.FindFont("Courier"),
// LineSpacing = 10
// }
//);
//Aspose.Pdf.Facades.FormattedText formattedText = new Aspose.Pdf.Facades.FormattedText($"{input.DisplayName} ({input.NetId})");
Aspose.Pdf.Facades.FormattedText formattedText = new Aspose.Pdf.Facades.FormattedText($"{input.DisplayName} ({input.NetId})",
new Aspose.Pdf.Facades.FontColor(128, 128, 128),
Aspose.Pdf.Facades.FontStyle.Unknown,
Aspose.Pdf.Facades.EncodingType.Identity_h,
true,
20,
10);
formattedText.AddNewLineText(input.Email, 10);
formattedText.AddNewLineText(input.DownloadTime.ToString("yyyy-MM-dd HH:mm:ss"), 10);
artifact.SetText(formattedText);
artifact.IsBackground = false;
pdfOrigin.Pages[1].Artifacts.Add(artifact);
var widthWm = artifact.Rectangle.Width;
var heightWm = artifact.Rectangle.Height;
//artifact.Rotation = 45;
//artifact.Opacity = 0.5;
double xGap = 10;
double yGap = 10;
foreach (var page in pdfOrigin.Pages)
{
var width = page.MediaBox.Width;
var height = page.MediaBox.Height;
int horizonCount = (int)(width / (widthWm + xGap)) + 1;
int verticalCount = (int)(height / (heightWm + yGap)) + 1;
for (int i = 0; i < horizonCount; i++)
{
for (int j = 0; j < verticalCount; j++)
{
double x = i * (artifact.Rectangle.Width + xGap);
double y = j * (artifact.Rectangle.Height + yGap);
artifact.Position = new Aspose.Pdf.Point(x, y);
artifact.Opacity = 0.5;
page.Artifacts.Add(artifact);
}
}
//artifact.Position = new Aspose.Pdf.Point(50, 100);
//page.Artifacts.Add(artifact);
//page.Artifacts[page.Artifacts.Count].Position = new Aspose.Pdf.Point(0, 0);
//var rect = artifact.Rectangle;
}
pdfOrigin.Save(ms);
return ms.ToArray();
}
}
@msdos41
Is it possible if you could please share the sample PDF document with us that you are using? We will further proceed to address the issue accordingly.