Aggiungi Filigrana al PDF

Durante la conversione del file Excel in PDF, potresti avere esigenze di aggiungere una filigrana al file PDF. Gli esempi seguenti mostrano come aggiungere una filigrana di testo e immagine al PDF durante il rendering in PDF.

Aggiungi filigrana di testo al PDF

Puoi facilmente aggiungere una filigrana di testo al pdf specificando il testo e il relativo font. Inoltre, puoi impostare l’allineamento, lo spostamento, la rotazione, l’opacità, il primo piano/sfondo e la scala della pagina in RenderingWatermark.

from aspose.cells import PaperSizeType, PdfSaveOptions, TextAlignmentType, Workbook
from aspose.cells.rendering import RenderingFont, RenderingWatermark
from aspose.pydrawing import Color
# prepare a workbook with 3 pages.
wb = Workbook()
wb.worksheets[0].cells.get("A1").put_value("Page1")
index = wb.worksheets.add()
wb.worksheets[index].cells.get("A1").put_value("Page2")
index = wb.worksheets.add()
wb.worksheets[index].cells.get("A1").put_value("Page3")
wb.worksheets[index].page_setup.paper_size = PaperSizeType.PAPER_A3
# create a font for watermark, and specify bold, italic, color.
font = RenderingFont("Calibri", 68)
font.italic = True
font.bold = True
font.color = Color.blue
# create a watermark from text and the specified font.
watermark = RenderingWatermark("Watermark", font)
# specify horizontal and vertical alignment
watermark.h_alignment = TextAlignmentType.CENTER
watermark.v_alignment = TextAlignmentType.CENTER
# specify rotation
watermark.rotation = 30.0
# specify opacity
watermark.opacity = 0.6
# specify the scale to page(e.g. 100, 50) in percent.
watermark.scale_to_page_percent = 50
# spcify watermark for rendering to pdf.
options = PdfSaveOptions()
options.watermark = watermark
wb.save("output_text_watermark.pdf", options)

Aggiungi filigrana di immagine al PDF

Puoi aggiungere una filigrana di immagine al pdf semplicemente specificando i byte dell’immagine. Inoltre, puoi impostare l’allineamento, lo spostamento, la rotazione, l’opacità, il primo piano/sfondo e la scala della pagina in RenderingWatermark.

from aspose.cells import PaperSizeType, PdfSaveOptions, Workbook
from aspose.cells.rendering import RenderingWatermark
# prepare a workbook with 3 pages.
wb = Workbook()
wb.worksheets[0].cells.get("A1").put_value("Page1")
index = wb.worksheets.add()
wb.worksheets[index].cells.get("A1").put_value("Page2")
index = wb.worksheets.add()
wb.worksheets[index].cells.get("A1").put_value("Page3")
wb.worksheets[index].page_setup.paper_size = PaperSizeType.PAPER_A3
# create a watermark from image(you need to prepare image bytes).
watermark = RenderingWatermark(imageBytes)
# specify offset to alignment.
watermark.offset_x = 100.0
watermark.offset_y = 200.0
# specify rotation
watermark.rotation = 30.0
# specify watermark to background.
watermark.is_background = True
# specify opacity
watermark.opacity = 0.6
# specify the scale to page(e.g. 100, 50) in percent.
watermark.scale_to_page_percent = 50
# spcify watermark for rendering to pdf.
options = PdfSaveOptions()
options.watermark = watermark
wb.save("oputput_image_watermark.pdf", options)