Zum Inhalt springen
NexPDF

Annotationen

PDF-Annotationen hinzufügen, auflisten, verschieben, entfernen und verflachen sowie XFDF importieren oder exportieren, mit NexPDF für Go.

Deutsch

Lizenzmodul

Das Erstellen und Ändern von Annotationen benötigt das Modul annotations (Tarif Indie und höher). Das Auflisten von Annotationen mit AnnotationsJSON funktioniert ohne Schlüssel.

Koordinaten sind PDF-Punkte mit dem Ursprung unten links auf der Seite. Rechtecke sind llx, lly, urx, ury.

Markierungen und Formen hinzufügen

AddAnnotation(pageIndex, kind, colorHex, opacity, contents, llx, lly, urx, ury) fügt eine Annotation eines PDF-Subtyps hinzu, etwa Highlight, Underline, StrikeOut, Squiggly, Square, Circle, FreeText oder Text (eine Haftnotiz). Textmarkierungen werden an die Zeilen unter dem Rechteck angepasst.

doc, err := nexpdf.Open("contract.pdf")
if err != nil {
	log.Fatal(err)
}
defer doc.Close()

doc.AddAnnotation(0, "Highlight", "#FFD400", 1.0, "", 72, 700, 300, 716)
doc.AddAnnotation(0, "FreeText", "#E5484D", 1.0, "Checked", 72, 640, 260, 660)
doc.AddAnnotation(0, "Text", "#2563EB", 1.0, "Please confirm this date", 320, 700, 340, 720)
doc.Save("annotated.pdf", false)

Linien, Pfeile und Freihandzeichnungen

// A red arrow from (100, 500) to (250, 560), 2 pt wide.
doc.AddLine(0, "Arrow", "#DC2626", 1.0, 2.0, 100, 500, 250, 560)

// Freehand ink: points "x,y;x,y;..." in PDF points.
doc.AddInk(0, "#1D4ED8", 2.0, 1.0, "100,400;120,410;140,405;160,420")

Eine handschriftliche Signatur ist eine Freihand-Annotation mit mehreren Strichen, getrennt durch |:

doc.AddInkSignature(0, "#111827", 1.5, "300,120;320,130;340,118|350,125;380,128")

Das ist nur eine visuelle Signatur. Zur kryptografischen Signatur siehe Signaturen.

Bildstempel und Messungen

// A picture stamp from RGBA pixels (width x height x 4 bytes), here 240 x 120.
doc.AddImageStamp(0, 400, 60, 520, 120, rgba, 240, 120, 1.0)

// A stored distance measurement; per_point converts PDF points to the unit.
m := `{"kind":"distance","points":[[100,100],[300,100]],"unit":"mm","per_point":0.3528}`
s, err := doc.AddMeasurement(0, m)
var result struct {
	Text string `json:"text"`
}
json.Unmarshal([]byte(s), &result)
fmt.Println(result.Text)

kind ist distance, perimeter oder area. doc.MeasurementsJSON() listet die gespeicherten auf.

Auflisten, bearbeiten, verschieben und entfernen

type Annotation struct {
	Num      uint32 `json:"num"`
	Gen      uint16 `json:"gen"`
	Kind     string `json:"kind"`
	Contents string `json:"contents"`
}

s, _ := doc.AnnotationsJSON(0)
var annots []Annotation
json.Unmarshal([]byte(s), &annots)
for _, a := range annots {
	fmt.Println(a.Num, a.Gen, a.Kind, a.Contents)
}

a := annots[0]
doc.SetAnnotationContents(a.Num, a.Gen, "Updated note")
doc.MoveAnnotation(a.Num, a.Gen, 10, -20) // dx, dy in points
doc.RemoveAnnotation(0, a.Num, a.Gen)

Annotationen werden über ihre Objektnummer (num) und Generation (gen) identifiziert.

Verflachen

Das Verflachen brennt Annotationen in den Seiteninhalt ein, sodass sie nicht mehr bearbeitet werden können:

doc.FlattenAnnotations(-1) // every page; pass a page index for one page

XFDF-Import und -Export

xfdf, err := doc.ExportXFDF()

other, err := nexpdf.Open("clean.pdf")
if err != nil {
	log.Fatal(err)
}
defer other.Close()
other.ImportXFDF(xfdf)
other.Save("with-annotations.pdf", false)

Dateianhänge

AddFileAttachmentAnnot(pageIndex, x, y, name, data, mime, description) fügt ein Büroklammer-Symbol hinzu, dessen obere linke Ecke bei (x, y) liegt:

data, _ := os.ReadFile("invoice.xml")
doc.AddFileAttachmentAnnot(0, 520, 760, "invoice.xml", data, "application/xml", "Source data")

Auf dieser Seite