none
Help with Open Source Libraries RRS feed

  • Question

  • 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
    Saturday, August 1, 2020 3:53 PM

Answers

All replies

  • Hi,

    what is the error message that you receive?

    Sunday, August 2, 2020 8:21 PM
  • Hi JRDumont,

    In order to help us analyze your problem and make a test, please provide more information about what you want to do with PDFSharp and the error message you have got.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, August 6, 2020 8:05 AM
    Moderator
  • Hello,

    Thank you for your responses.

    I was able to resolve the issue.

    • Marked as answer by JRDumont Friday, August 7, 2020 2:31 AM
    Friday, August 7, 2020 12:01 AM