Author Topic: Displaying Shape Data in a pdf  (Read 799 times)

0 Members and 1 Guest are viewing this topic.

pahmann

  • Newbie
  • *
  • Posts: 6
Displaying Shape Data in a pdf
« on: October 01, 2018, 10:11:53 AM »
Is there a way to display shape data when converting a visio to PDF?

Yacine

  • Hero Member
  • *****
  • Posts: 2571
Re: Displaying Shape Data in a pdf
« Reply #1 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
Yacine

metuemre

  • Full Member
  • ***
  • Posts: 162
Re: Displaying Shape Data in a pdf
« Reply #2 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.
« Last Edit: October 02, 2018, 04:42:24 AM by metuemre »

Dusty_Rhodes

  • Jr. Member
  • **
  • Posts: 36
Re: Displaying Shape Data in a pdf
« Reply #3 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

Nikolay

  • Hero Member
  • *****
  • Posts: 782
    • UnmanagedVisio
Re: Displaying Shape Data in a pdf
« Reply #4 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.

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.
« Last Edit: February 20, 2019, 03:49:40 PM by Nikolay »

metuemre

  • Full Member
  • ***
  • Posts: 162
Re: Displaying Shape Data in a pdf
« Reply #5 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: [Select]
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

  • Hero Member
  • *****
  • Posts: 782
    • UnmanagedVisio
Re: Displaying Shape Data in a pdf
« Reply #6 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: [Select]
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.
« Last Edit: February 21, 2019, 05:47:18 AM by Nikolay »

Nikolay

  • Hero Member
  • *****
  • Posts: 782
    • UnmanagedVisio
Re: Displaying Shape Data in a pdf
« Reply #7 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: [Select]
$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();
« Last Edit: February 26, 2019, 01:06:14 PM by Nikolay »

robert

  • Newbie
  • *
  • Posts: 2
Re: Displaying Shape Data in a pdf
« Reply #8 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 ?
« Last Edit: March 04, 2019, 02:26:08 AM by robert »

Nikolay

  • Hero Member
  • *****
  • Posts: 782
    • UnmanagedVisio
Re: Displaying Shape Data in a pdf
« Reply #9 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.

steven15

  • Newbie
  • *
  • Posts: 1
Re: Displaying Shape Data in a pdf
« Reply #10 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 ?