Displaying Shape Data in a pdf

Started by pahmann, October 01, 2018, 03:11:53 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

pahmann

Is there a way to display shape data when converting a visio to PDF?

Yacine

Yacine

metuemre

#2
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.

Dusty_Rhodes

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

Nikolay

#4
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.

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.

metuemre

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

Nikolay

#6
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.

Nikolay

#7
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();


robert

#8
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 ?

Nikolay

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.

steven15

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 ?

Nikolay

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

Nikolay

#12
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)...

TrinityIXP

Thank you so much Nikolay! This tool is fantastic! It did exactly what I needed!

Nikolay

#14
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