Visio Guy

Visio Discussions => General Visio => Topic started by: pahmann on October 01, 2018, 10:11:53 AM

Title: Displaying Shape Data in a pdf
Post by: pahmann on October 01, 2018, 10:11:53 AM
Is there a way to display shape data when converting a visio to PDF?
Title: Re: Displaying Shape Data in a pdf
Post by: Yacine on October 01, 2018, 12:24:47 PM
Not that much in a PDF, but in an HTML file:
http://visguy.com/vgforum/index.php?topic=7642.0
Title: Re: Displaying Shape Data in a pdf
Post by: metuemre on October 02, 2018, 04:39:47 AM
It's possible if you have Acrobat Standard installed on your computer. Attached is a sample visio file with a macro in it. Macro will save the visio file as pdf in the same directory, then it will read all shape data for each shape on each page and it will write them to comment boxes on the pdf and put them on shape locations.

My test environmet is Visio 2013 Pro, Acrobat XI Standard and visio drawing is in metric units.

Feel free to ask if you face any problems.
Title: Re: Displaying Shape Data in a pdf
Post by: Dusty_Rhodes on February 20, 2019, 12:38:54 PM
Hello, metuemre your attachment is exactly what i have been looking for. This is what i want to do to my pdf files. I cant get it to work nothing happends when i expoert the file as a pdf
Title: Re: Displaying Shape Data in a pdf
Post by: Nikolay on February 20, 2019, 02:22:27 PM
Hmm. The annotations idea looks great. But you don't really need Acrobat to work the PDF.
There are a lot of libraries wot work with it that support annotations, for example, open-source PdfSharp (http://www.pdfsharp.com/PDFsharp/).

I have some time today, will try to rewrite the macro in a form of extension!

---
After giving it a second thought, that didn't look like an 1-hour thing.
I think it could be easier to go with powershell and use either pdfSharp or iTextSharp dlls.
Title: Re: Displaying Shape Data in a pdf
Post by: metuemre on February 21, 2019, 03:47:49 AM
Nikolay, PdfSharp looks nice. There were some Uppercase/Lowercase variable name issues in the source code. I modified them to create a proper .tlb file. Then I referenced it in Visio and it works. I attached necessary type library files if anyone needs them. Below is a sample code to create an empty pdf file with a single page.

Code
Sub EmptyPdf()
    Dim PdfDoc As PdfDocument
   
    Set PdfDoc = New PdfDocument
    PdfDoc.Info.Title = "Created with PDFsharp"
   
    Dim Pdfpag As PdfPage
    Set Pdfpag = PdfDoc.AddPage
   
    Dim filename As String
   
    filename = "HelloWorld.pdf"
    PdfDoc.Save (filename)
End Sub
Title: Re: Displaying Shape Data in a pdf
Post by: Nikolay on February 21, 2019, 04:11:28 AM
Coordinate transformation should be easy.
Visio "internal units" are inches, in PDF(sharp) they are points, origin is the same (lower left corner)
So Visio x * 72, y * 72 should give PDF x, y coordinates.

To place the comment (screen tip) annotation to top-left corner of a shape with PdfSharp:
http://www.pdfsharp.net/wiki/Annotations-sample.ashx

Code
foreach (Shape shape in visioPage.Shapes)
{
  var title = shape.Text;
  var contents = shape.Cells["Comment"].ResultStr[0];
  if (string.IsNullOrEmpty(comment))
    continue;

  double x1, y1, x2, y2;
  shape.BoundingRect(1, out x1, out y1, out x2, out y2);

  pdfPage.Annotations.Add(new PdfTextAnnotation
  {
    Icon = PdfTextAnnotationIcon.Note,
    Title = title,
    Contents = contents,
    Rectangle = new PdfRectangle(new XRect(new XPoint(x1 * 72, y1 * 72), new XSize(0, 0)))
  });
}

Disclaimer: I did not try to compile the code above; hope it works or at least gives an idea how to proceed.
Title: Re: Displaying Shape Data in a pdf
Post by: Nikolay on February 26, 2019, 12:59:49 PM
Below is a powershell script to add annotations using the technique discussed above (PdfSharp), and a demo/test attached.
Now it does not seem to require anything extra basically - can be just run from a command line (right-click, "run with powershell")

Although the powershell syntax to load and use a .net assembly is a bit awkward, it does not need the type libraries, and can be run from a command line (for batch processing).
Could be probably nice to have this as an option in PDF export settings dialog, does not really look any complicated.

The properties can be added simply by modifying the script and building the "content", or by making the "Comment" take values from the properties.

I think rich text (in particular, text with pictures in the pdf tooltips/annoations) can be also theoretically possible; but looks more troublesome, PdfSharp has no direct support for formatted text in annotation, from what I see. But, one could write raw pdf data using it, for example.

If the code gives a security error, make sure you clicked "Unlock" in zip file properties before unzipping it.

Code
$visioFileName = $PSScriptRoot + "\demo\PdfNote.vsd"
$pdfFileName = $PSScriptRoot + "\demo\PdfNote.pdf"

Write-Host converting $visioFileName to $pdfFileName

# Load PdfSharp assembly
Add-Type -Path (Get-ChildItem -Filter PdfSharp.dll -Recurse $PSScriptRoot).FullName

# start Visio and open the document

$visio = New-Object -ComObject Visio.Application
$visio.Visible = $true
$visioDoc = $visio.Documents.Open($visioFileName)

# Export PDF, all pages
$visioDoc.ExportAsFixedFormat(1, $pdfFileName, 1, 0)

# Open exported PDF with PdfSharp
$pdfDoc = [PdfSharp.Pdf.IO.PdfReader]::Open($pdfFileName);

for ($i = 0; $i -lt $pdfDoc.PageCount; $i = $i + 1) {

    $pdfPage = $pdfDoc.Pages[$i]
    $visioPage = $visioDoc.Pages[1 + $i]

    foreach ($visioShape in $visioPage.Shapes) {

        # if comment exists
        $comment = $visioShape.CellsU("Comment").ResultStr(0)
        if ($comment) {

            # add it as annotation
            $x = $visioShape.CellsU("PinX").ResultIU - $visioShape.CellsU("Width").ResultIU / 2
            $y = $visioShape.CellsU("PinY").ResultIU + $visioShape.CellsU("Height").ResultIU / 2

            $annotation = New-Object PdfSharp.Pdf.Annotations.PdfTextAnnotation
            $annotation.Title = $visioShape.Characters.Text
            $annotation.Contents = $comment
            $annotation.Icon = 6 # "note" icon

            # inches to points
            $point = New-Object PdfSharp.Drawing.XPoint(($x * 72), ($y * 72))
            $size = New-Object PdfSharp.Drawing.XSize(0, 0)
            $rect = New-Object PdfSharp.Drawing.XRect($point, $size)
            $annotation.Rectangle = New-Object PdfSharp.Pdf.PdfRectangle($rect)
           
            $pdfPage.Annotations.Add($annotation)
        }
    }
}

$pdfDoc.Save($pdfFileName);
$visio.Quit();
Title: Re: Displaying Shape Data in a pdf
Post by: robert on March 04, 2019, 01:57:44 AM
Hello this looks great if it works. I have a a few office layout drawings this could be great for. But I can't get past the first step of installing PDF sharp. Am I just dumb or is it a complicated installation ?
Title: Re: Displaying Shape Data in a pdf
Post by: Nikolay on March 04, 2019, 04:31:26 AM
Hi Robert, there is nothing to install.. the latest (at the time of writing) PdfSharp.DLL is included in the ZIP archive (my post), it's right next to the .ps1 script.
The PdfSharp.dll is used directly from the powershell script.

If you really want to install it to your project, you could use nuget to get the latest version from the online repository.
Title: Re: Displaying Shape Data in a pdf
Post by: steven15 on March 05, 2019, 09:42:57 AM
good day to you all. Found this thread and needed to create a profile to ask. Would love to implement this on my drawings. I am new to using microsoft applications so i was wondering where do i store the ps1 script. and the Dll file to get this to work on my ssystem Nikolay ? and is there anything else i need or is it just Visio and these zip files placed in the right location ? No VBA code needed in the document ?
Title: Re: Displaying Shape Data in a pdf
Post by: Nikolay on November 20, 2019, 04:06:19 PM
Created a small extension to help with this (add tooltips to PDF export). I.e. it basically it wraps what is written here as a button next to the standard PDF export ;D
Do you think it can be useful / needed?

https://www.youtube.com/watch?v=y015BILE7zg
Title: Re: Displaying Shape Data in a pdf
Post by: Nikolay on March 01, 2020, 09:46:54 PM
Build a proper extension with setup:
http://unmanagedvisio.com/products/visio-pdf-export-with-tooltips/

Any feedback is appreciated!
Like usual, free for personal use, inexpensive for organizations ($20) :)

Please beware of new code-signing certificate (says "not commonly downloaded" when you try to download that in Chrome)...