Skip to content

Commit 18b0e2e

Browse files
authored
Merge pull request #339 from aspose-pdf/Whats_new_fix_samples
Whats new - fix samples - Part #1
2 parents 6e3ac48 + af07f9e commit 18b0e2e

File tree

1 file changed

+78
-72
lines changed

1 file changed

+78
-72
lines changed

net/whatsnew/_index.md

Lines changed: 78 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -157,24 +157,24 @@ The SHA-256 hash algorithm is used for generating the signature. ECDSA signature
157157
You can use your usual code to sign documents with ECDSA and to verify signatures:
158158

159159
```cs
160-
public void Sign(string cert, string inputPdfFile, string outFile)
160+
private static void Sign(string cert, string inputPdfFile, string signedPdfFile)
161161
{
162-
using (Document document = new Document(inputPdfFile))
162+
using (var document = new Aspose.Pdf.Document(inputPdfFile))
163163
{
164-
using (PdfFileSignature signature = new PdfFileSignature(document))
164+
using (var signature = new Aspose.Pdf.Facades.PdfFileSignature(document))
165165
{
166-
PKCS7Detached pkcs = new PKCS7Detached(cert, "12345");
166+
var pkcs = new Aspose.Pdf.Forms.PKCS7Detached(cert, "12345");
167167
signature.Sign(1, true, new System.Drawing.Rectangle(300, 100, 400, 200), pkcs);
168-
signature.Save(outFile);
168+
signature.Save(signedPdfFile);
169169
}
170170
}
171171
}
172172

173-
public static void Verify(string fileName)
173+
private static void Verify(string signedPdfFile)
174174
{
175-
using (Document document = new Document(fileName))
175+
using (var document = new Aspose.Pdf.Document(signedPdfFile))
176176
{
177-
using (PdfFileSignature signature = new PdfFileSignature(document))
177+
using (var signature = new Aspose.Pdf.Facades.PdfFileSignature(document))
178178
{
179179
var sigNames = signature.GetSignNames();
180180
foreach (var sigName in sigNames)
@@ -190,31 +190,31 @@ public static void Verify(string fileName)
190190
Sometimes, it is necessary to crop an image before inserting it into a PDF. We have added an overloaded version of the `AddImage()` method to support adding cropped images:
191191

192192
```cs
193-
var imagePath = "";
194-
var resultPdfPath = "";
195-
196-
using (Document document = new Document())
193+
private static void InsertCroppedImageToPdf(string imageFile, string resultPdf)
197194
{
198-
using (Stream imgStream = File.OpenRead(imagePath))
195+
using (var document = new Aspose.Pdf.Document())
199196
{
200-
// Define the rectangle where the image will be placed on the PDF page
201-
Rectangle imageRect = new Rectangle(17.62, 65.25, 602.62, 767.25);
197+
using (Stream imgStream = File.OpenRead(imageFile))
198+
{
199+
// Define the rectangle where the image will be placed on the PDF page
200+
var imageRect = new Aspose.Pdf.Rectangle(17.62, 65.25, 602.62, 767.25);
202201

203-
// Crop the image to half its original width and height
204-
var w = imageRect.Width / 2;
205-
var h = imageRect.Height / 2;
206-
Rectangle bbox = new Rectangle(imageRect.LLX, imageRect.LLY, imageRect.LLX + w, imageRect.LLY + h);
202+
// Crop the image to half its original width and height
203+
var w = imageRect.Width / 2;
204+
var h = imageRect.Height / 2;
205+
var bbox = new Aspose.Pdf.Rectangle(imageRect.LLX, imageRect.LLY, imageRect.LLX + w, imageRect.LLY + h);
207206

208-
// Add a new page to the document
209-
Page page = document.Pages.Add();
207+
// Add a new page to the document
208+
var page = document.Pages.Add();
210209

211-
// Insert the cropped image onto the page, specifying the original position (imageRect)
212-
// and the cropping area (bbox)
213-
page.AddImage(imgStream, imageRect, bbox);
214-
}
210+
// Insert the cropped image onto the page, specifying the original position (imageRect)
211+
// and the cropping area (bbox)
212+
page.AddImage(imgStream, imageRect, bbox);
213+
}
215214

216-
// Save the document to the specified file path
217-
document.Save(resultPdfPath);
215+
// Save the document to the specified file path
216+
document.Save(resultPdf);
217+
}
218218
}
219219
```
220220

@@ -232,28 +232,30 @@ try
232232
}
233233
catch (Exception ex)
234234
{
235-
PdfException.GenerateCrashReport(new CrashReportOptions(ex));
235+
Aspose.Pdf.PdfException.GenerateCrashReport(new Aspose.Pdf.CrashReportOptions(ex));
236236
}
237237
```
238238

239239
Extracting an PDF document layer elements and saving into new PDF stream are available from now. In PDF documents, layers (also known as Optional Content Groups or OCGs) are used for various purposes, primarily to manage and control the visibility of content within the document. This functionality is particularly useful in design, engineering, and publishing. For example: blueprint aspects, complex diagram components, language versions of the same content.
240240

241241
```cs
242-
var documentPath = "";
243-
var resultPdfPath ="";
244-
245-
var inputDocument = new Document(documentPath);
246-
var inputPage = inputDocument.Pages[1];
242+
private static void ExtractPdfLayer(string inputPdfPath, string outputPdfName)
243+
{
244+
using (var inputDocument = new Aspose.Pdf.Document(inputPdfPath))
245+
{
246+
var inputPage = inputDocument.Pages[1];
247247

248-
var layers = inputPage.Layers;
248+
var layers = inputPage.Layers;
249249

250-
foreach (var layer in layers)
251-
{
252-
var outputPdf = string.Format("{0}_{1}.pdf", resultPdfPath, layer.Id);
250+
foreach (var layer in layers)
251+
{
252+
var extractedLayerPdfName = string.Format("{0}_{1}.pdf", outputPdfName, layer.Id);
253253

254-
using (var stream = File.Create(outputPdf))
255-
{
256-
layer.Save(stream);
254+
using (var stream = File.Create(extractedLayerPdfName))
255+
{
256+
layer.Save(stream);
257+
}
258+
}
257259
}
258260
}
259261
```
@@ -263,19 +265,21 @@ The `GraphicalPdfComparer` class is added for the graphic comparison of PDF docu
263265
The following code snippet demonstrates the graphic comparison of two PDF documents and saves an image with the differences into the resultant PDF document:
264266

265267
```cs
266-
var firstDocumentPath = "";
267-
var secondDocumentPath = "";
268-
var resultPdfPath = "";
269-
270-
using (Document doc1 = new Document(firstDocumentPath), doc2 = new Document(secondDocumentPath))
268+
private static void PdfGraphicComparison(string firstDocumentPath, string secondDocumentPath, string comparisonResultPdfPath)
271269
{
272-
GraphicalPdfComparer comparer = new GraphicalPdfComparer()
270+
using (var firstDocument = new Aspose.Pdf.Document(firstDocumentPath))
273271
{
274-
Threshold = 3.0,
275-
Color = Color.Red,
276-
Resolution = new Resolution(300)
277-
};
278-
comparer.CompareDocumentsToPdf(doc1, doc2, resultPdfPath);
272+
using (var secondDocument = new Aspose.Pdf.Document(secondDocumentPath))
273+
{
274+
var comparer = new Aspose.Pdf.Comparison.GraphicalComparison.GraphicalPdfComparer()
275+
{
276+
Threshold = 3.0,
277+
Color = Color.Red,
278+
Resolution = new Resolution(300)
279+
};
280+
comparer.CompareDocumentsToPdf(firstDocument, secondDocument, comparisonResultPdfPath);
281+
}
282+
}
279283
}
280284
```
281285

@@ -284,29 +288,31 @@ API implemented for integrating FileFormat.HEIC and Aspose.PDF. The HEIC (High-E
284288
To convert HEIC images to PDF user should add the reference to `FileFormat.HEIC` NuGet package and use the following code snippet:
285289

286290
```cs
287-
var heicImagePath = "iphone_photo.heic";
288-
var resultPdfPath = "iphone_photo.pdf";
289-
290-
using (var fs = new FileStream(heicImagePath, FileMode.Open))
291+
private static void HeicToPdf(string heicImagePath, string resultPdfPath)
291292
{
292-
HeicImage image = HeicImage.Load(fs);
293-
var pixels = image.GetByteArray(PixelFormat.Rgb24);
294-
var width = (int)image.Width;
295-
var height = (int)image.Height;
293+
using (var fs = new FileStream(heicImagePath, FileMode.Open))
294+
{
295+
var image = FileFormat.Heic.Decoder.HeicImage.Load(fs);
296+
var pixels = image.GetByteArray(FileFormat.Heic.Decoder.PixelFormat.Rgb24);
297+
var width = (int)image.Width;
298+
var height = (int)image.Height;
296299

297-
var document = new Document();
298-
Page page = document.Pages.Add();
299-
Aspose.Pdf.Image asposeImage = new Aspose.Pdf.Image();
300-
asposeImage.BitmapInfo = new BitmapInfo(pixels, width, height, BitmapInfo.PixelFormat.Rgb24);
301-
page.PageInfo.Height = height;
302-
page.PageInfo.Width = width;
303-
page.PageInfo.Margin.Bottom = 0;
304-
page.PageInfo.Margin.Top = 0;
305-
page.PageInfo.Margin.Right = 0;
306-
page.PageInfo.Margin.Left = 0;
307-
308-
page.Paragraphs.Add(asposeImage);
309-
document.Save(resultPdfPath);
300+
using (var document = new Aspose.Pdf.Document())
301+
{
302+
var page = document.Pages.Add();
303+
var asposeImage = new Aspose.Pdf.Image();
304+
asposeImage.BitmapInfo = new Aspose.Pdf.BitmapInfo(pixels, width, height, Aspose.Pdf.BitmapInfo.PixelFormat.Rgb24);
305+
page.PageInfo.Height = height;
306+
page.PageInfo.Width = width;
307+
page.PageInfo.Margin.Bottom = 0;
308+
page.PageInfo.Margin.Top = 0;
309+
page.PageInfo.Margin.Right = 0;
310+
page.PageInfo.Margin.Left = 0;
311+
312+
page.Paragraphs.Add(asposeImage);
313+
document.Save(resultPdfPath);
314+
}
315+
}
310316
}
311317
```
312318

0 commit comments

Comments
 (0)