Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
للتحقق من توافق مستند PDF مع PDF/A-1a أو PDF/A-1b، استخدم طريقة Validate من فئة Document. تتيح لك هذه الطريقة تحديد اسم الملف الذي سيتم حفظ النتيجة فيه ونوع التحقق المطلوب من تعداد PdfFormat: PDF_A_1A أو PDF_A_1B.
تعمل مقتطفات الشيفرة التالية أيضًا مع مكتبة Aspose.PDF.Drawing.
تظهر مقتطفات الشيفرة التالية كيفية التحقق من مستند PDF لمعيار PDF/A-1A.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ValidateToPdfA1aStandard()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "ValidatePDFAStandard.pdf"))
{
// Validate PDF for PDF/A-1a
document.Validate(dataDir + "validation-result-A1A.xml", Aspose.Pdf.PdfFormat.PDF_A_1A);
}
}
تظهر مقتطفات الشيفرة التالية كيفية التحقق من مستند PDF لمعيار PDF/A-1b.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void ValidateToPdfA1bStandard()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "ValidatePDFAStandard.pdf"))
{
// Validate PDF for PDF/A-1b
document.Validate(dataDir + "validation-result-A1B.xml", Aspose.Pdf.PdfFormat.PDF_A_1B);
}
}
تتيح لك واجهة برمجة تطبيقات Aspose.PDF إضافة جدول محتويات إما عند إنشاء PDF، أو إلى ملف موجود. تتيح لك فئة ListSection في مساحة أسماء Aspose.Pdf.Generator إنشاء جدول محتويات عند إنشاء PDF من الصفر. لإضافة العناوين، وهي عناصر من جدول المحتويات، استخدم فئة Aspose.Pdf.Generator.Heading.
لإضافة جدول محتويات إلى ملف PDF موجود، استخدم فئة Heading في مساحة أسماء Aspose.PDF. يمكن لمساحة أسماء Aspose.Pdf إنشاء ملفات PDF جديدة والتلاعب بالملفات الموجودة. لإضافة جدول محتويات إلى PDF موجود، استخدم مساحة أسماء Aspose.PDF. تظهر مقتطفات الشيفرة التالية كيفية إنشاء جدول محتويات داخل ملف PDF موجود.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddTOCToPdf()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "AddTOC.pdf"))
{
// Get access to the first page of PDF file
var tocPage = document.Pages.Insert(1);
// Create an object to represent TOC information
var tocInfo = new Aspose.Pdf.TocInfo();
var title = new Aspose.Pdf.Text.TextFragment("Table Of Contents");
title.TextState.FontSize = 20;
title.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;
// Set the title for TOC
tocInfo.Title = title;
tocPage.TocInfo = tocInfo;
// Create string objects which will be used as TOC elements
string[] titles = { "First page", "Second page", "Third page", "Fourth page" };
for (int i = 0; i < 2; i++)
{
// Create Heading object
var heading = new Aspose.Pdf.Heading(1);
var segment = new Aspose.Pdf.Text.TextSegment();
heading.TocPage = tocPage;
heading.Segments.Add(segment);
// Specify the destination page for the heading object
heading.DestinationPage = document.Pages[i + 2];
// Destination page
heading.Top = document.Pages[i + 2].Rect.Height;
// Destination coordinate
segment.Text = titles[i];
// Add heading to the page containing TOC
tocPage.Paragraphs.Add(heading);
}
// Save PDF document
document.Save(dataDir + "TOC_out.pdf");
}
}
تتيح لك Aspose.PDF أيضًا تعيين نوع TabLeaderType مختلف لمستويات جدول المحتويات المختلفة. تحتاج إلى تعيين خاصية LineDash من FormatArray بالقيمة المناسبة من تعداد TabLeaderType كما يلي.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateTocWithCustomFormatting()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add a TOC page
var tocPage = document.Pages.Add();
// Create TOC information
var tocInfo = new Aspose.Pdf.TocInfo();
// Set LeaderType
tocInfo.LineDash = Aspose.Pdf.Text.TabLeaderType.Solid;
// Set the title for TOC
var title = new Aspose.Pdf.Text.TextFragment("Table Of Contents");
title.TextState.FontSize = 30;
tocInfo.Title = title;
// Add the TOC section to the document
tocPage.TocInfo = tocInfo;
// Define the format of the four levels list by setting the left margins
// and text format settings of each level
tocInfo.FormatArrayLength = 4;
// Level 1
tocInfo.FormatArray[0].Margin.Left = 0;
tocInfo.FormatArray[0].Margin.Right = 30;
tocInfo.FormatArray[0].LineDash = Aspose.Pdf.Text.TabLeaderType.Dot;
tocInfo.FormatArray[0].TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold | Aspose.Pdf.Text.FontStyles.Italic;
// Level 2
tocInfo.FormatArray[1].Margin.Left = 10;
tocInfo.FormatArray[1].Margin.Right = 30;
tocInfo.FormatArray[1].LineDash = Aspose.Pdf.Text.TabLeaderType.None;
tocInfo.FormatArray[1].TextState.FontSize = 10;
// Level 3
tocInfo.FormatArray[2].Margin.Left = 20;
tocInfo.FormatArray[2].Margin.Right = 30;
tocInfo.FormatArray[2].TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;
// Level 4
tocInfo.FormatArray[3].LineDash = Aspose.Pdf.Text.TabLeaderType.Solid;
tocInfo.FormatArray[3].Margin.Left = 30;
tocInfo.FormatArray[3].Margin.Right = 30;
tocInfo.FormatArray[3].TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;
// Create a section in the Pdf document
var page = document.Pages.Add();
// Add four headings in the section
for (int level = 1; level <= 4; level++)
{
var heading = new Aspose.Pdf.Heading(level);
var segment = new Aspose.Pdf.Text.TextSegment();
heading.Segments.Add(segment);
heading.IsAutoSequence = true;
heading.TocPage = tocPage;
segment.Text = "Sample Heading " + level;
heading.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont("Arial Unicode MS");
// Add the heading into Table Of Contents.
heading.IsInList = true;
page.Paragraphs.Add(heading);
}
// Save PDF document
document.Save(dataDir + "TOC_out.pdf");
}
}
في حال كنت لا ترغب في عرض أرقام الصفحات، جنبًا إلى جنب مع العناوين في جدول المحتويات، يمكنك استخدام خاصية IsShowPageNumbers من فئة TOCInfo كـ false. يرجى مراجعة مقتطف الشيفرة التالي لإخفاء أرقام الصفحات في جدول المحتويات:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CreateTocWithHiddenPageNumbers()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add a TOC page
var tocPage = document.Pages.Add();
// Create TOC information
var tocInfo = new Aspose.Pdf.TocInfo();
// Set the title for TOC
var title = new Aspose.Pdf.Text.TextFragment("Table Of Contents");
title.TextState.FontSize = 20;
title.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;
tocInfo.Title = title;
// Add the TOC section to the document
tocPage.TocInfo = tocInfo;
// Hide page numbers in TOC
tocInfo.IsShowPageNumbers = false;
// Define the format of the four levels list by setting the left margins and
// text format settings of each level
tocInfo.FormatArrayLength = 4;
// Level 1
tocInfo.FormatArray[0].Margin.Right = 0;
tocInfo.FormatArray[0].TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold | Aspose.Pdf.Text.FontStyles.Italic;
// Level 2
tocInfo.FormatArray[1].Margin.Left = 30;
tocInfo.FormatArray[1].TextState.Underline = true;
tocInfo.FormatArray[1].TextState.FontSize = 10;
// Level 3
tocInfo.FormatArray[2].TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;
// Level 4
tocInfo.FormatArray[3].TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;
// Create a section in the Pdf document
var page = document.Pages.Add();
// Add four headings in the section
for (int level = 1; level <= 4; level++)
{
var heading = new Aspose.Pdf.Heading(level);
var segment = new Aspose.Pdf.Text.TextSegment();
heading.TocPage = tocPage;
heading.Segments.Add(segment);
heading.IsAutoSequence = true;
segment.Text = "this is heading of level " + level;
heading.IsInList = true;
page.Paragraphs.Add(heading);
}
// Save PDF document
document.Save(dataDir + "TOC_out.pdf");
}
}
من الشائع تخصيص ترقيم الصفحات في جدول المحتويات أثناء إضافة جدول المحتويات في مستند PDF. على سبيل المثال، قد نحتاج إلى إضافة بعض البادئات قبل رقم الصفحة مثل P1، P2، P3 وما إلى ذلك. في هذه الحالة، توفر Aspose.PDF for .NET خاصية PageNumbersPrefix من فئة TocInfo التي يمكن استخدامها لتخصيص أرقام الصفحات كما هو موضح في مقتطف الشيفرة التالي.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void CustomizePageNumbersAddingToC()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "CustomizePageNumbersAddingToC.pdf"))
{
// Get access to first page of PDF file
Page tocPage = document.Pages.Insert(1);
// Create object to represent TOC information
var tocInfo = new Aspose.Pdf.TocInfo();
var title = new Aspose.Pdf.Text.TextFragment("Table Of Contents");
title.TextState.FontSize = 20;
title.TextState.FontStyle = Aspose.Pdf.Text.FontStyles.Bold;
// Set the title for TOC
tocInfo.Title = title;
tocInfo.PageNumbersPrefix = "P";
tocPage.TocInfo = tocInfo;
// Loop through the pages to create TOC entries
for (int i = 1; i < document.Pages.Count; i++)
{
// Create Heading object
var heading2 = new Aspose.Pdf.Heading(1);
var segment2 = new Aspose.Pdf.Text.TextSegment();
heading2.TocPage = tocPage;
heading2.Segments.Add(segment2);
// Specify the destination page for heading object
heading2.DestinationPage = document.Pages[i + 1];
// Destination page
heading2.Top = document.Pages[i + 1].Rect.Height;
// Destination coordinate
segment2.Text = "Page " + i.ToString();
// Add heading to page containing TOC
tocPage.Paragraphs.Add(heading2);
}
// Save PDF document
document.Save(dataDir + "CustomizePageNumbersAddingToC_out.pdf");
}
}
نطبق صلاحيات الوصول على ملفات PDF بحيث يمكن لمجموعة معينة من المستخدمين الوصول إلى ميزات/كائنات معينة من مستندات PDF. من أجل تقييد الوصول إلى ملف PDF، عادةً ما نقوم بتطبيق التشفير وقد يكون لدينا متطلبات لتعيين انتهاء صلاحية ملف PDF، بحيث يحصل المستخدم الذي يصل إلى/يستعرض المستند على تنبيه صالح بشأن انتهاء صلاحية ملف PDF.
لتحقيق المتطلبات المذكورة أعلاه، يمكننا استخدام كائن JavascriptAction. يرجى إلقاء نظرة على مقتطف الشيفرة التالي.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void SetExpiryDate()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Create PDF document
using (var document = new Aspose.Pdf.Document())
{
// Add page
var page = document.Pages.Add();
// Add text fragment to paragraphs collection of page object
page.Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Hello World..."));
// Create JavaScript object to set PDF expiry date
var javaScript = new Aspose.Pdf.Annotations.JavascriptAction(
"var year=2017;" +
"var month=5;" +
"today = new Date(); today = new Date(today.getFullYear(), today.getMonth());" +
"expiry = new Date(year, month);" +
"if (today.getTime() > expiry.getTime())" +
"app.alert('The file is expired. You need a new one.');"
);
// Set JavaScript as PDF open action
document.OpenAction = javaScript;
// Save PDF Document
document.Save(dataDir + "SetExpiryDate_out.pdf");
}
}
طلب منا عميل إضافة ميزة تتيح للمطورين تحديد تقدم توليد ملف PDF. إليك الرد على هذا الطلب.
تتيح لك حقل CustomerProgressHandler من فئة DocSaveOptions تحديد كيفية تقدم توليد PDF. يحتوي المعالج على الأنواع التالية:
تظهر مقتطفات الشيفرة أدناه كيفية استخدام CustomerProgressHandler.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void DetermineProgress()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "AddTOC.pdf"))
{
// Create DocSaveOptions instance and set custom progress handler
var saveOptions = new Aspose.Pdf.DocSaveOptions();
saveOptions.CustomProgressHandler = new Aspose.Pdf.UnifiedSaveOptions.ConversionProgressEventHandler(ShowProgressOnConsole);
// Save PDF Document
document.Save(dataDir + "DetermineProgress_out.pdf", saveOptions);
}
}
// Method to handle progress and display it on the console
private static void ShowProgressOnConsole(Aspose.Pdf.UnifiedSaveOptions.ProgressEventHandlerInfo eventInfo)
{
switch (eventInfo.EventType)
{
case Aspose.Pdf.ProgressEventType.TotalProgress:
Console.WriteLine(String.Format("{0} - Conversion progress : {1}% .", DateTime.Now.ToLongTimeString(), eventInfo.Value.ToString()));
break;
case Aspose.Pdf.ProgressEventType.SourcePageAnalysed:
Console.WriteLine(String.Format("{0} - Source page {1} of {2} analyzed.", DateTime.Now.ToLongTimeString(), eventInfo.Value.ToString(), eventInfo.MaxValue.ToString()));
break;
case Aspose.Pdf.ProgressEventType.ResultPageCreated:
Console.WriteLine(String.Format("{0} - Result page's {1} of {2} layout created.", DateTime.Now.ToLongTimeString(), eventInfo.Value.ToString(), eventInfo.MaxValue.ToString()));
break;
case Aspose.Pdf.ProgressEventType.ResultPageSaved:
Console.WriteLine(String.Format("{0} - Result page {1} of {2} exported.", DateTime.Now.ToLongTimeString(), eventInfo.Value.ToString(), eventInfo.MaxValue.ToString()));
break;
default:
break;
}
}
غالبًا ما تتضمن مستندات PDF نماذج تحتوي على عناصر تفاعلية قابلة للتعبئة مثل أزرار الاختيار، مربعات الاختيار، صناديق النص، القوائم، إلخ. لجعلها غير قابلة للتعديل لأغراض تطبيقية مختلفة، نحتاج إلى تسطيح ملف PDF. توفر Aspose.PDF وظيفة لتسطيح PDF الخاص بك في C# مع بضع أسطر من الشيفرة:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void FlattenForms()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_WorkingDocuments();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "input.pdf"))
{
// Flatten Fillable PDF
if (document.Form.Fields.Count() > 0)
{
foreach (var item in document.Form.Fields)
{
item.Flatten();
}
}
// Save PDF document
document.Save(dataDir + "FlattenForms_out.pdf");
}
}
للتحقق مما إذا كان المستند قد تم تحديثه بشكل تزايدي، استخدم طريقة HasIncrementalUpdate
من فئة Document. تقوم هذه الطريقة بتحليل ملف PDF وتعيد قيمة منطقية تشير إلى ما إذا تم اكتشاف تحديثات تزايدية. لاحظ أنه عند حفظ مستند باستخدام طريقة Save بدون معلمات، يتم حفظه بشكل تزايدي.
توضح الشيفرة التالية في C# كيفية استخدام طريقة HasIncrementalUpdate
:
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.