Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
私たちは常に、C#プロジェクトでPDFドキュメントを生成し、それを正確かつ効果的に扱う方法を探しています。ライブラリからの使いやすい関数を持つことで、作業の多くを追跡し、PDFを生成するための時間のかかる詳細にあまり時間をかけずに済みます。
以下のコードスニペットは、Aspose.PDF.Drawingライブラリでも動作します。
Aspose.PDF for .NET APIを使用すると、C#およびVB.NETを使用してPDFファイルを作成および読み取ることができます。このAPIは、WinForms、ASP.NETなど、さまざまな.NETアプリケーションで使用できます。この記事では、Aspose.PDF for .NET APIを使用して、.NETアプリケーションでPDFファイルを簡単に生成および読み取る方法を示します。
C#を使用してPDFファイルを作成するには、以下の手順を使用できます。
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateHelloWorldDocument()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_QuickStart();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
// Add text to new page
page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Hello World!"));
// Save PDF document
document.Save(dataDir + "HelloWorld_out.pdf");
}
}
Aspose.PDF for .NETは、既存のPDFドキュメントを作成および操作する機能を提供します。PDFファイル内にテキスト要素を追加すると、結果のPDFは検索可能になります。ただし、テキストを含む画像をPDFファイルに変換する場合、PDF内の内容は検索可能ではありません。ただし、回避策として、結果のファイルにOCRを使用することで、検索可能にすることができます。
以下に示すロジックは、PDF画像のテキストを認識します。認識には、外部OCRがHOCR標準をサポートしていることを使用できます。テスト目的で、無料のGoogle Tesseract OCRを使用しました。したがって、最初にシステムにTesseract-OCRをインストールし、tesseractコンソールアプリケーションを持っている必要があります。
この要件を達成するための完全なコードは以下の通りです:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateSearchableDocument()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_QuickStart();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "SearchableDocument.pdf"))
{
document.Convert(CallBackGetHocr);
// Save PDF document
document.Save(dataDir + "SearchableDocument_out.pdf");
}
}
private static string CallBackGetHocr(System.Drawing.Image img)
{
var tmpFile = Path.GetTempFileName();
try
{
using (var bmp = new System.Drawing.Bitmap(img))
{
bmp.Save(tmpFile, System.Drawing.Imaging.ImageFormat.Bmp);
}
var inputFile = string.Concat('"', tmpFile, '"');
var outputFile = string.Concat('"', tmpFile, '"');
var arguments = string.Concat(inputFile, " ", outputFile, " -l eng hocr");
var tesseractProcessName = RunExamples.GetTesseractExePath();
var psi = new System.Diagnostics.ProcessStartInfo(tesseractProcessName, arguments)
{
UseShellExecute = true,
CreateNoWindow = true,
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
WorkingDirectory = Path.GetDirectoryName(tesseractProcessName)
};
var p = new System.Diagnostics.Process
{
StartInfo = psi
};
p.Start();
p.WaitForExit();
using (var streamReader = new StreamReader(tmpFile + ".hocr"))
{
string text = streamReader.ReadToEnd();
return text;
}
}
finally
{
if (File.Exists(tmpFile))
{
File.Delete(tmpFile);
}
if (File.Exists(tmpFile + ".hocr"))
{
File.Delete(tmpFile + ".hocr");
}
}
}
このコードスニペットは、PDFドキュメントとそのタグ付きコンテンツを操作し、Aspose.PDFライブラリを使用して処理します。
この例では、PDFの最初のページのタグ付きコンテンツに新しいspan要素を作成し、すべてのBDC要素を見つけてそれらをspanに関連付けます。変更されたドキュメントは、その後保存されます。
BDCPropertiesオブジェクトを使用して、mcid、lang、および展開テキストを指定するBDCステートメントを作成できます:
var bdc = new Aspose.Pdf.Operators.BDC("P", new Aspose.Pdf.Facades.BDCProperties(1, "de", "Hallo, welt!"));
構造ツリーを作成した後、要素オブジェクトのメソッドTagを使用してBDCオペレーターを指定された構造の要素にバインドすることができます:
Aspose.Pdf.LogicalStructure.SpanElement span = content.CreateSpanElement();
span.Tag(bdc);
アクセシブルなPDFを作成する手順:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateAnAccessibleDocument()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_QuickStart();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "tourguidev2_gb_tags.pdf"))
{
// Access tagged content
Aspose.Pdf.Tagged.ITaggedContent content = document.TaggedContent;
// Create a span element
Aspose.Pdf.LogicalStructure.SpanElement span = content.CreateSpanElement();
// Append span to root element
content.RootElement.AppendChild(span);
// Iterate over page contents
foreach (var op in document.Pages[1].Contents)
{
var bdc = op as Aspose.Pdf.Operators.BDC;
if (bdc != null)
{
span.Tag(bdc);
}
}
// Save PDF document
document.Save(dataDir + "AccessibleDocument_out.pdf");
}
}
このコードは、ドキュメントのタグ付きコンテンツ内にspan要素を作成し、最初のページから特定のコンテンツ(BDC操作)をこのspanでタグ付けすることによってPDFを修正します。変更されたPDFは、新しいファイルに保存されます。
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.