注释 (Annotations)
常规注释
一个 annotation 注释将对象(如注释,线条和高亮)与 PDF 文档页面上的位置相关联。其提供了一种通过鼠标和键盘与用户进行交互的方式。PDF 包括以下表格中列出的各种标准注释类型。在这些注释类型中,许多被定义为标记注释,因为它们主要用于标记 PDF 文档。标记注释中作为其自身一部分的文本,可以在其他符合标准的阅读器中以其他方式显示,例如在 Comments 面板。以下表格中的 Markup
列用来说明是否为标记注释。
福昕 PDF SDK 支持 PDF Reference 中定义的大多数注释类型。福昕 PDF SDK 提供了注释创建,属性访问和修改,外观设置和绘制的 APIs。
注释类型 | 描述 | Markup | SDK 是否支持 |
---|---|---|---|
Text(Note) | Text annotation | Yes | Yes |
Link | Link Annotation | No | Yes |
FreeText (TypeWriter/TextBox/Callout) | Free text annotation | Yes | Yes |
Line | Line annotation | Yes | Yes |
Square | Square annotation | Yes | Yes |
Circle | Circle annotation | Yes | Yes |
Polygon | Polygon annotation | Yes | Yes |
PolyLine | PolyLine annotation | Yes | Yes |
Highlight | Highlight annotation | Yes | Yes |
Underline | Underline annotation | Yes | Yes |
Squiggly | Squiggly annotation | Yes | Yes |
StrikeOut | StrikeOut annotation | Yes | Yes |
Stamp | Stamp annotation | Yes | Yes |
Caret | Caret annotation | Yes | Yes |
Ink(pencil) | Ink annotation | Yes | Yes |
Popup | Popup annotation | No | Yes |
File Attachment | FileAttachment annotation | Yes | Yes |
Sound | Sound annotation | Yes | No |
Movie | Movie annotation | No | No |
Widget* | Widget annotation | No | Yes |
Screen | Screen annotation | No | Yes |
PrinterMark | PrinterMark annotation | No | No |
TrapNet | Trap network annotation | No | No |
Watermark* | Watermark annotation | No | Yes |
3D | 3D annotation | No | No |
Redact | Redact annotation | Yes | Yes |
NOTE
- Widget 和 watermark 注释类型是比较特殊的。
Annotation
模块不支持它们。Widget 类型仅在form filler
模块中使用,watermark 类型仅在watermark
模块中使用。 - 福昕 PDF SDK 支持名为 PSI (pressure sensitive ink,压感笔迹) 的自定义注释类型。在 PDF Reference 中没有对该注释进行描述。通常,PSI 用于手写签名功能,福昕 PDF SDK 将其视为 PSI 注释,以便其他 PDF 产品可以对其进行相关处理。
向 PDF 页面中添加 link 注释
c++
#include "include/common/fs_common.h"
#include "include/pdf/actions/fs_action.h"
#include "include/pdf/annots/fs_annot.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/objects/fs_pdfobject.h"
#include "include/pdf/fs_pdfpage.h"
using namespace foxit;
using namespace foxit::common;
using namespace pdf;
using namespace annots;
// Assuming PDFPage page has been loaded and parsed.
// Assuming the annnots in the page have been loaded.
// Add link annotation.
annots::Link link(page.AddAnnot( Annot::e_Link, RectF(350,350,380,400)));
link.SetHighlightingMode(Annot::e_HighlightingToggle);
...
C
#include "include/fs_basictypes_c.h"
#include "include/fs_common_c.h"
#include "include/fs_action_c.h"
#include "include/fs_annot_c.h"
#include "include/fs_pdfdoc_c.h"
#include "include/fs_pdfobject_c.h"
#include "include/fs_pdfpage_c.h"
// Assuming FS_PDFPAGE_HANDLE page has been loaded and parsed.
// Assuming the annnots in the page have been loaded.
// Add link annotation.
FSRectF rectf;
rectf.left = 350;
rectf.bottom = 350;
rectf.right = 380;
rectf.top = 400;
FS_ANNOT_HANDLE annot;
FSDK_PDFPage_AddAnnot(page, e_FSLink, rectf, &annot);
FS_LINKANNOT_HANDLE link;
FSDK_Link_Create0(annot, &link);
FSDK_Link_SetHighlightingMode(link, e_FSHighlightingToggle);
...
java
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.pdf.annots.Link;
...
// Assuming PDFPage page has been loaded and parsed.
// Add link annotation
Link link = new Link(page.addAnnot(Annot.e_Link, new RectF(350, 350, 380, 400)));
link.setHighlightingMode(Annot.e_HighlightingToggle);
// Appearance should be reset.
link.resetAppearanceStream();
...
py
import sys
import site
if sys.version_info.major == 2:
_PYTHON2_ = True
else:
_PYTHON2_ = False
if _PYTHON2_:
#replace with the python2 lib path
site.addsitedir('../../../')
from FoxitPDFSDKPython2 import *
else:
from FoxitPDFSDKPython3 import *
...
# Assuming PDFPage page has been loaded and parsed.
# Assuming the annnots in the page have been loaded.
# Add link annotation.
link = Link(page.AddAnnot(Annot.e_Link, RectF(350,350,380,400)))
link.SetHighlightingMode(Annot.e_HighlightingToggle)
objc
#include "FSPDFObjC.h"
...
// Assuming FSPDFPage page has been loaded and parsed.
// Add link annotation.
FSRectF* annot_rect = [[FSRectF alloc] initWithLeft1:350 bottom1:350 right1:380 top1:400];
FSLink* link = [[FSLink alloc] initWithAnnot:[page addAnnot:FSAnnotLink rect:annot_rect]];
[link setHighlightingMode:FSAnnotHighlightingToggle];
[link resetAppearanceStream];
...
js
const FSDK = require("@foxitsoftware/foxit-pdf-sdk-node");
...
// Assuming PDFPage page has been loaded and parsed.
// Assuming the annnots in the page have been loaded.
// Add link annotation.
let link = new FSDK.Link(page.AddAnnot(FSDK.Annot.e_Link, new FSDK.RectF(350, 350, 380, 400)));
link.SetHighlightingMode(FSDK.Annot.e_HighlightingToggle);
csharp
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
using foxit.pdf.graphics;
using foxit.pdf.annots;
using foxit.pdf.actions;
...
// Assuming PDFPage page has been loaded and parsed.
Annot annot = page.AddAnnot(Annot.Type.e_Link, new RectF(350, 350, 380, 400))
Link link = new Link(annot);
link.SetHighlightingMode(Annot.HighlightingMode.e_HighlightingToggle);
// Appearance should be reset.
link.ResetAppearanceStream();
...
向 PDF 页面中添加 highlight 注释,并且设置相关属性
c++
#include "include/common/fs_common.h"
#include "include/pdf/actions/fs_action.h"
#include "include/pdf/annots/fs_annot.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/objects/fs_pdfobject.h"
#include "include/pdf/fs_pdfpage.h"
using namespace foxit;
using namespace foxit::common;
using namespace pdf;
using namespace annots;
// Assuming PDFPage page has been loaded and parsed.
// Assuming the annnots in the page have been loaded.
// Add highlight annotation.
annots::Highlight highlight(page.AddAnnot(Annot::e_Highlight,RectF(10,450,100,550)));
highlight.SetContent(L"Highlight");
annots::QuadPoints quad_points;
quad_points.first = PointF(10, 500);
quad_points.second = PointF(90, 500);
quad_points.third = PointF(10, 480);
quad_points.fourth = PointF(90, 480);
annots::QuadPointsArray quad_points_array;
quad_points_array.Add(quad_points);
highlight.SetQuadPoints(quad_points_array);
highlight.SetSubject(L"Highlight");
highlight.SetTitle(L"Foxit SDK");
highlight.SetCreationDateTime(GetLocalDateTime());
highlight.SetModifiedDateTime(GetLocalDateTime());
highlight.SetUniqueID(WString::FromLocal(RandomUID()));
// Appearance should be reset.
highlight.ResetAppearanceStream();
...
C
#include "include/fs_basictypes_c.h"
#include "include/fs_common_c.h"
#include "include/fs_action_c.h"
#include "include/fs_annot_c.h"
#include "include/fs_pdfdoc_c.h"
#include "include/fs_pdfobject_c.h"
#include "include/fs_pdfpage_c.h"
int string2wstring(const char *source, size_t source_size, wchar_t *dest, size_t dest_size) {
const char *_source;
wchar_t *_dest;
if (!source || !dest) return 0;
_source = source;
_dest = dest;
setlocale(LC_ALL, "chs");
mbstowcs(_dest, _source, _Dsize);
setlocale(LC_ALL, "C");
return (int)dest_size;
}
...
// Assuming FS_PDFPAGE_HANDLE page has been loaded and parsed.
// Assuming the annnots in the page have been loaded.
// Add highlight annotation.
FSRectF rectf;
rectf.left = 10;
rectf.bottom = 450;
rectf.right = 100;
rectf.top = 550;
FS_ANNOT_HANDLE annot;
FSDK_PDFPage_AddAnnot(page, e_FSHighlight, rectf, &annot);
FS_HIGHLIGHTANNOT_HANDLE highlight;
FSDK_Highlight_Create0(annot, &highlight);
FSDK_Annot_SetContent(highlight, L"Highlight");
FSQuadPoints quad_points;
FSPointF point1;
point1.x = 10;
point1.y = 500;
quad_points.first = point1;
FSPointF point2;
point2.x = 90;
point2.y = 500;
quad_points.second = point2;
FSPointF point3;
point3.x = 10;
point3.y = 480;
quad_points.third = point3;
FSPointF point4;
point4.x = 90;
point4.y = 480;
quad_points.fourth = point4;
FSQuadPoints *quad_points_array = malloc(1 * sizeof(FSQuadPoints));
quad_points_array[0] = quad_points;
FSDK_TextMarkup_SetQuadPoints(highlight, quad_points_array, 1);
free(quad_points_array);
FSDK_Markup_SetSubject(highlight, L"Highlight");
FSDK_Markup_SetTitle(highlight, L"Foxit SDK");
FSDK_Markup_SetCreationDateTime(highlight, GetLocalDateTime());
FSDK_Annot_SetModifiedDateTime(highlight, GetLocalDateTime());
FSDK_Annot_SetUniqueID(highlight, WRandomUID());
// Appearance should be reset.
FS_BOOL return_code;
FSDK_Annot_ResetAppearanceStream(highlight, &return_code);
FSDK_Highlight_Release(highlight);
FSDK_Annot_Release(annot);
...
java
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.pdf.annots.Highlight;
import com.foxit.sdk.common.fxcrt.PointF;
import com.foxit.sdk.common.fxcrt.PointFArray;
import com.foxit.sdk.common.fxcrt.RectF;
import com.foxit.sdk.pdf.annots.QuadPoints;
import com.foxit.sdk.pdf.annots.QuadPointsArray;
...
// Assuming PDFPage page has been loaded and parsed.
// Add highlight annotation
Highlight highlight = new Highlight(page.addAnnot(Annot.e_Highlight, new RectF(10, 450, 100, 550)));
highlight.setContent("Highlight");
QuadPoints quad_points = new QuadPoints();
quad_points.setFirst(new PointF(10, 500));
quad_points.setSecond(new PointF(90, 500));
quad_points.setThird(new PointF(10, 480));
quad_points.setFourth(new PointF(90, 480));
QuadPointsArray quad_points_array = new QuadPointsArray();
quad_points_array.add(quad_points);
highlight.setQuadPoints(quad_points_array);
highlight.setSubject("Highlight");
highlight.setTitle("Foxit SDK");
highlight.setCreationDateTime(GetLocalDateTime());
highlight.setModifiedDateTime(GetLocalDateTime());
highlight.setUniqueID(RandomUID());
// Appearance should be reset.
highlight.resetAppearanceStream();
...
py
import sys
import site
if sys.version_info.major == 2:
_PYTHON2_ = True
else:
_PYTHON2_ = False
if _PYTHON2_:
#replace with the python2 lib path
site.addsitedir('../../../')
from FoxitPDFSDKPython2 import *
else:
from FoxitPDFSDKPython3 import *
...
# Assuming PDFPage page has been loaded and parsed.
# Assuming the annnots in the page have been loaded.
# Add highlight annotation.
highlight = Highlight(page.AddAnnot(Annot.e_Highlight,RectF(10,450,100,550)))
highlight.SetContent("Highlight")
quad_points = QuadPoints()
quad_points.first = PointF(10, 500)
quad_points.second = PointF(90, 500)
quad_points.third = PointF(10, 480)
quad_points.fourth = PointF(90, 480)
quad_points_array = QuadPointsArray()
quad_points_array.Add(quad_points)
highlight.SetQuadPoints(quad_points_array)
highlight.SetSubject("Highlight")
highlight.SetTitle("Foxit SDK")
highlight.SetCreationDateTime(GetLocalDateTime())
highlight.SetModifiedDateTime(GetLocalDateTime())
highlight.SetUniqueID(RandomUID())
# Appearance should be reset.
highlight.ResetAppearanceStream()
...
objc
#include "FSPDFObjC.h"
...
// Assuming FSPDFPage page has been loaded and parsed.
// Add highlight annotation.
FSRectF* annot_rect = [[FSRectF alloc] initWithLeft1:10 bottom1:450 right1:100 top1:550];
FSHighlight* highlight = [[FSHighlight alloc] initWithAnnot:[page addAnnot:FSAnnotHighlight rect:annot_rect]];
[highlight setContent:@"Highlight"];
FSQuadPoints* quad_points = [FSQuadPoints new];
FSPointF* point = [FSPointF new];
[point set:10 y:500];
quad_points.first = point;
[point set:90 y:500];
quad_points.second = point;
[point set:10 y:480];
quad_points.third = point;
[point set:90 y:480];
quad_points.fourth = point;
FSQuadPointsArray* quad_points_array = [FSQuadPointsArray new];
[quad_points_array add:quad_points];
[highlight setQuadPoints:quad_points_array];
[highlight setSubject:@"Highlight"];
[highlight setTitle:@"Foxit SDK"];
FSDateTime* local_time = getLocalDateTime();
[highlight setCreationDateTime:local_time];
[highlight setModifiedDateTime:local_time];
[highlight setUniqueID:randomUID()];
// Appearance should be reset.
[highlight resetAppearanceStream];
...
js
const FSDK = require("@foxitsoftware/foxit-pdf-sdk-node");
...
// Assuming PDFPage page has been loaded and parsed.
// Assuming the annnots in the page have been loaded.
// Add highlight annotation.
let highlight = new FSDK.Highlight(page.AddAnnot(FSDK.Annot.e_Highlight, new FSDK.RectF(10, 450, 100, 550)));
// This flag is used for printing annotations.
highlight.SetFlags(4);
highlight.SetContent("Highlight");
let quad_points = new FSDK.QuadPoints();
quad_points.first = new FSDK.PointF(10, 500);
quad_points.second = new FSDK.PointF(90, 500);
quad_points.third = new FSDK.PointF(10, 480);
quad_points.fourth = new FSDK.PointF(90, 480);
let quad_points_array = new FSDK.QuadPointsArray();
quad_points_array.Add(quad_points);
highlight.SetQuadPoints(quad_points_array);
highlight.SetSubject("Highlight");
highlight.SetTitle("Foxit SDK");
highlight.SetCreationDateTime(GetLocalDateTime());
highlight.SetModifiedDateTime(GetLocalDateTime());
highlight.SetUniqueID(RandomUID());
// Appearance should be reset.
highlight.ResetAppearanceStream();
csharp
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
using foxit.pdf.graphics;
using foxit.pdf.annots;
using foxit.pdf.actions;
...
// Assuming PDFPage page has been loaded and parsed.
// Add highlight annotation.
Annot annot = page.AddAnnot(Annot.Type.e_Highlight, new RectF(10, 450, 100, 550));
Highlight highlight = new Highlight(annot);
highlight.SetContent("Highlight");
QuadPoints quad_points = new QuadPoints();
quad_points.first = new foxit.common.fxcrt.PointF(10, 500);
quad_points.second = new foxit.common.fxcrt.PointF(90, 500);
quad_points.third = new foxit.common.fxcrt.PointF(10, 480);
quad_points.fourth = new foxit.common.fxcrt.PointF(90, 480);
QuadPointsArray quad_points_array = new QuadPointsArray();
quad_points_array.Add(quad_points);
highlight.SetQuadPoints(quad_points_array);
highlight.SetSubject("Highlight");
highlight.SetTitle("Foxit SDK");
highlight.SetCreationDateTime(GetLocalDateTime());
highlight.SetModifiedDateTime(GetLocalDateTime());
highlight.SetUniqueID(RandomUID());
// Appearance should be reset.
highlight.ResetAppearanceStream();
...
在创建 markup 注释时设置 popup 信息
c++
#include "include/common/fs_common.h"
#include "include/pdf/actions/fs_action.h"
#include "include/pdf/annots/fs_annot.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/objects/fs_pdfobject.h"
#include "include/pdf/fs_pdfpage.h"
using namespace foxit;
using namespace foxit::common;
using namespace pdf;
using namespace annots;
// Assuming PDFPage page has been loaded and parsed.
// Assuming the annnots in the page have been loaded.
// Create a new note annot and set the properties for it.
annots::Note note(page.AddAnnot(Annot::e_Note, RectF(10,350,50,400)));
note.SetIconName("Comment");
note.SetSubject(L"Note");
note.SetTitle(L"Foxit SDK");
note.SetContent(L"Note annotation.");
note.SetCreationDateTime(GetLocalDateTime());
note.SetModifiedDateTime(GetLocalDateTime());
note.SetUniqueID(WString::FromLocal(RandomUID()));
// Create a new popup annot and set it to the new note annot.
Popup popup(page.AddAnnot(Annot::e_Popup, RectF(300,450,500,550)));
popup.SetBorderColor(0x00FF00);
popup.SetOpenStatus(false);
popup.SetModifiedDateTime(GetLocalDateTime());
note.SetPopup(popup);
...
C
#include "include/fs_basictypes_c.h"
#include "include/fs_common_c.h"
#include "include/fs_action_c.h"
#include "include/fs_annot_c.h"
#include "include/fs_pdfdoc_c.h"
#include "include/fs_pdfobject_c.h"
#include "include/fs_pdfpage_c.h"
// Assuming FS_PDFPAGE_HANDLE page has been loaded and parsed.
// Assuming the annnots in the page have been loaded.
// Create a new note annot and set the properties for it.
FSRectF rectf;
rectf.left = 10;
rectf.bottom = 350;
rectf.right = 50;
rectf.top = 400;
FS_ANNOT_HANDLE annot;
FSDK_PDFPage_AddAnnot(page, e_FSNote, rectf, &annot);
FS_NOTEANNOT_HANDLE note;
FSDK_Note_Create0(annot, ¬e);
FSDK_Note_SetIconName(note, "Comment");
FSDK_Markup_SetSubject(note, L"Note");
FSDK_Markup_SetTitle(note, L"Foxit SDK");
FSDK_Annot_SetContent(note, L"Note annotation.");
FSDK_Markup_SetCreationDateTime(note, GetLocalDateTime());
FSDK_Annot_SetModifiedDateTime(note, GetLocalDateTime());
FSDK_Annot_SetUniqueID(note, WRandomUID());
// Create a new popup annot and set it to the new note annot.
rectf.left = 300;
rectf.bottom = 450;
rectf.right = 500;
rectf.top = 550;
FS_ANNOT_HANDLE annot1;
FSDK_PDFPage_AddAnnot(page, e_FSPopup, rectf, &annot1)
FS_POPUPANNOT_HANDLE popup;
FSDK_Popup_Create0(annot1, &popup);
FSDK_Annot_SetBorderColor(popup, 0x00FF00);
FSDK_Popup_SetOpenStatus(popup, false);
FSDK_Annot_SetModifiedDateTime(popup, GetLocalDateTime());
FSDK_Markup_SetPopup(note, popup);
...
java
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.pdf.annots.Note;
import com.foxit.sdk.pdf.annots.Popup;
import com.foxit.sdk.common.fxcrt.RectF;
...
// Assuming PDFPage page has been loaded and parsed.
// Add note annotation
Note note = new Note(page.addAnnot(Annot.e_Note, new RectF(10, 350, 50, 400)));
note.setIconName("Comment");
note.setSubject("Note");
note.setTitle("Foxit SDK");
note.setContent("Note annotation.");
note.setCreationDateTime(GetLocalDateTime());
note.setModifiedDateTime(GetLocalDateTime());
note.setUniqueID(RandomUID());
// Add popup to note annotation
Popup popup = new Popup(page.addAnnot(Annot.e_Popup, new RectF(300, 450, 500, 550)));
popup.setBorderColor(0x00FF00);
popup.setOpenStatus(false);
popup.setModifiedDateTime(GetLocalDateTime());
note.setPopup(popup);
// Appearance should be reset.
note.resetAppearanceStream();
...
py
import sys
import site
if sys.version_info.major == 2:
_PYTHON2_ = True
else:
_PYTHON2_ = False
if _PYTHON2_:
#replace with the python2 lib path
site.addsitedir('../../../')
from FoxitPDFSDKPython2 import *
else:
from FoxitPDFSDKPython3 import *
...
# Assuming PDFPage page has been loaded and parsed.
# Assuming the annnots in the page have been loaded.
# Create a new note annot and set the properties for it.
note = Note(page.AddAnnot(Annot.e_Note, RectF(10,350,50,400)))
note.SetIconName("Comment")
note.SetSubject("Note")
note.SetTitle("Foxit SDK")
note.SetContent("Note annotation.")
note.SetCreationDateTime(GetLocalDateTime())
note.SetModifiedDateTime(GetLocalDateTime())
note.SetUniqueID(RandomUID())
# Create a new popup annot and set it to the new note annot.
popup = Popup(page.AddAnnot(Annot.e_Popup, RectF(300,450,500,550)))
popup.SetBorderColor(0x00FF00)
popup.SetOpenStatus(False)
popup.SetModifiedDateTime(GetLocalDateTime())
note.SetPopup(popup)
objc
#include "FSPDFObjC.h"
...
// Assuming FSPDFPage page has been loaded and parsed.
// Add note annotation
FSRectF* annot_rect = [[FSRectF alloc] initWithLeft1:10 bottom1:350 right1:50 top1:400];
FSNote* note = [[FSNote alloc] initWithAnnot:[page addAnnot:FSAnnotNote rect:annot_rect]];
[note setIconName:@"Comment"];
[note setSubject:@"Note"];
[note setTitle:@"Foxit SDK"];
[note setContent:@"Note annotation."];
FSDateTime* local_time = getLocalDateTime();
[note setCreationDateTime:local_time];
[note setModifiedDateTime:local_time];
[note setUniqueID:randomUID()];
// Add popup to note annotation
annot_rect = [[FSRectF alloc] initWithLeft1:300 bottom1:450 right1:500 top1:550];
FSPopup* popup = [[FSPopup alloc] initWithAnnot:[page addAnnot:FSAnnotPopup rect:annot_rect]];
[popup setBorderColor:0x00FF00];
[popup setOpenStatus:NO];
local_time = getLocalDateTime();
[popup setModifiedDateTime:local_time];
[note setPopup:popup];
[note resetAppearanceStream];
...
js
const FSDK = require("@foxitsoftware/foxit-pdf-sdk-node");
...
// Assuming PDFPage page has been loaded and parsed.
// Assuming the annnots in the page have been loaded.
// Create a new note annot and set the properties for it.
let note = new FSDK.Note(page.AddAnnot(FSDK.Annot.e_Note, new FSDK.RectF(10, 350, 50, 400)));
// This flag is used for printing annotations.
note.SetFlags(4);
note.SetIconName("Comment");
note.SetSubject("Note");
note.SetTitle("Foxit SDK");
note.SetContent("Note annotation.");
note.SetCreationDateTime(GetLocalDateTime());
note.SetModifiedDateTime(GetLocalDateTime());
note.SetUniqueID(RandomUID());
// Add popup to note annotation
let popup = new FSDK.Popup(page.AddAnnot(FSDK.Annot.e_Popup, new FSDK.RectF(300, 450, 500, 550)));
popup.SetBorderColor(0x00FF00);
popup.SetOpenStatus(false);
popup.SetModifiedDateTime(GetLocalDateTime());
note.SetPopup(popup);
csharp
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
using foxit.pdf.graphics;
using foxit.pdf.annots;
using foxit.pdf.actions;
...
// Assuming PDFPage page has been loaded and parsed.
// Assuming the annnots in the page have been loaded.
// Create a new note annot and set the properties for it.
Annot annot = page.AddAnnot(Annot.Type.e_Note, new RectF(10, 350, 50, 400));
Note note = new Note(annot);
note.SetIconName("Comment");
note.SetSubject("Note");
note.SetTitle("Foxit SDK");
note.SetContent("Note annotation.");
note.SetCreationDateTime(GetLocalDateTime());
note.SetModifiedDateTime(GetLocalDateTime());
note.SetUniqueID(RandomUID());
// Add popup to note annotation.
Annot annot_popup = page.AddAnnot(Annot.Type.e_Popup, new RectF(300, 450, 500, 550));
Popup popup = new Popup(annot);
popup.SetBorderColor(0x00FF00);
popup.SetOpenStatus(false);
popup.SetModifiedDateTime(GetLocalDateTime());
note.SetPopup(popup);
// Appearance should be reset.
note.ResetAppearanceStream();
...
使用设备坐标 获取 PDF 中特定的注释
c++
#include "include/common/fs_common.h"
#include "include/pdf/actions/fs_action.h"
#include "include/pdf/annots/fs_annot.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/objects/fs_pdfobject.h"
#include "include/pdf/fs_pdfpage.h"
using namespace foxit;
using namespace foxit::common;
using namespace pdf;
using namespace annots;
// Assuming PDFDoc doc has been loaded.
// Assuming PDFPage page has been loaded and parsed.
...
int width = static_cast<int>(page.GetWidth());
int height = static_cast<int>(page.GetHeight());
// Get page transformation matrix.
Matrix displayMatrix= page.GetDisplayMatrix(0, 0, width, height, page.GetRotation());
int iAnnotCount = page.GetAnnotCount();
for(int i=0; i<iAnnotCount; i++)
{
Annot pAnnot = page.GetAnnot(i);
ASSERT_FALSE(pAnnot.IsEmpty());
if (Annot::e_Popup == pAnnot.GetType()) continue;
RectI annotRect = pAnnot.GetDeviceRect(false, displayMatrix);
PointF pt;
float tolerance = 1.0;
// Get the same annot (pAnnot) using annotRect.
pt.x = annotRect.left + tolerance;
pt.y = (annotRect.top - annotRect.bottom)/2 + annotRect.bottom;
Annot gAnnot = page.GetAnnotAtDevicePoint(pt, tolerance, &displayMatrix);
...
}
C
#include "include/fs_basictypes_c.h"
#include "include/fs_common_c.h"
#include "include/fs_action_c.h"
#include "include/fs_annot_c.h"
#include "include/fs_pdfdoc_c.h"
#include "include/fs_pdfobject_c.h"
#include "include/fs_pdfpage_c.h"
// Assuming FS_PDFDOC_HANDLE doc has been loaded.
// Assuming FS_PDFPAGE_HANDLE page has been loaded and parsed.
...
float fWidth;
float fHeight;
FSDK_PDFPage_GetWidth(page, &fWidth);
FSDK_PDFPage_GetHeight(page, &fHeight);
int width = (int)fWidth;
int height = (int)fHeight;
// Get page transformation matrix.
FSRotation return_Rotation;
FSDK_PDFPage_GetRotation(page, &return_Rotation);
FSMatrix displayMatrix;
FSDK_PDFPage_GetDisplayMatrix(page, 0, 0, width, height, return_Rotation, &displayMatrix);
int iAnnotCount;
FSDK_PDFPage_GetAnnotCount(page, &iAnnotCount);
for(int i=0; i<iAnnotCount; i++)
{
FS_ANNOT_HANDLE pAnnot;
FSDK_PDFPage_GetAnnot(page, i, &pAnnot);
FS_BOOL isEmpty;
FSDK_Annot_IsEmpty(pAnnot, &isEmpty);
ASSERT_FALSE(isEmpty);
FSAnnotType type;
FSDK_Annot_GetType(pAnnot, &type);
if (e_FSPopup == type) continue;
FSRectI annotRect;
FSDK_Annot_GetDeviceRect(pAnnot, displayMatrix, &annotRect);
FSPointF pt;
float tolerance = 1.0;
// Get the same annot (pAnnot) using annotRect.
pt.x = annotRect.left + tolerance;
pt.y = (annotRect.top - annotRect.bottom)/2 + annotRect.bottom;
FS_ANNOT_HANDLE gAnnot;
FSDK_PDFPage_GetAnnotAtDevicePoint(page, pt, tolerance, &displayMatrix, &gAnnot);
...
}
java
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.common.fxcrt.RectF;
import com.foxit.sdk.common.fxcrt.Matrix2D;
import com.foxit.sdk.common.fxcrt.PointF;
import com.foxit.sdk.common.fxcrt. RectI;
import com.foxit.sdk.pdf.annots.Annot;
...
int width = (int)(page.getWidth());
int height = (int)(page.getHeight());
Matrix2D displayMatrix = page.getDisplayMatrix(0, 0, width, height, page.getRotation());
int iAnnotCount = page.getAnnotCount();
for(int i=0; i<iAnnotCount; i++)
{
Annot pAnnot = page.GetAnnot(i);
if (Annot.e_Popup == pAnnot.getType()) continue;
RectI annotRect = pAnnot.getDeviceRect(false, displayMatrix);
float tolerance = 1.0;
float x = annotRect.getLeft() + tolerance;
float y = (annotRect.getTop() - annotRect.getBottom())/2 + annotRect.getBottom();
// Get the same annot (pAnnot) using annotRect.
PointF pt = new PointF(x, y);
Annot gAnnot = page.getAnnotAtDevicePoint(pt, tolerance, displayMatrix);
...
}
...
py
import sys
import site
if sys.version_info.major == 2:
_PYTHON2_ = True
else:
_PYTHON2_ = False
if _PYTHON2_:
#replace with the python2 lib path
site.addsitedir('../../../')
from FoxitPDFSDKPython2 import *
else:
from FoxitPDFSDKPython3 import *
...
# Assuming PDFDoc doc has been loaded.
# Assuming PDFPage page has been loaded and parsed.
...
width = int(page.GetWidth())
height = int(page.GetHeight())
# Get page transformation matrix.
displayMatrix= page.GetDisplayMatrix(0, 0, width, height, page.GetRotation())
iAnnotCount = page.GetAnnotCount()
for i in range(0, iAnnotCount):
pAnnot = page.GetAnnot(i)
if Annot.e_Popup == pAnnot.GetType(): continue
annotRect = pAnnot.GetDeviceRect(False, displayMatrix)
pt = PointF()
float tolerance = 1.0
# Get the same annot (pAnnot) using annotRect.
pt.x = annotRect.left + tolerance
pt.y = (annotRect.top - annotRect.bottom)/2 + annotRect.bottom
gAnnot = page.GetAnnotAtDevicePoint(pt, tolerance, displayMatrix)
...
objc
#include "FSPDFObjC.h"
...
// Assuming FSPDFPage page has been loaded and parsed.
...
int width = (int)[page getWidth];
int height = (int)[page getHeight];
FSMatrix2D* display_matrix = [page getDisplayMatrix:0 top:0 width:width height:height rotate:page.rotation];
int annot_count = [page getAnnotCount];
for (int i=0; i<annot_count; i++) {
FSAnnot* annot = [page getAnnot:i];
FSAnnotType annot_type = [annot getType];
if (FSAnnotPopup == annot_type) continue;
FSRectI* device_rect = [annot getDeviceRect:NO matrix:display_matrix];
// Get the same annot by using device point.
float tolerance = 1.0;
FSPointF* point = [FSPointF alloc];
[point set:device_rect.left+tolerance y:(device_rect.top - device_rect.bottom)/2+device_rect.bottom];
FSAnnot* get_annot = [page getAnnotAtDevicePoint:point tolerance:tolerance matrix:display_matrix];
}
js
const FSDK = require("@foxitsoftware/foxit-pdf-sdk-node");
...
// Assuming PDFDoc doc has been loaded.
// Assuming PDFPage page has been loaded and parsed.
...
let width = page.GetWidth();
let height = page.GetHeight();
// Get page transformation matrix.
let displayMatrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation());
let iAnnotCount = page.GetAnnotCount();
for (var i = 0; i < iAnnotCount; i++) {
let pAnnot = page.GetAnnot(i)
if (FSDK.Annot.e_Popup == pAnnot.GetType()) continue;
let annotRect = pAnnot.GetDeviceRect(False, displayMatrix);
let pt = new FSDK.PointF();
let tolerance = 1.0;
// Get the same annot (pAnnot) using annotRect.
pt.x = annotRect.left + tolerance;
pt.y = (annotRect.top - annotRect.bottom) / 2 + annotRect.bottom;
let gAnnot = page.GetAnnotAtDevicePoint(pt, tolerance, displayMatrix);
}
...
csharp
using foxit;
using foxit.common;
using foxit.pdf;
using foxit.pdf.graphics;
using foxit.pdf.annots;
...
// Assuming PDFDoc doc has been loaded.
// Assuming PDFPage page has been loaded and parsed.
...
int width = (int)page.GetWidth();
int height = (int)page.GetHeight();
Matrix2D matrix = page.GetDisplayMatrix(0, 0, width, height, page.GetRotation());
// Assuming PointF point has been got.
float tolerance = 3.0f;
Annot annot = page.GetAnnotAtDevicePoint(point, tolerance, matrix);
...
提取 text markup annotation 中的文本内容
c++
#include "include/common/fs_common.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/fs_pdfpage.h"
#include "include/pdf/fs_search.h"
using namespace foxit;
using namespace foxit::common;
using namespace pdf;
using namespace annots;
...
// Assuming PDFDoc doc has been loaded.
...
PDFPage page = doc.GetPage(0);
// Parse the first page.
page.StartParse(foxit::pdf::PDFPage::e_ParsePageNormal, NULL, false);
int annot_count = page.GetAnnotCount();
TextPage text_page(page);
for (int i = 0; i < annot_count; i++) {
annots::Annot annot = page.GetAnnot(i);
annots::TextMarkup text_markup(annot);
if (!text_markup.IsEmpty()) {
// Get the texts which intersect with a text markup annotation.
WString text = text_page.GetTextUnderAnnot(text_markup);
}
}
C
#include "include/fs_basictypes_c.h"
#include "include/fs_common_c.h"
#include "include/fs_pdfdoc_c.h"
#include "include/fs_pdfpage_c.h"
#include "include/fs_search_c.h"
...
// Assuming FS_PDFDOC_HANDLE doc has been loaded.
...
FS_PDFPAGE_HANDLE page;
FSDK_PDFDoc_GetPage(doc, 0, &page);
// Parse the first page.
FS_PROGRESSIVE_HANDLE progressive;
FSDK_PDFPage_StartParse(page, e_FSParseFlagsParsePageNormal, NULL, false, &progressive);
int annot_count;
FSDK_PDFPage_GetAnnotCount(page, &annot_count);
FS_TEXTPAGE_HANDLE text_page;
FSDK_TextPage_Create(page, e_FSTextParseFlagsParseTextNormal, &text_page);
for (int i = 0; i < annot_count; i++) {
FS_ANNOT_HANDLE annot;
FSDK_PDFPage_GetAnnot(page, i, &annot);
FS_TEXTMARKUPANNOT_HANDLE text_markup;
FSDK_TextMarkup_Create0(annot, &text_markup);
FS_BOOL return_value;
FSDK_Annot_IsEmpty(text_markup, &return_value);
if (!return_value) {
// Get the texts which intersect with a text markup annotation.
FS_WSTR text;
FSDK_TextPage_GetTextUnderAnnot(text_page, text_markup, &text);
}
}
java
import com.foxit.sdk.PDFException;
import com.foxit.sdk.common.Library;
import com.foxit.sdk.pdf.PDFDoc;
import com.foxit.sdk.pdf.PDFPage;
import com.foxit.sdk.pdf.TextPage;
import com.foxit.sdk.pdf.annots.Annot;
import com.foxit.sdk.pdf.annots.TextMarkup;
import static com.foxit.sdk.common.Constants.e_ErrSuccess;
import static com.foxit.sdk.pdf.PDFPage.e_ParsePageNormal;
// Assuming PDFDoc doc has been loaded.
PDFPage page = doc.getPage(0);
// Parse the first page.
page.startParse(e_ParsePageNormal, null, false);
// Get a TextPage object.
TextPage text_page = new TextPage(page, TextPage.e_ParseTextNormal);
int annot_count = page.getAnnotCount();
for (int i = 0; i < annot_count; i++) {
Annot annot = page.getAnnot(i);
TextMarkup text_markup = new TextMarkup(annot);
if (!text_markup.isEmpty()) {
// Get the texts which intersect with a text markup annotation.
String text = text_page.getTextUnderAnnot(text_markup);
}
}
py
import sys
import site
if sys.version_info.major == 2:
_PYTHON2_ = True
else:
_PYTHON2_ = False
if _PYTHON2_:
#replace with the python2 lib path
site.addsitedir('../../../')
from FoxitPDFSDKPython2 import *
else:
from FoxitPDFSDKPython3 import *
...
# Assuming PDFDoc doc has been loaded.
...
page = doc.GetPage(0)
# Parse the first page.
page.StartParse(PDFPage.e_ParsePageNormal, None, False)
annot_count = page.GetAnnotCount()
text_page = TextPage(page)
for i in range(0, annot_count):
annot = page.GetAnnot(i)
text_markup = TextMarkup(annot)
if not text_markup.IsEmpty():
# Get the texts which intersect with a text markup annotation.
text = text_page.GetTextUnderAnnot(text_markup)
objc
#include "FSPDFObjC.h"
...
// Assuming FSPDFDoc doc has been loaded.
...
FSPDFPage *page = [doc getPage:0];
[page startParse:FSPDFPageParsePageNormal pause:nil is_reparse:NO];
// Get a FSTextPage object.
FSTextPage *textPage = [[FSTextPage alloc] initWithPage:page flags:FSTextPageParseTextNormal];
int annot_count = [page getAnnotCount];
for (int i = 0; i < annot_count; i++)
{
FSAnnot *annot = [page getAnnot:i];
FSTextMarkup *text_markup = [[FSTextMarkup alloc] initWithAnnot:annot];
if (![text_markup isEmpty])
{
// Get the texts which intersect with a text markup annotation.
NSString *text = [textPage getTextUnderAnnot:text_markup];
}
}
js
const FSDK = require("@foxitsoftware/foxit-pdf-sdk-node");
...
// Assuming PDFDoc doc has been loaded.
...
let page = doc.GetPage(0)
// Parse the first page.
page.StartParse(FSDK.PDFPage.e_ParsePageNormal, null, false)
let annot_count = page.GetAnnotCount()
let text_page = news
FSDK.TextPage(page)
for (var i = 0; i < annot_count; i++) {
let annot = page.GetAnnot(i)
let text_markup = TextMarkup(annot)
if (text_markup.IsEmpty()) {
// Get the texts which intersect with a text markup annotation.
text = text_page.GetTextUnderAnnot(text_markup)
}
}
csharp
using foxit.common;
using foxit.pdf;
using foxit.pdf.annots;
...
// Assuming PDFDoc doc has been loaded.
...
using (var page = doc.GetPage(0))
{
// Parse the first page.
page.StartParse((int) PDFPage.ParseFlags.e_ParsePageNormal, null, false);
// Get a TextPage object.
using (var text_page = new TextPage(page, (int) TextPage.TextParseFlags.e_ParseTextNormal))
{
int annot_count = page.GetAnnotCount();
for (int i = 0; i<annot_count; i++)
{
Annot annot = page.GetAnnot(i);
TextMarkup text_markup = new TextMarkup(annot);
if (!text_markup.IsEmpty())
{
// Get the texts which intersect with a text markup annotation.
string text = text_page.GetTextUnderAnnot(text_markup);
}
}
}
}
向 freetext 注释添加 richtext
c++
#include "include/common/fs_common.h"
#include "include/pdf/annots/fs_annot.h"
using namespace foxit;
using namespace foxit::common;
using namespace pdf;
using namespace annots;
...
// Make sure that SDK has already been initialized successfully.
// Load a PDF document, get a PDF page and parse it.
// Add a new freetext annotation, as text box.
annots::FreeText freetext(pdf_page.AddAnnot(Annot::e_FreeText, RectF(50, 50, 150, 100)));
// Set annotation's properties.
// Add/insert richtext string with style.
RichTextStyle richtext_style;
richtext_style.font = Font(L"Times New Roman", 0, Font::e_CharsetANSI, 0);
richtext_style.text_color = 0xFF0000;
richtext_style.text_size = 10;
freetext.AddRichText(L"Textbox annotation ", richtext_style);
richtext_style.text_color = 0x00FF00;
richtext_style.is_underline = true;
freetext.AddRichText(L"1-underline ", richtext_style);
richtext_style.font = Font(L"Calibri", 0, Font::e_CharsetANSI, 0);
richtext_style.text_color = 0x0000FF;
richtext_style.is_underline = false;
richtext_style.is_strikethrough = true;
int richtext_count = freetext.GetRichTextCount();
freetext.InsertRichText(richtext_count - 1, L"2_strikethrough ", richtext_style);
// Appearance should be reset.
freetext.ResetAppearanceStream();
C
#include "include/fs_common_c.h"
#include "include/fs_annot_c.h"
// Make sure that SDK has already been initialized successfully.
// Load a PDF document, get a PDF page and parse it.
// Add a new freetext annotation, as text box.
FSRectF rectf;
rectf.left = 50;
rectf.bottom = 50;
rectf.right = 150;
rectf.top = 100;
FS_ANNOT_HANDLE annot;
FSDK_PDFPage_AddAnnot(pdf_page, e_FSFreeText, rectf, &annot);
FS_ANNOT_HANDLE freetext;
FSDK_FreeText_Create0(annot, &freetext);
// Set annotation's properties.
// Add/insert richtext string with style.
FSRichTextStyle richtext_style;
FS_FONT_HANDLE font0;
FSDK_Font_Create(L"Times New Roman", 0, e_FSCharsetANSI, 0, &font0);
richtext_style.font = font0;
richtext_style.text_color = 0xFF0000;
richtext_style.text_size = 10;
richtext_style.text_alignment = e_FSAlignmentLeft;
richtext_style.mark_style = e_FSCornerMarkNone;
richtext_style.is_bold = 0;
richtext_style.is_italic = 0;
richtext_style.is_strikethrough = 0;
richtext_style.is_underline = 0;
FSDK_Markup_AddRichText(freetext, L"Textbox annotation ", richtext_style);
richtext_style.text_color = 0x00FF00;
richtext_style.is_underline = 1;
FSDK_Markup_AddRichText(freetext, L"1-underline ", richtext_style);
FS_FONT_HANDLE font1;
FSDK_Font_Create(L"Calibri", 0, e_FSCharsetANSI, 0, &font1);
richtext_style.font = font1;
richtext_style.text_color = 0x0000FF;
richtext_style.is_underline = 0;
richtext_style.is_strikethrough = 1;
int richtext_count = 0;
FSDK_Markup_GetRichTextCount(freetext, &richtext_count);
FSDK_Markup_InsertRichText(freetext, richtext_count - 1, L"2_strikethrough ", richtext_style);
// Appearance should be reset.
FS_BOOL result = 0;
FSDK_Annot_ResetAppearanceStream(freetext, &result);
FSDK_Annot_Release(freetext);
java
import com.foxit.sdk.common.Library;
import com.foxit.sdk.common.Font;
import com.foxit.sdk.pdf.annots.FreeText;
import com.foxit.sdk.pdf.annots.RichTextStyle;
// Make sure that SDK has already been initialized successfully.
// Load a PDF document, get a PDF page and parse it.
// Add a new freetext annotation, as text box.
Annot annot = pdf_page.addAnnot(Annot.e_FreeText, new RectF(50, 50, 150, 100));
FreeText freetext = new FreeText(annot);
// Set annotation's properties.
// Add/insert richtext string with style.
RichTextStyle richtext_style = new RichTextStyle();
richtext_style.setFont(new Font("Times New Roman", 0, Font.e_CharsetANSI, 0));
richtext_style.setText_color(0xFF0000);
richtext_style.setText_size(10);
freetext.addRichText("Textbox annotation ", richtext_style);
richtext_style.setText_color(0x00FF00);
richtext_style.setIs_underline(true);
freetext.addRichText("1-underline ", richtext_style);
richtext_style.setFont(new Font("Calibri", 0, Font.e_CharsetANSI, 0));
richtext_style.setText_color(0x0000FF);
richtext_style.setIs_underline(false);
richtext_style.setIs_strikethrough(true);
int richtext_count = freetext.getRichTextCount();
freetext.insertRichText(richtext_count-1, "2_strikethrough ", richtext_style);
// Appearance should be reset.
freetext.resetAppearanceStream();
py
import sys
import site
if sys.version_info.major == 2:
_PYTHON2_ = True
else:
_PYTHON2_ = False
if _PYTHON2_:
#replace with the python2 lib path
site.addsitedir('../../../')
from FoxitPDFSDKPython2 import *
else:
from FoxitPDFSDKPython3 import *
...
# Make sure that SDK has already been initialized successfully.
# Load a PDF document, get a PDF page and parse it.
# Add a new freetext annotation, as text box.
freetext = FreeText(pdf_page.AddAnnot(Annot.e_FreeText, RectF(50, 50, 150, 100)))
# Set annotation's properties.
# Add/insert richtext string with style.
richtext_style = RichTextStyle()
richtext_style.font = Font("Times New Roman", 0, Font.e_CharsetANSI, 0)
richtext_style.text_color = 0xFF0000
richtext_style.text_size = 10
freetext.AddRichText("Textbox annotation ", richtext_style)
richtext_style.text_color = 0x00FF00
richtext_style.is_underline = True
freetext.AddRichText("1-underline ", richtext_style)
richtext_style.font = Font("Calibri", 0, Font.e_CharsetANSI, 0)
richtext_style.text_color = 0x0000FF
richtext_style.is_underline = False
richtext_style.is_strikethrough = True
richtext_count = freetext.GetRichTextCount()
freetext.InsertRichText(richtext_count - 1, "2_strikethrough ", richtext_style)
# Appearance should be reset.
freetext.ResetAppearanceStream()
objc
#include "FSPDFObjC.h"
// Make sure that SDK has already been initialized successfully.
// Load a PDF document, get a PDF page and parse it.
// Add a new freetext annotation, as text box.
FSRectF* annot_rect = [[FSRectF alloc] initWithLeft1:50 bottom1:50 right1:150 top1:100];
FSFreeText* freetext = [[FSFreeText alloc] initWithAnnot:[pdf_page addAnnot:FSAnnotFreeText rect:annot_rect]];
// Set annotation's properties.
// Add/insert richtext string with style.
FSRichTextStyle* richtext_style = [[FSRichTextStyle alloc] init];
FSFont* rcstyle_font_1 = [[FSFont alloc] initWithName:@"Times New Roman" styles:0 charset:FSFontCharsetANSI weight:0];
[richtext_style setFont:rcstyle_font_1];
[richtext_style setText_color:0xFF0000];
[richtext_style setText_size:10];
[freetext addRichText:@"Textbox annotation " style:richtext_style];
[richtext_style setText_color:0x00FF00];
[richtext_style setIs_underline:YES];
[freetext addRichText:@"1-underline " style:richtext_style];
[richtext_style setIs_underline:NO];
[richtext_style setText_color:0x0000FF];
[richtext_style setIs_strikethrough:YES];
FSFont* rcstyle_font_2 = [[FSFont alloc] initWithName:@"Calibri" styles:0 charset:FSFontCharsetANSI weight:0];
[richtext_style setFont:rcstyle_font_2];
int rc_count = [freetext getRichTextCount];
[freetext insertRichText:(rc_count-1) content:@"2_strikethrough " style:richtext_style];
// Appearance should be reset.
[freetext resetAppearanceStream];
js
const FSDK = require("@foxitsoftware/foxit-pdf-sdk-node");
...
// Make sure that SDK has already been initialized successfully.
// Load a PDF document, get a PDF page and parse it.
// Add a new freetext annotation, as text box.
let freetext = new FSDK.FreeText(page.AddAnnot(FSDK.Annot.e_FreeText, new FSDK.RectF(450, 50, 550, 100)));
// Set annotation's properties.
// Add/insert richtext string with style.
richtext_style = new FSDK.RichTextStyle();
richtext_style.font = new FSDK.Font("Times New Roman", 0, FSDK.Font.e_CharsetANSI, 0);
richtext_style.text_color = 0xFF0000;
richtext_style.text_size = 10;
freetext.AddRichText("Textbox annotation ", richtext_style);
richtext_style.text_color = 0x00FF00;
richtext_style.is_underline = true;
freetext.AddRichText("1-underline ", richtext_style);
richtext_style.font = new FSDK.Font("Calibri", 0, FSDK.Font.e_CharsetANSI, 0);
richtext_style.text_color = 0x0000FF;
richtext_style.is_underline = false;
richtext_style.is_strikethrough = true;
richtext_count = freetext.GetRichTextCount();
freetext.InsertRichText(richtext_count - 1, "2_strikethrough ", richtext_style);
// Appearance should be reset.
ffreetext.ResetAppearanceStream();
csharp
using foxit.common;
using foxit.pdf;
using foxit.pdf.annots;
// Make sure that SDK has already been initialized successfully.
// Load a PDF document, get a PDF page and parse it.
// Add a new freetext annotation, as text box.
FreeText freetext = null;
Annot annot = null;
using (annot = pdf_page.AddAnnot(Annot.Type.e_FreeText, new RectF(50, 50, 150, 100)))
using (freetext = new FreeText(annot))
{
// Set annotation's properties.
// Add/insert richtext string with style.
using (RichTextStyle richtext_style = new RichTextStyle())
{
{
String font_name = "Times New Roman";
using (richtext_style.font = new foxit.common.Font(font_name, 0, foxit.common.Font.Charset.e_CharsetANSI, 0))
{
richtext_style.text_color = 0xFF0000;
richtext_style.text_size = 10;
freetext.AddRichText("Textbox annotation ", richtext_style);
richtext_style.text_color = 0x00FF00;
richtext_style.is_underline = true;
freetext.AddRichText("1-underline ", richtext_style);
}
}
{
String font_name = "Calibri";
using (richtext_style.font = new foxit.common.Font(font_name, 0, foxit.common.Font.Charset.e_CharsetANSI, 0))
{
richtext_style.text_color = 0x0000FF;
richtext_style.is_underline = false;
richtext_style.is_strikethrough = true;
int richtext_count = freetext.GetRichTextCount();
freetext.InsertRichText(richtext_count - 1, "2_strikethrough ", richtext_style);
}
}
}
// Appearance should be reset.
freetext.resetAppearanceStream();
}
从 FDF 文件导入注释或者将注释导出到 FDF 文件
在 福昕 PDF SDK 中,可以使用来自应用程序或者 FDF 文件的数据来创建注释。同时,PDF SDK 支持将注释导出到 FDF 文件。
从 FDF 文件导入注释,并将其添加到 PDF 文档的首页
c++
#include "include/common/fs_common.h"
#include "include/pdf/actions/fs_action.h"
#include "include/pdf/annots/fs_annot.h"
#include "include/pdf/fs_pdfdoc.h"
#include "include/pdf/objects/fs_pdfobject.h"
#include "include/pdf/fs_pdfpage.h"
using namespace std;
using namespace foxit;
using namespace foxit::common;
using foxit::common::Library;
using namespace pdf;
using namespace annots;
// Assuming PDFDoc doc has been loaded.
...
FILE* file = NULL;
#if defined(_WIN32) || defined(_WIN64)
fopen_s(&file, (const char*)(const char*)String::FromUnicode(fdf_file), "rb+");
#else
file = fopen((const char*)(const char*)String::FromUnicode(fdf_file), "rb+");
#endif
fseek(file, 0, SEEK_END);
size_t file_size = (size_t)ftell(file);
char* buffer = (char*)malloc(file_size * sizeof(char));
memset(buffer, 0 , file_size);
fseek(file, 0, SEEK_SET);
fread(buffer, sizeof(char), file_size, file);
fclose(file);
fdf::FDFDoc fdf_doc(buffer, file_size);
pdf_doc.ImportFromFDF(fdf_doc, PDFDoc::e_Annots);
C
#include "include/fs_basictypes_c.h"
#include "include/fs_common_c.h"
#include "include/fs_action_c.h"
#include "include/fs_annot_c.h"
#include "include/fs_pdfdoc_c.h"
#include "include/fs_pdfobject_c.h"
#include "include/fs_pdfpage_c.h"
char* wstring2string(const wchar_t *source, size_t source_size, char *dest, size_t dest_size) {
char* curLocale = setlocale(LC_ALL, NULL);
setlocale(LC_ALL, "chs");
memset(dest, 0, dest_size);
wcstombs(dest, source, dest_size);
setlocale(LC_ALL, "C");
return dest;
}
...
// Assuming FS_PDFDOC_HANDLE doc has been loaded.
...
FILE* file = NULL;
_wfopen_s (&file, fdf_file, "rb+");
fseek(file, 0, SEEK_END);
size_t file_size = (size_t)ftell(file);
char* buffer = (char*)malloc(file_size * sizeof(char));
memset(buffer, 0 , file_size);
fseek(file, 0, SEEK_SET);
fread(buffer, sizeof(char), file_size, file);
fclose(file);
FS_FDFDOC_HANDLE fdf_doc;
FSDK_FDFDoc_Create2(buffer, file_size, &fdf_doc);
FS_RANGE_HANDLE page_range;
FSDK_Range_Create(&page_range);
FS_BOOL return_result;
FSDK_PDFDoc_ImportFromFDF(pdf_doc, fdf_doc, e_FSAnnots, page_range, &return_result);
java
import com.foxit.sdk.common.Range;
import com.foxit.sdk.fdf.FDFDoc;
import com.foxit.sdk.pdf.PDFDoc;
import static com.foxit.sdk.pdf.PDFDoc.e_Annots;
// Assuming PDFDoc doc has been loaded.
...
Range empty_range = new Range();
{
String input_file = input_path + "AboutFoxit.pdf";
String fdf_file = input_path + "AnnotationData.fdf";
PDFDoc pdf_doc = new PDFDoc(input_file);
error_code = pdf_doc.load(null);
if (error_code != e_ErrSuccess) {
System.out.println("The Doc " + input_file + " Error: " + error_code);
return;
}
FDFDoc fdf_doc = new FDFDoc(fdf_file);
pdf_doc.importFromFDF(fdf_doc, e_Annots, empty_range);
}
...
py
import sys
import site
import os
if sys.version_info.major == 2:
_PYTHON2_ = True
else:
_PYTHON2_ = False
if _PYTHON2_:
#replace with the python2 lib path
site.addsitedir('../../../')
from FoxitPDFSDKPython2 import *
else:
from FoxitPDFSDKPython3 import *
file = open(input_file, "rb+")
buffer = file.read()
file_size = len(buffer)
fdf_doc = FDFDoc(buffer, file_size)
pdf_doc.ImportFromFDF(fdf_doc, PDFDoc.e_Annots)
objc
#include "FSPDFObjC.h"
...
// Assuming FSPDFDoc doc has been loaded.
...
FSFDFDoc* fdf_doc = [[FSFDFDoc alloc] initWithPath: @"AnnotationData.fdf"];
[doc importFromFDF:fdf_doc types:FSPDFDocAnnots page_range:[FSRange new]];
...
js
const FSDK = require("@foxitsoftware/foxit-pdf-sdk-node");
const fs = require('fs');
var buffer = fs.readFileSync(fdf_file);
const fdf_doc = new FSDK.FDFDoc(buffer, Buffer.byteLength(buffer));
pdf_doc.ImportFromFDF(fdf_doc, FSDK.PDFDoc.e_Annots)
csharp
using foxit;
using foxit.common;
using foxit.common.fxcrt;
using foxit.pdf;
// Assuming PDFDoc doc has been loaded.
...
string fdf_file = "The FDF file path";
foxit.fdf.FDFDoc fdf_doc = new foxit.fdf.FDFDoc(fdf_file);
pdf_doc.ImportFromFDF(fdf_doc, (int)foxit.pdf.PDFDoc.DataType.e_Annots, new Range());
...