คำอธิบาย

เพิ่มการสนับสนุนคำอธิบายในตัวดู

คำอธิบายใน Doconut ทำงานในสองทิศทาง: ผู้ใช้วาดมันในวิดเจ็ตของเบราว์เซอร์และเซิร์ฟเวอร์บันทึกไว้ต่อหน้า, หรือโค้ดของคุณสร้างมันโดยโปรแกรมและโหลดเข้าเซสชันที่เปิดอยู่. ไม่ว่าจะอย่างใดก็ตาม มันจะแสดงบนหน้าและสามารถฝังลงในไฟล์ PDF/PNG ที่ส่งออกได้.

การสนับสนุนคำอธิบายถูกจำกัดโดยความสามารถของใบอนุญาต Annotation (ได้รับโดยอัตโนมัติภายใต้ใบอนุญาต Temporary ที่ใช้งานอยู่).

เปิดใช้งาน UI ของคำอธิบาย

คำอธิบายเป็นโมดูลของ Viewer ไม่ใช่แถบเครื่องมือแยกส่วน หน้าเต็มต้องรวมทรัพยากร Viewer, แถบเครื่องมือ Viewer, การเมานท์ Viewer, และ objViewer ที่ได้เริ่มต้นแล้ว; จากนั้น Ribbon ของคำอธิบายจะถูกเมานท์และแนบกับอินสแตนซ์เดียวกันนั้น.

ส่งออกชุดบันเดิลคำอธิบรร่วมกับบันเดิล Viewer — พวกมันถูกจำกัดโดยใบอนุญาต, ดังนั้นแท็กจะปรากฏเฉพาะเมื่อความสามารถพร้อมใช้งาน:

html
@Html.Raw(Viewer.ReferenceCss(new CssConfig
{
    IncludeViewerCss     = true,
    IncludeAnnotationCss = true   // jquery-ui.min.css + annotationBar.css
}))

@Html.Raw(Viewer.ReferenceScripts(new ScriptConfig
{
    IncludeJQuery             = true,
    IncludeViewerScripts      = true,
    IncludeAnnotationScripts  = true, // jquery-ui, raphael.js, annotation.js
    IncludeAnnotationBar      = true  // ริบบิ้นการทำหมายเหตุที่ฝังอยู่
}))

ทำให้การประกอบ Viewer ทั้งหมดมองเห็นได้ใน markup:

html
<nav id="toolbar" aria-label="Document viewer controls">
    <!-- การควบคุม Viewer, รวมถึงปุ่มที่เปิด Annotation -->
</nav>
<div id="annBarMount"></div>
<div id="divDocViewer"><div id="div_ctlDoc"></div></div>

บันเดิลคำอธิบายสร้าง DOM ของ Ribbon ภายใน annBarMount; คุณไม่จำเป็นต้องคัดลอกปุ่มหรือ markup ของไดอะล็อก. เริ่มต้น docViewer ก่อน, แล้วสร้าง Ribbon เฉพาะเมื่อเซิร์ฟเวอร์ยืนยันว่าคำอธิบายได้รับใบอนุญาต:

html
<script>
    let annBar = null;
    let currentToken = '';

    const objViewer = $('#div_ctlDoc').docViewer({
        BasePath: 'doconut',
        ResPath: 'doconut-res/images',
        onAnnLoaded:    () => annBar?.handleAnnLoaded(),
        onAnnSaved:     () => annBar?.handleAnnSaved(),
        onAnnSaveError: () => annBar?.handleAnnSaveError(),
        onAnnClosed:    () => annBar?.handleAnnClosed(),
        onError:        (message) => console.error('Viewer error:', message)
    });

    @if (Viewer.IsAnnotationEnabled)
    {
        <text>
    annBar = $('#annBarMount').doconutAnnotationBar({
        docId: 'ctlDoc',
        getRequestParams: () => ({ token: currentToken }),
        onStatus: (message) => console.log(message),
        onToast: (message, type) => console.log(type, message),
        onLayout: () => requestAnimationFrame(() => objViewer.Refit())
    });
    annBar.attach(objViewer);
        </text>
    }
</script>

หากไม่มีความสามารถ, ReferenceScripts จะละเว้นสคริปต์ของ Ribbon, ดังนั้น if ฝั่งเซิร์ฟเวอร์จะรักษาการเรียก doconutAnnotationBar(...) ที่ไม่มีการป้องกันไม่ให้ทำลายหน้า.

การบันทึกจาก Ribbon จะโพสต์ข้อมูลผ่าน middleware (AnnSave), ซึ่งเก็บไว้ในเซสชันเอกสารต่อหน้า. การโหลด (AnnLoad) เกิดขึ้นโดยอัตโนมัติเมื่อหน้าที่มีคำอธิบายแสดงผล. สี่ callback onAnn* รักษาให้ Ribbon สอดคล้องกับวงจรชีวิตของ viewer.

เปิดและปิดมันจากแถบเครื่องมือ Viewer ที่เป็นของโฮสต์ใดก็ได้:

javascript
annBar.open();
annBar.close();

API สาธารณะของ Ribbon คือ:

MethodPurpose
attach(objViewer)เชื่อมต่อ Ribbon กับ viewer ที่ได้เริ่มต้นแล้ว; จำเป็นต้องทำครั้งเดียว
open() / close()เข้าสู่หรือออกจากการแก้ไขคำอธิบาย
reset()คืน Ribbon ไปสู่สถานะปิด, ไม่อยู่ในโหมดแก้ไข
isOpen() / annotating()อ่านสถานะของ Ribbon / สถานะการแก้ไขคำอธิบายของ viewer
reopenEditable()โหลดคำอธิบายของหน้าปัจจุบันใหม่เป็นอ็อบเจ็กต์ที่สามารถแก้ไขได้
updateActionState()รีเฟรชความพร้อมใช้งานของการควบคุมบันทึก/ลบหลังจากการเปลี่ยนแปลงของโฮสต์
headerSlot()รับช่องส่วนขยายหัวเรื่องที่เป็นตัวเลือกสำหรับควบคุมที่เป็นของโฮสต์

onStatus, onToast, onLayout, onEditStart, และ onEditEnd เป็น callback ของโฮสต์ที่เป็นตัวเลือก. วัตถุ endpoints สามารถให้ exportPdf, exportPng, imageUpload, และ imageList เพิ่มเติม; ควบคุมที่ไม่มี endpoint ที่กำหนดจะยังคงซ่อนอยู่.

สำหรับลำดับการเริ่มต้นของ Viewer, Search, และ Annotation ที่รวมกัน, ดูที่ เริ่มต้นอย่างเร็ว.

บันเดิลคำอธิบายเพิ่มเครื่องมือการสร้างในเบราว์เซอร์, แต่ข้อมูลยังคงเป็นของเซสชันเอกสารฝั่งเซิร์ฟเวอร์ที่ระบุด้วยโทเค็น. การเปิดแหล่งใหม่จะสร้างเซสชันใหม่; เก็บ XML หรือ envelope ของคำอธิบายที่เข้ารหัสไว้ในแอปพลิเคชันของคุณหากคำอธิบายต้องคงอยู่หลังจากอายุของเซสชันสิ้นสุด.

สองประเภทของ callback, เชื่อมต่อสองวิธี

โค้ดการทำ annotation ใช้สองรูปแบบของ callback, และแต่ละแบบจะล้มเหลวโดยไม่มีการแจ้งเตือนเมื่อเชื่อมต่อในทางตรงกันข้าม:

  • onAnnLoaded, onAnnSaved, onAnnSaveError, onAnnClosed เป็น ตัวเลือก ของ docViewer({...}), เช่น callback ของ viewer ทุกตัว.
  • Created, Changed, Deleted, and Properties เป็น ฟังก์ชันระดับโลกที่ค้นหาตามชื่อ: โดยมีไอดีของ viewer นำหน้ากับเหตุการณ์. สำหรับ viewer ใน #div_ctlDoc ไอดีคือ ctlDoc, ดังนั้นฟังก์ชันจะเป็น ctlDoc_Created, ctlDoc_Changed, ctlDoc_Deleted, และ ctlDoc_Properties. ฟังก์ชันที่สะกดผิดหรือเปลี่ยนชื่อจะไม่ถูกเรียกใช้เลย.

SaveAnnotations() ไม่ได้คืนค่าว่าบันทึกสำเร็จหรือไม่: ผลลัพธ์จะมาผ่าน onAnnSaved หรือ onAnnSaveError. โค้ดที่อ่านค่าที่คืนกลับจะไม่เคลียร์แฟล็ก "unsaved changes" flag.

สร้าง annotation ด้วย C#

รับ manager ที่ผูกกับเซสชันที่เปิดอยู่, เพิ่ม annotation, และโหลดพวกมัน (โดยใช้ using Doconut.Annotations; สำหรับประเภทและ using System.Drawing; สำหรับ Rectangle/Color):

csharp
app.MapPost("/api/annotations/load-sample", (string token, Viewer viewer) =>
{
    // ผูกกับมิติของหน้าที่เปิดอยู่ในเซสชัน
    var manager = viewer.GetAnnotationManager(token);
    var pageCount = viewer.GetPageCount(token);

    // หนึ่งตราประทับต่อหน้า
    for (int page = 1; page <= pageCount; page++)
    {
        manager.Add(new StampAnnotation(page, new Rectangle(30, 20, 240, 90),
            $"PAGE {page}", 28, 4, Color.Maroon)
        {
            Opacity = 60,
            Rotate  = -8
        });
    }

    manager.Add(new NoteAnnotation(1, new Rectangle(420, 150, 220, 120),
        "Loaded from C# code.", Color.FromArgb(255, 255, 255, 170), 14));

    // โหลดเข้าสู่เซสชัน — วิดเจ็ตดึงข้อมูลเหล่านั้นผ่าน AnnLoad และ the
    // renderer แปลงพวกมันเป็น image/PDF exports.
    viewer.LoadAnnotationData(token, manager);
    return Results.Ok();
});

ประเภทของ Annotation

ทุกประเภทอยู่ใน Doconut.Annotations และสืบทอดจาก BaseAnnotation (หมายเลขหน้า + bounding Rectangle):

ประเภทหมายเหตุ
StampAnnotationปั๊มข้อความพร้อมขนาดฟอนต์, เส้นขอบ, สี; รองรับ Opacity, Rotate
NoteAnnotationโน้ตติดกาวพร้อมข้อความ, สีพื้นหลัง, ขนาดฟอนต์, TitleColor
RectangleAnnotationสีเส้นขอบ + สีเติม, Title/ShowTitle
CircleAnnotationเส้นขอบ + เติม, ShowBorder
EllipseAnnotationเส้นขอบ + เติม, ShowBorder
TriangleAnnotationสีเส้นขอบ, BackColor, ShowBorder
LineAnnotationเส้นตรงพร้อมความกว้างและสี
ArrowAnnotationเส้นพร้อมหัวศร; สามารถตั้งค่า Direction (ประเภท ArrowDirection, จุดทิศทาง, ค่าเริ่มต้น E)
FreehandAnnotationเส้นอิสระจากจุดที่เข้ารหัส FreehandData
ImageAnnotationรูปภาพจาก URL. URL relative จะถูกแก้ไขตามโฮสต์ของคำขอเมื่อเพิ่ม annotation; เซิร์ฟเวอร์จะดึงรูปภาพเฉพาะในเวลาการบันทึก — ดูหมายเหตุการส่งออกด้านล่าง

API ของ AnnotationManager

สมาชิกวัตถุประสงค์
Add(BaseAnnotation)คิว annotation
GetAnnotations() / GetAnnotations(int page)ตรวจสอบสิ่งที่ manager เก็บไว้
ClearAnnotations() / ClearAnnotations(int page)ลบทั้งหมด / ตามหน้า
GetAnnotationData() / GetAnnotationData(int page)สตริงข้อมูล annotation ที่เข้ารหัส — ซอง Base64 (ที่วิดเจ็ตใช้)
GetAnnotationXml()ฟอร์ม XML

Viewer สะท้อนการโหลด/อ่านต่อกับเซสชัน: LoadAnnotationData(token, manager) หรือ LoadAnnotationData(token, encodedData) (ซอง Base64 จาก GetAnnotationData()), LoadAnnotationXML(token, xml), GetAnnotationXML(token).

ส่งออกพร้อม annotation ที่ฝังไว้

csharp
// PDF ของทุกหน้าพร้อมคำอธิบายที่เรนเดอร์ลงบนหน้า
app.MapGet("/api/annotations/export-pdf", async (string token, Viewer viewer) =>
{
    byte[] pdf = await viewer.ExportAnnotationsToPdfAsync(token, zoom: 100);
    return Results.File(pdf, "application/pdf", "export.pdf");
});

// หรือ ZIP ของ PNG ต่อหน้า
app.MapGet("/api/annotations/export-png-zip", async (string token, Viewer viewer) =>
{
    byte[] zip = await viewer.ExportAnnotationsToPngZipAsync(token, zoom: 100);
    return Results.File(zip, "application/zip", "annotations-png.zip");
});

การส่งออกใช้เครื่องมือบันทึกเดียวกับการเรนเดอร์บนหน้าจอ, ดังนั้นสิ่งที่ผู้ใช้เห็นคือสิ่งที่ไฟล์มี — ยกเว้น annotation รูปภาพ, จะอธิบายในต่อไป.

การทำหมายเหตุภาพบนโฮสต์อินทราเน็ตส่งออกเป็น “ภาพถูกบล็อก”

The viewer and the export fetch an ImageAnnotation's image from different places:

พื้นผิวผู้ที่ดาวน์โหลดภาพบนโฮสต์อินทราเน็ตหรือการพัฒนา
ตัวดูในเบราว์เซอร์เบราว์เซอร์ของผู้ใช้ภาพปรากฏ
ExportAnnotationsToPdfAsync / …PngZipAsyncเซิร์ฟเวอร์ตัวแทนที่สีแดง "ภาพถูกบล็อก"

When the server fetches an image to burn it in, it refuses addresses that point back into private networks: loopback (localhost, 127.0.0.1), 0.0.0.0, link‑local (169.254.x.x), the private ranges 10.x, 172.16–31.x, and 192.168.x, carrier‑grade NAT (100.64.0.0/10), and IPv6 link‑local and unique‑local addresses. This protects the server from being used to reach internal systems through a crafted image URL, and there is no switch to turn it off.

A relative image URL resolves against the host of the current request, so on localhost, on an intranet server, or on any site reached through a private address, the image shows in the viewer and the export contains the placeholder instead. That is the guard working, not a rendering failure. If exports must contain the image, serve it from a host with a public address and use an absolute URL. A host name that cannot be resolved is refused the same way; any other download failure draws the URL and the error message in red where the image would be.

กระบวนการคงสภาพ

  1. เปิดเอกสารและรับโทเค็นของมัน.
  2. โหลด XML หรือข้อมูลที่เข้ารหัสที่เก็บไว้ก่อนหน้านี้เข้าสู่โทเค็นนั้น.
  3. ให้วิดเจ็ตอ่านและแก้ไขหมายเหตุของเซสชัน.
  4. ดึง XML ด้วย GetAnnotationXML(token) เมื่อแอปพลิเคชันของคุณตัดสินใจบันทึก.
  5. ส่งออกเป็น PDF/PNG เมื่อจำเป็นต้องมีไฟล์ส่งมอบที่แบน.
  6. ปิดเซสชันของเอกสาร.

Do not use the opaque viewer token as a permanent annotation identifier. Associate persisted annotation data with your own document and version identifiers.

บันทึกด้านความปลอดภัยและการเรนเดอร์

  • คำขอหมายเหตุใช้ความปลอดภัยของเซสชัน/โทเค็นเดียวกับคำขอหน้า.
  • ตรวจสอบและควบคุม URL ของภาพที่ผู้ใช้ให้มาเพื่อหลีกเลี่ยงการปลอมแปลงคำขอจากฝั่งเซิร์ฟเวอร์.
  • การส่งออกใช้การตัดสินใจเรื่องใบอนุญาต/ลายน้ำแบบกำหนดเองเช่นเดียวกับการเรนเดอร์หน้าในหน้าจอ.
  • ข้อมูลฟรีแฮนด์ขนาดใหญ่และการส่งออกความละเอียดสูงจะเพิ่มการใช้หน่วยความจำ; ทดสอบเอกสารที่เป็นจริงและค่าการซูม.

การแก้ไขปัญหา

อาการการตรวจสอบ
ริบบิ้นหมายเหตุหายไปAnnotation ความสามารถและสี่แฟล็ก CSS/สคริปต์ของหมายเหตุ
คอลแบ็กการบันทึกรายงานข้อผิดพลาดการหมดอายุของโทเค็น/เซสชันและมิดเดิลแวร์ BasePath
ctlDoc_Created (หรือฟังก์ชันเหตุการณ์อื่น) ไม่เคยทำงานชื่อฟังก์ชัน: viewer id + _ + event, เป็นฟังก์ชันระดับ global
หมายเหตุ C# ไม่แสดงการนับหน้าตั้งแต่ 1 และข้อมูลถูกโหลดเข้าสู่โทเค็นที่ใช้งาน
หมายเหตุภาพแสดงบนหน้าจอแต่ไม่อยู่ในการส่งออกโฮสต์ของภาพมีที่อยู่ส่วนตัวหรือ loopback — ดูด้านบน
เอกสารที่เปิดใหม่ไม่มีหมายเหตุบันทึก XML/ข้อมูลนอกเซสชันตัวดู, จากนั้นโหลดเข้าสู่โทเค็นใหม่

หน้านี้เป็นประโยชน์หรือไม่?