ajaxray/markpdf

what about my code to support full page water mark

Opened this issue · 0 comments

func drawText_offset(p *creator.Paragraph, c *creator.Creator, offsetX float64, offsetY float64, att *text_attributes) {
	// Change to times bold font (default is helvetica).
	p.SetFont(getFontByName(font))
	p.SetFontSize(att.FontSize)
	p.SetPos(offsetX, offsetY)
	p.SetColor(creator.ColorRGBFromHex("#" + att.FontColor))
	p.SetAngle(att.Angle)

	// Encountering problem with tiles and text watermark. Contributions welcome.
	//if tiles {
	//	repeatTiles(p, c)
	//	return
	//}

	_ = c.Draw(p)
}


func markPDF(inputPath string, outputPath string, watermark string) error {
	debugInfo(fmt.Sprintf("Input PDF: %v", inputPath))

	c := creator.New()
	var watermarkImg *creator.Image
	var para *creator.Paragraph

	isImageMark := isImageMark(watermark)
	watermarkIsATemplate := isWatermarkATemplate(watermark)

	// Read the input pdf file.
	f, err := os.Open(inputPath)
	fatalIfError(err, fmt.Sprintf("Failed to open the source file. [%s]", err))
	defer f.Close()

	pdfReader, err := pdf.NewPdfReader(f)
	fatalIfError(err, fmt.Sprintf("Failed to parse the source file. [%s]", err))

	numPages, err := pdfReader.GetNumPages()
	fatalIfError(err, fmt.Sprintf("Failed to get PageCount of the source file. [%s]", err))

	// Prepare data to insert into the template.
	rec := Recipient{
		Pages:    numPages,
		Filename: filepath.Base(inputPath[:len(inputPath)-len(filepath.Ext(inputPath))]),
	}
	var t *template.Template
	if !isImageMark && watermarkIsATemplate {
		t = template.Must(template.New("watermark").Parse(watermark))
	}

	for i := 0; i < numPages; i++ {
		pageNum := i + 1
		rec.Page = pageNum

		// Read the page.
		page, err := pdfReader.GetPage(pageNum)
		fatalIfError(err, fmt.Sprintf("Failed to read page from source. [%s]", err))

		// Add to creator.
		c.AddPage(page)

		// Calculate the position on first page
		if pageNum == 1 {
			debugInfo(fmt.Sprintf("Page Width       : %v", c.Context().PageWidth))
			debugInfo(fmt.Sprintf("Page Height      : %v", c.Context().PageHeight))
		}

		if isImageMark {
			if pageNum == 1 {
				watermarkImg, err = creator.NewImageFromFile(watermark)
				fatalIfError(err, fmt.Sprintf("Failed to load watermark image. [%s]", err))
				adjustImagePosition(watermarkImg, c)
			}

			drawImage(watermarkImg, c)

		}
		var need_draw_text = !isImageMark || full
		if need_draw_text {
			var attr text_attributes
			var text = watermark
			if full {
				attr.Alpha = float64(water_mark_config.Outdevconf.MaskConf.Alpha)
				attr.Angle = float64(water_mark_config.Outdevconf.MaskConf.Angle)
				attr.FontSize = float64(water_mark_config.Outdevconf.MaskConf.Fontsize)
				attr.FontColor = water_mark_config.Outdevconf.MaskConf.Fontcolor[1:]
				text = water_mark_config.Outdevconf.MaskText
			}
			if pageNum == 1 {
				para = creator.NewParagraph(text)
				adjustTextPosition(para, c)
			}

			if watermarkIsATemplate {
				applyTemplate(t, &rec, para)
			}
			if full {
				var gap_y = c.Context().PageHeight / 2
				var gap_x = c.Context().PageWidth / 4
				for x := 0; x < int(c.Context().PageWidth); x = x + int(gap_x) {
					for y := 0; y < int(c.Context().PageHeight); y = y + int(gap_y) {
						if x%2 == 0 {
							drawText_offset(para, c, float64(x), float64(y), &attr)
						} else {
							drawText_offset(para, c, float64(x), float64(y+int(gap_y)/2), &attr)
						}
					}
				}
			} else {
				drawText(para, c)
			}
		}
	}

	err = c.WriteToFile(outputPath)
	return err
}