All formats

Ofdrw.Net

LanguageC# (.NET Standard 2.0 / 2.1)
LicenseMIT
Repositorygithub.com/whynpc9/ofdrw.net
Documentationwhynpc9.github.io/ofdrw.net (Chinese)
Depends onmanaged packages per module; optional Skia and HarfBuzz extensions
Read / writeyes / yes

Ofdrw.Net reads, creates, edits and converts OFD documents in .NET. It follows the module design of the Java ofdrw but is a separate implementation. It converts OFD to PDF, PNG, JPEG, SVG and HTML, imports PDF, images, DOCX and plain text into OFD, extracts text, finds and replaces text, and checks signature integrity. The API is async and works with streams.

The project is new (version 0.2.0) with few users yet, and the documentation is in Chinese. Check the known issues page before relying on it.

Installation#

Shell
dotnet add package Ofdrw.Net.Converter   # PDF, image, SVG, DOCX conversion (pulls in Reader)
dotnet add package Ofdrw.Net.Reader      # reading and text extraction only
dotnet add package Ofdrw.Net.Layout
dotnet add package Ofdrw.Net.Packaging   # creating documents
dotnet tool install --global Ofdrw.Net.Cli   # `ofdrw` command, needs .NET 10

Basic usage: read an OFD file and extract text#

Page indexes start at 0:

C#
using Ofdrw.Net.Reader.Extraction;
using Ofdrw.Net.Reader.Readers;

await using var input = File.OpenRead("invoice.ofd");
var package = await new OfdReader().ReadAsync(input);

Console.WriteLine($"{package.Pages.Count} pages");
var pages = new OfdTextExtractor().ExtractPages(package, includeTemplates: true);
for (var i = 0; i < pages.Count; i++)
{
    Console.WriteLine($"--- page {i + 1} ---");
    Console.WriteLine(pages[i]);
}

includeTemplates: true also returns text from template pages (shared backgrounds), which invoices often use for fixed labels.

How to convert OFD to PDF#

C#
using Ofdrw.Net.Converter.Pdf.Converters;

await using var input = File.OpenRead("invoice.ofd");
await using var output = File.Create("invoice.pdf");
await new OfdToPdfConverter().ConvertAsync(input, output);
// export selected pages: ConvertAsync(input, output, new[] { 0, 2 })

ConvertWithResultAsync returns diagnostics, for example about seals that were drawn only as images.

How to convert an OFD page to PNG#

C#
using Ofdrw.Net.Converter.Pdf;
using Ofdrw.Net.Converter.Pdf.Converters;

var options = new OfdToImageOptions
{
    PixelsPerMillimeter = 144d / 25.4d,   // 144 dpi
    Format = OfdImageFormat.Png
};

await using var input = File.OpenRead("invoice.ofd");
await using var output = File.Create("page-1.png");
await new OfdToImageConverter(options).ConvertAsync(input, output, pageIndex: 0);

How to convert PDF to OFD#

C#
using Ofdrw.Net.Converter.Pdf.Converters;

await using var input = File.OpenRead("input.pdf");
await using var output = File.Create("output.ofd");
await new PdfToOfdConverter().ConvertAsync(input, output);

How to create an OFD document#

C#
using Ofdrw.Net.Layout;
using Ofdrw.Net.Packaging;

var document = new FlowDocument();
document.Blocks.Add(new Paragraph("你好,Ofdrw.Net"));

await using var output = File.Create("hello.ofd");
await new OfdPackageWriter().WriteAsync(document.Render(), output);

How to create OFD from images#

C#
using Ofdrw.Net.Converter.Pdf;
using Ofdrw.Net.Converter.Pdf.Converters;
using Ofdrw.Net.Core.Models;

var options = new ImageToOfdOptions
{
    PageSize = new OfdPageSize { WidthMillimeters = 210, HeightMillimeters = 297 }
};

await using var first = File.OpenRead("page-1.png");
await using var second = File.OpenRead("page-2.jpg");
await using var output = File.Create("images.ofd");
await new ImageToOfdConverter(options).ConvertAsync(new Stream[] { first, second }, output);

Tips#