Visio Guy

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

Title: Displaying Shape Data in a pdf
Post by: pahmann on October 01, 2018, 03:11:53 PM
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, 05: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, 09: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, 05: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, 07: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, 08: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.

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, 09: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


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, 05: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.


$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, 06: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, 09: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, 02:42:57 PM
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, 09: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 02, 2020, 02:46:54 AM
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)...
Title: Re: Displaying Shape Data in a pdf
Post by: TrinityIXP on May 25, 2021, 12:39:38 PM
Thank you so much Nikolay! This tool is fantastic! It did exactly what I needed!
Title: Re: Displaying Shape Data in a pdf
Post by: Nikolay on July 05, 2023, 01:00:44 PM
To append to this topic. Now there is a web-version of the tool, that works without Visio (all in the cloud).
Two files as input (original VSDX and PDF without tooltips), one file as output (PDF with tooltips from Visio file).
Files are not saved, processed only in-memory.

https://webtools.unmanagedvisio.com/pdftip

Source code:

https://github.com/nbelyh/visiopdftip-webapp
Title: Re: Displaying Shape Data in a pdf
Post by: Yacine on July 06, 2023, 04:31:50 AM
Wow!
Title: Re: Displaying Shape Data in a pdf
Post by: Surrogate on July 06, 2023, 07:02:14 AM
Quote from: Nikolay on July 05, 2023, 01:00:44 PM
Now there is a web-version of the tool, that works without Visio (all in the cloud).
For free, without SMS or registration ?
(https://cs4.pikabu.ru/post_img/2015/09/01/11/1441134445_628407477.jpg)
Title: Re: Displaying Shape Data in a pdf
Post by: Nikolay on July 06, 2023, 08:06:23 AM
It is not exactly the same as the Add-In because the web app only works for top-level shapes (when the tooltip position can be taken from shape position) because the app only parses Visio VSDX xml without calculations of sub-shapes.
If the comments are for top-level shapes only (which may cover 99% of the cases, I assume) then it should work fine, though.

So far, according to analytics, the site has been visited by 9 (nine) users, and one of them actually tried to process a diagram. That is, in one week. I would call that success :D