mirror of
https://github.com/UglyToad/PdfPig.git
synced 2025-06-28 15:30:17 +08:00
add more examples to the examples solution
This commit is contained in:
parent
27e251f921
commit
391b650e3c
@ -1,7 +1,6 @@
|
|||||||
namespace UglyToad.Examples
|
namespace UglyToad.Examples
|
||||||
{
|
{
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
|
||||||
using PdfPig;
|
using PdfPig;
|
||||||
using PdfPig.Content;
|
using PdfPig.Content;
|
||||||
using PdfPig.XObjects;
|
using PdfPig.XObjects;
|
||||||
@ -16,25 +15,23 @@
|
|||||||
{
|
{
|
||||||
foreach (var image in page.GetImages())
|
foreach (var image in page.GetImages())
|
||||||
{
|
{
|
||||||
|
if (!image.TryGetBytes(out var b))
|
||||||
|
{
|
||||||
|
b = image.RawBytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
var type = string.Empty;
|
||||||
switch (image)
|
switch (image)
|
||||||
{
|
{
|
||||||
case XObjectImage ximg:
|
case XObjectImage ximg:
|
||||||
byte[] b;
|
type = "XObject";
|
||||||
try
|
|
||||||
{
|
|
||||||
b = ximg.Bytes.ToArray();
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
b = ximg.RawBytes.ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.WriteLine($"Image with {b.Length} bytes and dictionary {ximg.ImageDictionary}.");
|
|
||||||
break;
|
break;
|
||||||
case InlineImage inline:
|
case InlineImage inline:
|
||||||
Console.WriteLine($"Inline image: {inline.RawBytes.Count} bytes.");
|
type = "Inline";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Console.WriteLine($"Image with {b.Count} bytes of type '{type}' on page {page.Number}. Location: {image.Bounds}.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
50
examples/GeneratePdfA2AFile.cs
Normal file
50
examples/GeneratePdfA2AFile.cs
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
namespace UglyToad.Examples
|
||||||
|
{
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using PdfPig.Content;
|
||||||
|
using PdfPig.Core;
|
||||||
|
using PdfPig.Writer;
|
||||||
|
|
||||||
|
internal static class GeneratePdfA2AFile
|
||||||
|
{
|
||||||
|
public static void Run(string trueTypeFontPath, string jpgImagePath)
|
||||||
|
{
|
||||||
|
var builder = new PdfDocumentBuilder
|
||||||
|
{
|
||||||
|
ArchiveStandard = PdfAStandard.A2A
|
||||||
|
};
|
||||||
|
|
||||||
|
var font = builder.AddTrueTypeFont(File.ReadAllBytes(trueTypeFontPath));
|
||||||
|
|
||||||
|
var page = builder.AddPage(PageSize.A4);
|
||||||
|
var pageTop = new PdfPoint(0, page.PageSize.Top);
|
||||||
|
|
||||||
|
var letters = page.AddText("This is some text added to the output file near the top of the page.",
|
||||||
|
12,
|
||||||
|
pageTop.Translate(20, -25),
|
||||||
|
font);
|
||||||
|
|
||||||
|
var bottomOfText = letters.Min(x => x.GlyphRectangle.Bottom);
|
||||||
|
|
||||||
|
var imagePlacement = new PdfRectangle(new PdfPoint(50, bottomOfText - 200),
|
||||||
|
new PdfPoint(150, bottomOfText));
|
||||||
|
page.AddJpeg(File.ReadAllBytes(jpgImagePath), imagePlacement);
|
||||||
|
|
||||||
|
var fileBytes = builder.Build();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var location = AppDomain.CurrentDomain.BaseDirectory;
|
||||||
|
var output = Path.Combine(location, "outputOfPdfA2A.pdf");
|
||||||
|
File.WriteAllBytes(output, fileBytes);
|
||||||
|
Console.WriteLine($"File output to: {output}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Failed to write output to file due to error: {ex}.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
41
examples/GetFormContents.cs
Normal file
41
examples/GetFormContents.cs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
namespace UglyToad.Examples
|
||||||
|
{
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using PdfPig;
|
||||||
|
using PdfPig.AcroForms.Fields;
|
||||||
|
|
||||||
|
internal static class GetFormContents
|
||||||
|
{
|
||||||
|
public static void Run(string filePath)
|
||||||
|
{
|
||||||
|
using (var document = PdfDocument.Open(filePath))
|
||||||
|
{
|
||||||
|
if (!document.TryGetForm(out var form))
|
||||||
|
{
|
||||||
|
Console.WriteLine($"No form found in file: {filePath}.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var page1Fields = form.GetFieldsForPage(1);
|
||||||
|
|
||||||
|
foreach (var field in page1Fields)
|
||||||
|
{
|
||||||
|
switch (field)
|
||||||
|
{
|
||||||
|
case AcroTextField text:
|
||||||
|
Console.WriteLine($"Found text field on page 1 with text: {text.Value}.");
|
||||||
|
break;
|
||||||
|
case AcroCheckboxesField cboxes:
|
||||||
|
Console.WriteLine($"Found checkboxes field on page 1 with {cboxes.Children.Count} checkboxes.");
|
||||||
|
break;
|
||||||
|
case AcroListBoxField listbox:
|
||||||
|
var opts = string.Join(", ", listbox.Options.Select(x => x.Name));
|
||||||
|
Console.WriteLine($"Found listbox field on page 1 with options: {opts}.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
30
examples/MergePdfDocuments.cs
Normal file
30
examples/MergePdfDocuments.cs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
namespace UglyToad.Examples
|
||||||
|
{
|
||||||
|
using System;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using PdfPig.Writer;
|
||||||
|
|
||||||
|
internal static class MergePdfDocuments
|
||||||
|
{
|
||||||
|
public static void Run(string filePath1, string filePath2, string filePath3)
|
||||||
|
{
|
||||||
|
var fileBytes = new[] { filePath1, filePath2, filePath3 }
|
||||||
|
.Select(File.ReadAllBytes).ToList();
|
||||||
|
|
||||||
|
var resultFileBytes = PdfMerger.Merge(fileBytes);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var location = AppDomain.CurrentDomain.BaseDirectory;
|
||||||
|
var output = Path.Combine(location, "outputOfMerge.pdf");
|
||||||
|
File.WriteAllBytes(output, resultFileBytes);
|
||||||
|
Console.WriteLine($"File output to: {output}");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Failed to write output to file due to error: {ex}.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -21,12 +21,27 @@
|
|||||||
() => OpenDocumentAndExtractWords.Run(Path.Combine(filesDirectory, "Two Page Text Only - from libre office.pdf")))
|
() => OpenDocumentAndExtractWords.Run(Path.Combine(filesDirectory, "Two Page Text Only - from libre office.pdf")))
|
||||||
},
|
},
|
||||||
{2,
|
{2,
|
||||||
|
("Extract Text with newlines (using built-in content extractor)",
|
||||||
|
() => ExtractTextWithNewlines.Run(Path.Combine(filesDirectory, "Two Page Text Only - from libre office.pdf")))
|
||||||
|
},
|
||||||
|
{3,
|
||||||
("Extract images",
|
("Extract images",
|
||||||
() => ExtractImages.Run(Path.Combine(filesDirectory, "2006_Swedish_Touring_Car_Championship.pdf")))
|
() => ExtractImages.Run(Path.Combine(filesDirectory, "2006_Swedish_Touring_Car_Championship.pdf")))
|
||||||
},
|
},
|
||||||
{3,
|
{4,
|
||||||
("Extract Text with newlines (using built-in content extractor)",
|
("Merge PDF Documents",
|
||||||
() => ExtractTextWithNewlines.Run(Path.Combine(filesDirectory, "Two Page Text Only - from libre office.pdf")))
|
() => MergePdfDocuments.Run(Path.Combine(filesDirectory, "Two Page Text Only - from libre office.pdf"),
|
||||||
|
Path.Combine(filesDirectory, "2006_Swedish_Touring_Car_Championship.pdf"),
|
||||||
|
Path.Combine(filesDirectory, "Rotated Text Libre Office.pdf")))
|
||||||
|
},
|
||||||
|
{5,
|
||||||
|
("Extract form contents",
|
||||||
|
() => GetFormContents.Run(Path.Combine(filesDirectory, "AcroFormsBasicFields.pdf")))
|
||||||
|
},
|
||||||
|
{6,
|
||||||
|
("Generate PDF/A-2A compliant file",
|
||||||
|
() => GeneratePdfA2AFile.Run(Path.Combine(filesDirectory, "..", "..", "Fonts", "TrueType", "Roboto-Regular.ttf"),
|
||||||
|
Path.Combine(filesDirectory, "smile-250-by-160.jpg")))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -53,6 +68,12 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
act.action.Invoke();
|
act.action.Invoke();
|
||||||
|
|
||||||
|
Console.WriteLine();
|
||||||
|
Console.WriteLine();
|
||||||
|
|
||||||
|
Console.WriteLine(choices);
|
||||||
|
Console.WriteLine();
|
||||||
} while (true);
|
} while (true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user