Skip to content

Commit 7fddc82

Browse files
authored
Merge pull request #208 from aspose-pdf/examples/9f94b342-graphs-zugferd-operators
Add 84 ZUGFeRD operator examples to Graphs API
2 parents c5165ae + e8b2e42 commit 7fddc82

86 files changed

Lines changed: 6551 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Annotations; // optional, kept for consistency
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
const string inputPath = "input.pdf";
11+
const string outputPath = "output.pdf";
12+
const int timeoutMs = 5000; // close after 5 seconds
13+
14+
if (!File.Exists(inputPath))
15+
{
16+
Console.Error.WriteLine($"File not found: {inputPath}");
17+
return;
18+
}
19+
20+
// Load the PDF inside a using block for proper disposal
21+
using (Document doc = new Document(inputPath))
22+
{
23+
// JavaScript that sets a timeout to close the document
24+
string js = $"app.setTimeOut('this.closeDoc();', {timeoutMs});";
25+
26+
// Attach the script to the document's open action (correct property)
27+
doc.OpenAction = new JavascriptAction(js);
28+
// Alternatively you could use: doc.Actions.OnOpen = new JavascriptAction(js);
29+
30+
// Save the modified PDF
31+
doc.Save(outputPath);
32+
}
33+
34+
Console.WriteLine($"PDF saved with auto‑close script to '{outputPath}'.");
35+
}
36+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Annotations;
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
const string inputPath = "input.pdf";
11+
const string outputPath = "output_with_print_action.pdf";
12+
13+
if (!File.Exists(inputPath))
14+
{
15+
Console.Error.WriteLine($"File not found: {inputPath}");
16+
return;
17+
}
18+
19+
// Load the existing PDF document
20+
using (Document doc = new Document(inputPath))
21+
{
22+
// Add a JavaScript action that opens the print dialog when the document is opened
23+
// The script "this.print(true);" triggers the print dialog immediately.
24+
doc.OpenAction = new JavascriptAction("this.print(true);");
25+
26+
// Save the modified PDF
27+
doc.Save(outputPath);
28+
}
29+
30+
Console.WriteLine($"PDF saved with auto‑print action: {outputPath}");
31+
}
32+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
global using System;
2+
global using System.IO;
3+
global using Aspose.Pdf;
4+
global using Aspose.Pdf.Annotations;
5+
6+
// Program.cs
7+
using Aspose.Pdf.Annotations; // needed for JavascriptAction (kept for clarity)
8+
9+
class Program
10+
{
11+
static void Main()
12+
{
13+
const string inputPath = "input.pdf";
14+
const string outputPath = "output_with_js.pdf";
15+
16+
// Verify source file exists
17+
if (!File.Exists(inputPath))
18+
{
19+
Console.Error.WriteLine($"File not found: {inputPath}");
20+
return;
21+
}
22+
23+
// Load the PDF inside a using block (ensures proper disposal)
24+
using (Document doc = new Document(inputPath))
25+
{
26+
// JavaScript that triggers an automatic save every 60 000 ms (1 minute)
27+
// The script uses app.setTimeOut to schedule a saveAs operation.
28+
string js = $"app.setTimeOut('this.saveAs({{cPath:\"{outputPath}\"}});', 60000);";
29+
30+
// Assign the script to the document‑level OpenAction (executed when the PDF is opened)
31+
doc.OpenAction = new JavascriptAction(js);
32+
33+
// Save the modified PDF (no extra SaveOptions needed for PDF output)
34+
doc.Save(outputPath);
35+
}
36+
37+
Console.WriteLine($"PDF saved with auto‑save JavaScript to '{outputPath}'.");
38+
}
39+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
const string inputPath = "input_zugferd.pdf";
10+
const string outputPath = "output_zugferd_with_metadata.pdf";
11+
12+
if (!File.Exists(inputPath))
13+
{
14+
Console.Error.WriteLine($"File not found: {inputPath}");
15+
return;
16+
}
17+
18+
// Load the existing ZUGFeRD PDF, modify metadata, and save.
19+
using (Document doc = new Document(inputPath))
20+
{
21+
// Add custom metadata entries.
22+
// The Metadata property implements IDictionary<string, XmpValue> and
23+
// provides an Add(string, object) overload for simple values.
24+
doc.Metadata.Add("ProjectCode", "PRJ-2023-001");
25+
doc.Metadata.Add("Department", "Finance");
26+
27+
// Save the updated PDF.
28+
doc.Save(outputPath);
29+
}
30+
31+
Console.WriteLine($"PDF saved with custom metadata to '{outputPath}'.");
32+
}
33+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Annotations;
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
const string inputPath = "input.pdf";
11+
const string outputPath = "output_expiry.pdf";
12+
13+
if (!File.Exists(inputPath))
14+
{
15+
Console.Error.WriteLine($"File not found: {inputPath}");
16+
return;
17+
}
18+
19+
// JavaScript that checks the current date against an expiry date.
20+
string js = @"
21+
Date today = new Date();
22+
Date expiry = new Date('2025-12-31T23:59:59');
23+
if (today > expiry) {
24+
app.alert('This document has expired.');
25+
this.closeDoc();
26+
}
27+
";
28+
29+
using (Document doc = new Document(inputPath))
30+
{
31+
// Assign the JavaScript to run when the document is opened.
32+
doc.OpenAction = new JavascriptAction(js);
33+
34+
doc.Save(outputPath);
35+
}
36+
37+
Console.WriteLine($"PDF with expiry action saved to '{outputPath}'.");
38+
}
39+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
// Input PDF to which the attachment will be added
10+
const string inputPdfPath = "input.pdf";
11+
// File that will be attached (its content will be encrypted together with the PDF)
12+
const string attachmentFilePath = "secret.txt";
13+
// Output encrypted PDF
14+
const string outputPdfPath = "output_encrypted.pdf";
15+
16+
// Passwords for PDF encryption
17+
const string userPassword = "user123"; // password required to open the PDF
18+
const string ownerPassword = "owner123"; // password with full permissions
19+
20+
// Verify that required files exist
21+
if (!File.Exists(inputPdfPath))
22+
{
23+
Console.Error.WriteLine($"Input PDF not found: {inputPdfPath}");
24+
return;
25+
}
26+
if (!File.Exists(attachmentFilePath))
27+
{
28+
Console.Error.WriteLine($"Attachment file not found: {attachmentFilePath}");
29+
return;
30+
}
31+
32+
// Load the existing PDF (lifecycle: load)
33+
using (Document pdfDoc = new Document(inputPdfPath))
34+
{
35+
// ------------------------------------------------------------
36+
// 1. Embed the file as an embedded file (portfolio entry).
37+
// ------------------------------------------------------------
38+
// Create a FileSpecification with a display name.
39+
FileSpecification fileSpec = new FileSpecification(Path.GetFileName(attachmentFilePath));
40+
// Set the file contents – this stream will be encrypted together with the PDF.
41+
fileSpec.Contents = new MemoryStream(File.ReadAllBytes(attachmentFilePath));
42+
// Optionally set a description.
43+
fileSpec.Description = "Encrypted attachment";
44+
// Add the specification to the document's EmbeddedFiles collection.
45+
pdfDoc.EmbeddedFiles.Add(fileSpec);
46+
47+
// ------------------------------------------------------------
48+
// 2. Define permissions for the encrypted PDF.
49+
// ------------------------------------------------------------
50+
Permissions permissions = Permissions.PrintDocument | Permissions.ModifyContent;
51+
52+
// ------------------------------------------------------------
53+
// 3. Encrypt the whole document (including the embedded file).
54+
// ------------------------------------------------------------
55+
pdfDoc.Encrypt(userPassword, ownerPassword, permissions, CryptoAlgorithm.AESx256);
56+
57+
// ------------------------------------------------------------
58+
// 4. Save the encrypted PDF.
59+
// ------------------------------------------------------------
60+
pdfDoc.Save(outputPdfPath);
61+
}
62+
63+
Console.WriteLine($"Encrypted PDF with attachment saved to '{outputPdfPath}'.");
64+
}
65+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Annotations;
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
const string inputPdf = "input.pdf"; // source PDF
11+
const string outputPdf = "output.pdf"; // PDF with attachment
12+
const string attachFile = "document.pdf"; // file to attach
13+
14+
if (!File.Exists(inputPdf))
15+
{
16+
Console.Error.WriteLine($"Input PDF not found: {inputPdf}");
17+
return;
18+
}
19+
20+
if (!File.Exists(attachFile))
21+
{
22+
Console.Error.WriteLine($"Attachment file not found: {attachFile}");
23+
return;
24+
}
25+
26+
// Load the PDF document
27+
using (Document doc = new Document(inputPdf))
28+
{
29+
// Choose the page where the annotation will be placed (first page)
30+
Page page = doc.Pages[1];
31+
32+
// Define the rectangle that bounds the annotation icon (Aspose.Pdf.Rectangle, not System.Drawing.Rectangle)
33+
Aspose.Pdf.Rectangle rect = new Aspose.Pdf.Rectangle(100, 500, 200, 600);
34+
35+
// Create a FileSpecification describing the attached file (description is optional)
36+
FileSpecification fileSpec = new FileSpecification(attachFile, "Attached PDF document");
37+
// NOTE: Aspose.Pdf.FileSpecification does not expose a MimeType property. The MIME type is inferred from the file extension.
38+
39+
// Create the file attachment annotation
40+
FileAttachmentAnnotation fileAnnot = new FileAttachmentAnnotation(page, rect, fileSpec)
41+
{
42+
Title = "Attachment", // title shown in the popup
43+
Contents = "Click the icon to open the attached PDF.", // tooltip text
44+
Icon = FileIcon.Paperclip // use the correct enum for the icon
45+
};
46+
47+
// Add the annotation to the page
48+
page.Annotations.Add(fileAnnot);
49+
50+
// Save the modified PDF
51+
doc.Save(outputPdf);
52+
}
53+
54+
Console.WriteLine($"File attachment added and saved to '{outputPdf}'.");
55+
}
56+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Forms;
5+
using Aspose.Pdf.Annotations;
6+
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
const string inputPath = "input_form.pdf";
12+
const string outputPath = "output_form.pdf";
13+
14+
if (!File.Exists(inputPath))
15+
{
16+
Console.Error.WriteLine($"File not found: {inputPath}");
17+
return;
18+
}
19+
20+
// Load the PDF document containing the form fields
21+
using (Document doc = new Document(inputPath))
22+
{
23+
// Access the form object
24+
Form form = doc.Form;
25+
26+
// Ensure the form will recalculate when any field changes
27+
form.AutoRecalculate = true;
28+
29+
// Retrieve the summary field where the total will be displayed
30+
// (Assumes a field named "Total" already exists in the PDF)
31+
Field totalField = doc.Form["Total"] as Field;
32+
if (totalField == null)
33+
{
34+
Console.Error.WriteLine("Summary field 'Total' not found.");
35+
return;
36+
}
37+
38+
// JavaScript that sums the values of line‑item fields and writes the result
39+
// Adjust the field names ("Item1", "Item2", "Item3") to match your PDF.
40+
string jsCode = @"
41+
var sum = 0;
42+
var f1 = this.getField('Item1');
43+
var f2 = this.getField('Item2');
44+
var f3 = this.getField('Item3');
45+
if (f1 && f1.value) sum += parseFloat(f1.value);
46+
if (f2 && f2.value) sum += parseFloat(f2.value);
47+
if (f3 && f3.value) sum += parseFloat(f3.value);
48+
this.getField('Total').value = sum.toFixed(2);
49+
";
50+
51+
// Assign the JavaScript to the OnCalculate action of the summary field
52+
totalField.Actions.OnCalculate = new JavascriptAction(jsCode);
53+
54+
// Save the modified PDF
55+
doc.Save(outputPath);
56+
}
57+
58+
Console.WriteLine($"PDF with JavaScript calculation saved to '{outputPath}'.");
59+
}
60+
}

0 commit comments

Comments
 (0)