Help with Open Source Libraries

  • Thread starter Thread starter JRDumont
  • Start date Start date
J

JRDumont

Guest
I have written some code using the PDFSharp Open Source .Net library and I could use some help.

The example that I am going by uses C# but I'm trying to convert it to .Net

I'm having issues with the "page" variable and can't debug beyond this point.

C# example

static void Main()
{
const string watermark = "MTC";
const int emSize = 150;

// Get a fresh copy of the sample PDF file.
const string filename = "Portable Document Format.pdf";
var file = Path.Combine(Directory.GetCurrentDirectory(), filename);
File.Copy(Path.Combine("../../../../assets/PDFs/", filename), file, true);

// Remove ReadOnly attribute from the copy.
File.SetAttributes(file, File.GetAttributes(file) & ~FileAttributes.ReadOnly);

// Create the font for drawing the watermark.
var font = new XFont("Times New Roman", emSize, XFontStyle.BoldItalic);

// Open an existing document for editing and loop through its pages.
var document = PdfReader.Open(filename);

// Set version to PDF 1.4 (Acrobat 5) because we use transparency.
if (document.Version < 14)
document.Version = 14;

for (var idx = 0; idx < document.Pages.Count; idx++)
{
var page = document.Pages[idx];

switch (idx % 3)
{
case 0:
{ // Variation 1: Draw a watermark as a text string.

// Get an XGraphics object for drawing beneath the existing content.
var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);

// Get the size (in points) of the text.
var size = gfx.MeasureString(watermark, font);

// Define a rotation transformation at the center of the page.
gfx.TranslateTransform(page.Width / 2, page.Height / 2);
gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);
gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);

// Create a string format.
var format = new XStringFormat();
format.Alignment = XStringAlignment.Near;
format.LineAlignment = XLineAlignment.Near;

// Create a dimmed red brush.
XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));

// Draw the string.
gfx.DrawString(watermark, font, brush,
new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),
format);
}

Here's my .Net code so far

Sub PDFWatermark(ByRef PDFName As String, ByRef PDFProject As String, PDFQty As String)
Dim Watermark As String
Watermark = "Project: " + PDFProject + "Project Qty: " + PDFQty

Dim PDFemSize As Integer = 50
Dim PDFFont As New XFont("Times New Roman", PDFemSize, XFontStyle.BoldItalic)
Dim PDFDocument = PdfReader.Open(PDFName)
If (PDFDocument.Version < 14) Then
PDFDocument.Version = 14
End If
'Dim idx As Integer = 0
For idx As Integer = 0 To PDFDocument.Pages.Count

Dim page = PDFDocument.Pages(idx)

'Get an XGraphics object for drawing beneath the existing content.
'Dim xgfx = XGraphics.FromPdfPage(page)
Dim gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend)

'// Get the size (in points) of the text.
'Dim Size As Integer = gfx.MeasureString(Watermark, PDFFont)
'Dim Size1 As Integer = gfx.MeasureString("Project", PDFFont)
''// Define a rotation transformation at the center of the page.
'gfx.TranslateTransform(page.Width / 2, page.Height / 2)
'gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI)
'gfx.TranslateTransform(-page.Width / 2, -page.Height / 2)

'// Create a string format.
Dim Format As New XStringFormat()
Format.Alignment = XStringAlignment.Near
Format.LineAlignment = XLineAlignment.Near

'// Create a dimmed red brush.
Dim Brush = New XSolidBrush(XColor.FromArgb(128, 255, 0, 0))

'// Draw the string.
gfx.DrawString(Watermark, PDFFont, Brush, New XPoint(10, 10), Format)
Next

Continue reading...
 
Back
Top