OFD
OFD(Open Fixed-layout Document),是一种由中国国家制定的开放版式文档标准,主要用于电子公文、档案存储与交换等场景。它旨在提供一种不依赖于特定软件或硬件的文档格式,确保文档在不同设备和平台间显示效果的一致性。
OFD 文件不仅包含结构化数据,还支持丰富的图形元素,能够精确定义文档的布局和内容,包括文本、图片、矢量图形、注释等。XML 格式的特性使其易于理解、处理和渲染,极大地提升了文档的互操作性。
OFD 文件在文档完整性、安全性和互操作性方面具有显著优势:
- 文档真实性: 支持数字签名,确保文档来源可靠。
- 信息安全性: 支持加密,保护敏感信息免受未经授权的访问。
- 交互性: 支持表单字段和数字签名等交互功能,增强用户体验。
处理 OFD 文件需要使用支持 OFD 标准的查看器或编辑器软件。这些工具能够实现 OFD 文档的显示、编辑、转换和打印等操作。
总的来说,OFD 作为一种国产化的文档格式标准,在政府机关、企事业单位中得到了广泛应用,尤其是在需要长期保存和法律效力保障的正式文件方面,OFD 提供了比传统PDF更适合中国国情的选择。此外,随着技术的发展,OFD 也在不断地完善和进化,以满足日益增长的数字化办公需求。
为快速上手,开发者可查阅 OFD 示例配置与运行指南,了解配置步骤和运行示例,快速掌握该功能的使用方法。
实现 OFD 文件与 PDF 文件之间的转换
::: tabs key:c++cjavapythonjsc#
== C++
c++
#include "include/common/fs_common.h"
#include "include/addon/conversion/fs_convert.h"
using namespace foxit::common;
// Initialize OFD engine.
Library::InitializeOFDEngine(engine_path);
WString src_ofd_path = input_path + L"wm_txttiled.ofd";
WString src_pdf_path = input_path + L"test.pdf";
// Convert PDF document to OFD document, and convert OFD document to PDF document.
{
foxit::addon::conversion::OFDConvertParam convert_param;
// Convert OFD document to PDF document.
foxit::addon::conversion::Convert::FromOFD(src_ofd_path, L"", output_directory + L"ofd2pdf.pdf", convert_param);
// Convert PDF document to OFD document.
foxit::addon::conversion::Convert::ToOFD(src_pdf_path, L"", output_directory + L"pdf2ofd.ofd", convert_param);
}
Library::ReleaseOFDEngine();
== C
C
#include "include/fs_common_c.h"
#include "include/fs_convert_c.h"
…
// Initialize OFD engine.
FSDK_Library_InitializeOFDEngine(engine_path);
wchar_t src_ofd_path[MAX_FILE_PATH] = { 0 };
swprintf_s(src_ofd_path, MAX_FILE_PATH, L"%swm_txttiled.ofd", input_path);
wchar_t src_pdf_path[MAX_FILE_PATH] = { 0 };
swprintf_s(src_pdf_path, MAX_FILE_PATH, L"%stest.pdf", input_path);
wchar_t pdf2ofd_path[MAX_FILE_PATH] = { 0 };
swprintf_s(pdf2ofd_path, MAX_FILE_PATH, L"%spdf2ofd.ofd", output_directory);
wchar_t ofd2pdf_path[MAX_FILE_PATH] = { 0 };
swprintf_s(ofd2pdf_path, MAX_FILE_PATH, L"%sofd2pdf.pdf", output_directory);
FSOFDConvertParam convert_param;
convert_param.is_embed_font = FALSE;
// Convert OFD document to PDF document.
FS_BOOL ret = FALSE;
FSDK_Convert_FromOFD(src_ofd_path, L"", ofd2pdf_path, convert_param, &ret);
// Convert PDF document to OFD document.
FSDK_Convert_ToOFD(src_pdf_path, L"", pdf2ofd_path, convert_param, &ret);
// Release OFD engine.
FSDK_Library_ReleaseOFDEngine();
== java
java
import com.foxit.sdk.common.Library;
import com.foxit.sdk.addon.conversion.OFDConvertParam;
import com.foxit.sdk.addon.conversion.Convert;
...
// Initialize OFD engine.
Library.initializeOFDEngine(engine_path);
static String src_ofd_path = input_path + "wm_txttiled.ofd";
static String src_pdf_path = input_path + "test.pdf";
// Convert PDF document to OFD document, and convert OFD document to PDF document.
{
OFDConvertParam convert_param = new OFDConvertParam(false);
// Convert OFD document to PDF document.
Convert.fromOFD(src_ofd_path, "", output_path + "ofd2pdf.pdf", convert_param);
// Convert PDF document to OFD document.
Convert.toOFD(src_pdf_path, "", output_path + "pdf2ofd.ofd", convert_param);
}
// Release OFD engine.
Library.releaseOFDEngine();
== python
py
import sys
import site
# Initialize OFD engine.
Library.InitializeOFDEngine(engine_path)
src_ofd_path = input_path + "wm_txttiled.ofd"
src_pdf_path = input_path + "test.pdf"
# Convert PDF document to OFD document, and convert OFD document to PDF document.
convert_param = OFDConvertParam(False)
# Convert OFD document to PDF document.
Convert.FromOFD(src_ofd_path, "", output_directory + "ofd2pdf.pdf", convert_param)
# Convert PDF document to OFD document.
Convert.ToOFD(src_pdf_path, "", output_directory + "pdf2ofd.ofd", convert_param)
# Release OFD engine.
Library.ReleaseOFDEngine()
== js
js
const FSDK = require("@foxitsoftware/foxit-pdf-sdk-node");
// Initialize OFD engine.
FSDK.Library.InitializeOFDEngine(ofd_resource_path);
let src_ofd_path = input_path + "wm_txttiled.ofd";
let src_pdf_path = input_path + "test.pdf";
// Convert PDF document to OFD document, and convert OFD document to PDF document.
let convert_param = new FSDK.OFDConvertParam(false);
// Convert OFD document to PDF document.
FSDK.Convert.FromOFD(src_ofd_path, "", output_directory + "ofd2pdf.pdf", convert_param);
// Convert PDF document to OFD document.
FSDK.Convert.ToOFD(src_pdf_path, "", output_directory + "pdf2ofd.ofd", convert_param);
// Release OFD engine.
FSDK.Library.ReleaseOFDEngine();
== c#
csharp
using foxit.addon.conversion;
using foxit.addon.ofd;
...
// Initialize OFD engine.
Library.InitializeOFDEngine(engine_path);
String src_ofd_path = input_path + "wm_txttiled.ofd";
String src_pdf_path = input_path + "test.pdf";
using (OFDConvertParam convert_param = new OFDConvertParam())
{ // Convert OFD document to PDF document.
foxit.addon.conversion.Convert.FromOFD(src_ofd_path, "", output_path + "ofd2pdf.pdf", convert_param);
// Convert PDF document to OFD document.
foxit.addon.conversion.Convert.ToOFD(src_pdf_path, "", output_path + "pdf2ofd.ofd", convert_param);
}
// Release OFD engine.
Library.ReleaseOFDEngine();
:::
渲染 OFD 文档页面
::: tabs key:c++cjavapythonjsc#
== C++
c++
#include "include/common/fs_common.h"
#include "include/addon/ofd/fs_ofddoc.h"
#include "include/addon/ofd/fs_ofdpage.h"
#include "include/addon/ofd/fs_ofdrenderer.h"
using namespace std;
using namespace foxit;
using namespace foxit::common;
using namespace pdf;
using namespace addon::ofd;
// Initialize OFD engine.
Library::InitializeOFDEngine(engine_path);
// Render OFD document to bitmap.
{
WString render_file_path = input_path + L"content_flag.ofd";
OFDDoc doc(render_file_path, L"");
OFDPage ofd_page = doc.GetPage(0);
// Get the size of the page.
float w = ofd_page.GetWidth();
float h = ofd_page.GetHeight();
Bitmap bitmap = Bitmap(w, h, Bitmap::e_DIBArgb, NULL, 0);
RectI fRect(0, h, w, 0);
bitmap.FillRect(0xFFFFFFFF, &fRect);
// Get the display matrix of the page.
Matrix matrix_1 = ofd_page.GetDisplayMatrix(0, 0, w, h, CommonDefines::e_Rotation0);
OFDRenderer ofd_render(bitmap);
Progressive progressive = ofd_render.StartRender(ofd_page, matrix_1);
WString sSaveFilePath = output_directory + L"renderBitmap.bmp";
// Add the bitmap to image and save the image.
Image image;
image.AddFrame(bitmap);
image.SaveAs(sSaveFilePath);
ofd_render.Release();
ofd_page.Release();
doc.Release();
}
Library::ReleaseOFDEngine();
== C
C
#include "include/fs_common_c.h"
#include "include/fs_ofddoc_c.h"
#include "include/fs_ofdpage_c.h"
#include "include/fs_ofdrenderer_c.h"
// Initialize OFD engine.
FSDK_Library_InitializeOFDEngine(engine_path);
// Render OFD document to bitmap.
{
wchar_t render_file_path[MAX_FILE_PATH] = { 0 };
swprintf_s(render_file_path, MAX_FILE_PATH, L"%scontent_flag.ofd", input_path);
FS_OFDDOC_HANDLE doc;
FSDK_OFDDoc_Create0(render_file_path, L"", &doc);
FS_OFDPAGE_HANDLE ofd_page;
FSDK_OFDDoc_GetPage(doc, 0, &ofd_page);
// Get the size of the page.
float w = 0;
FSDK_OFDPage_GetWidth(ofd_page, &w);
float h = 0;
FSDK_OFDPage_GetHeight(ofd_page, &h);
FS_BITMAP_HANDLE bitmap;
FSDK_Bitmap_Create(w, h, e_FSDIBArgb, NULL, 0, &bitmap);
FSRectI fRect;
fRect.left = 0;
fRect.top = h;
fRect.right = w;
fRect.bottom = 0;
FSDK_Bitmap_FillRect(bitmap, 0xFFFFFFFF, &fRect);
// Get the display matrix of the page.
FSMatrix matrix_1;
FSDK_OFDPage_GetDisplayMatrix(ofd_page, 0, 0, w, h, e_FSRotation0, &matrix_1);
FS_OFDRENDERER_HANDLE ofd_render;
FSDK_OFDRenderer_Create0(bitmap, &ofd_render);
FS_PROGRESSIVE_HANDLE progressive;
FSDK_OFDRenderer_StartRender(ofd_render, ofd_page, matrix_1, &progressive);
Save2Image(bitmap);
// Add the bitmap to image and save the image.
FS_IMAGE_HANDLE image;
FSDK_Image_Create(&image);
FS_BOOL ret;
FSDK_Image_AddFrame(image, bitmap, &ret);
wchar_t sSaveFilePath[MAX_FILE_PATH] = { 0 };
swprintf_s(sSaveFilePath, MAX_FILE_PATH, L"%srenderBitmap.bmp", output_directory);
FSDK_Image_SaveAs(image, sSaveFilePath, &ret);
FSDK_OFDRenderer_Release(ofd_render);
FSDK_OFDPage_Release(ofd_page);
FSDK_OFDDoc_Release(doc);
}
// Release OFD engine.
FSDK_Library_ReleaseOFDEngine();
== java
java
import com.foxit.sdk.common.Image;
import com.foxit.sdk.common.Library;
import com.foxit.sdk.common.Bitmap;
import com.foxit.sdk.addon.ofd.*;
import com.foxit.sdk.common.Progressive;
import com.foxit.sdk.common.fxcrt.Matrix2D;
import com.foxit.sdk.common.fxcrt.RectI;
...
// Initialize OFD engine.
Library.initializeOFDEngine(engine_path);
// Render OFD document to bitmap.
{
String render_file_path = input_path + "content_flag.ofd";
OFDDoc doc = new OFDDoc(render_file_path, "");
OFDPage ofd_page = doc.getPage(0);
// Get the size of the page.
float w = ofd_page.getWidth();
float h = ofd_page.getHeight();
Bitmap bitmap = new Bitmap((int)w, (int)h, Bitmap.e_DIBArgb, null, 0);
RectI fRect = new RectI(0, (int)h, (int)w, 0);
bitmap.fillRect(0xFFFFFFFF, fRect);
// Get the display matrix of the page.
Matrix2D matrix_1 = ofd_page.getDisplayMatrix(0, 0, (int)w, (int)h, e_Rotation0);
OFDRenderer ofd_render = new OFDRenderer(bitmap);
Progressive progressive = ofd_render.startRender(ofd_page, matrix_1);
String sSaveFilePath = output_path + "renderBitmap.bmp";
// Add the bitmap to image and save the image.
Image image = new Image();
image.addFrame(bitmap);
image.saveAs(sSaveFilePath);
ofd_page.release();
doc.release();
}
// Release OFD engine.
Library.releaseOFDEngine();
== python
py
import sys
import site
# Initialize OFD engine.
Library.InitializeOFDEngine(engine_path)
# Render OFD document to bitmap.
render_file_path = input_path + "content_flag.ofd"
doc = OFDDoc(render_file_path, "")
ofd_page = doc.GetPage(0)
# Get the size of the page.
w = int(ofd_page.GetWidth())
h = int(ofd_page.GetHeight())
bitmap = Bitmap(w, h, Bitmap.e_DIBArgb)
fRect = RectI(0, h, w, 0)
bitmap.FillRect(0xFFFFFFFF, fRect)
# Get the display matrix of the page.
matrix_1 = ofd_page.GetDisplayMatrix(0, 0, w, h, e_Rotation0)
ofd_render = OFDRenderer(bitmap)
progressive = ofd_render.StartRender(ofd_page, matrix_1)
sSaveFilePath = output_directory + "renderBitmap.bmp"
# Add the bitmap to image and save the image.
image = Image()
image.AddFrame(bitmap)
image.SaveAs(sSaveFilePath)
ofd_render.Release()
ofd_page.Release()
doc.Release()
# Release OFD engine.
Library.ReleaseOFDEngine()
== js
js
const FSDK = require("@foxitsoftware/foxit-pdf-sdk-node");
// Initialize OFD engine.
FSDK.Library.InitializeOFDEngine(ofd_resource_path);
// Render OFD document to bitmap.
let render_file_path = input_path + "content_flag.ofd";
let doc = new FSDK.OFDDoc(render_file_path, "");
let ofd_page = doc.GetPage(0);
// Get the size of the page.
let w = ofd_page.GetWidth();
let h = ofd_page.GetHeight();
let bitmap = new FSDK.Bitmap(w, h, FSDK.Bitmap.e_DIBArgb, null, 0);
let fRect = new FSDK.RectI(0, h, w, 0);
bitmap.FillRect(0xFFFFFFFF, fRect);
// Get the display matrix of the page.
let matrix_1 = ofd_page.GetDisplayMatrix(0, 0, w, h, FSDK.e_Rotation0);
let ofd_render = new FSDK.OFDRenderer(bitmap);
let progressive = ofd_render.StartRender(ofd_page, matrix_1);
let sSaveFilePath = output_directory + "renderBitmap.bmp";
// Add the bitmap to image and save the image.
let image = new FSDK.Image();
image.AddFrame(bitmap);
image.SaveAs(sSaveFilePath);
ofd_page.Release();
doc.Release();
== c#
csharp
using foxit.common;
using foxit.addon.ofd;
using foxit.common.fxcrt;
...
// Initialize OFD engine.
Library.InitializeOFDEngine(engine_path);
// Render OFD document to bitmap.
string render_file_path = input_path + "content_flag.ofd";
using (OFDDoc doc = new OFDDoc(render_file_path, ""))
{
using (OFDPage ofd_page = doc.GetPage(0))
{
// Get the size of the page.
float w = ofd_page.GetWidth();
float h = ofd_page.GetHeight();
using (Bitmap bitmap = new Bitmap((int)w, (int)h, Bitmap.DIBFormat.e_DIBArgb, System.IntPtr.Zero, 0))
{
// Get the display matrix of the page.
Matrix2D matrix_1 = ofd_page.GetDisplayMatrix(0, 0, (int)w, (int)h, Rotation.e_Rotation0);
OFDRenderer ofd_render = new OFDRenderer(bitmap);
Progressive progressive = ofd_render.StartRender(ofd_page, matrix_1);
string save_name = output_path + "renderBitmap.bmp";
// Add the bitmap to image and save the image.
foxit.common.Image image = new foxit.common.Image();
image.AddFrame(bitmap);
image.SaveAs(save_name);
System.GC.Collect();
ofd_render.Release();
ofd_page.Release();
doc.Release();
}
}
}
// Release OFD engine.
Library.ReleaseOFDEngine();
:::