Skip to content

Commit

Permalink
catch potential issues when modifying the column widths (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
vittala committed Jan 10, 2018
1 parent 685d918 commit 5f015c0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 78 deletions.
4 changes: 2 additions & 2 deletions OfficeToPDF/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.8.5.0")]
[assembly: AssemblyFileVersion("1.8.5.0")]
[assembly: AssemblyVersion("1.8.6.0")]
[assembly: AssemblyFileVersion("1.8.6.0")]
159 changes: 83 additions & 76 deletions OfficeToPDF/WordConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class WordConverter: Converter
return (int)ExitCode.FileOpenFailure;
}
doc.Activate();

// Check if there are too many pages
if (maxPages > 0)
{
Expand Down Expand Up @@ -296,7 +296,6 @@ class WordConverter: Converter
}
}


// See if we have to update fields
if (!(Boolean)options["word_no_field_update"])
{
Expand All @@ -319,6 +318,7 @@ class WordConverter: Converter
doc.Save();
}
doc.Saved = true;

doc.ExportAsFixedFormat(outputFile, WdExportFormat.wdExportFormatPDF, false,
quality, WdExportRange.wdExportAllDocument,
1, 1, showMarkup, includeProps, true, bookmarks, includeTags, bitmapMissingFonts, pdfa);
Expand Down Expand Up @@ -428,97 +428,104 @@ private static void updateDocumentFields(Document doc, Microsoft.Office.Interop.
return;
}

// Update some of the field types in the document so the printed
// PDF looks correct. Skips some field types (such as ASK) that would
// create dialogs
foreach (Microsoft.Office.Interop.Word.Section section in doc.Sections)
try
{
var sectionRange = section.Range;
var sectionFields = sectionRange.Fields;
var zeroHeader = true;
var zeroFooter = true;

foreach (Field sectionField in sectionFields)
// Update some of the field types in the document so the printed
// PDF looks correct. Skips some field types (such as ASK) that would
// create dialogs
foreach (Microsoft.Office.Interop.Word.Section section in doc.Sections)
{
WordConverter.updateField(sectionField, word, inputFile);
}
var sectionRange = section.Range;
var sectionFields = sectionRange.Fields;
var zeroHeader = true;
var zeroFooter = true;

var sectionPageSetup = section.PageSetup;
var headers = section.Headers;
foreach (Microsoft.Office.Interop.Word.HeaderFooter header in headers)
{
if (header.Exists)
foreach (Field sectionField in sectionFields)
{
var range = header.Range;
var rangeFields = range.Fields;
foreach (Field rangeField in rangeFields)
WordConverter.updateField(sectionField, word, inputFile);
}

var sectionPageSetup = section.PageSetup;
var headers = section.Headers;
foreach (Microsoft.Office.Interop.Word.HeaderFooter header in headers)
{
if (header.Exists)
{
WordConverter.updateField(rangeField, word, inputFile);
var range = header.Range;
var rangeFields = range.Fields;
foreach (Field rangeField in rangeFields)
{
WordConverter.updateField(rangeField, word, inputFile);
}
// Simply querying the range of the header will create it.
// If the header is empty, this can introduce additional space
// between the non-existant header and the top of the page.
// To counter this for empty headers, we manually set the header
// distance to zero here
var shapes = header.Shapes;
var rangeShapes = range.ShapeRange;
if ((shapes.Count > 0) || !String.IsNullOrWhiteSpace(range.Text) || (rangeShapes.Count > 0))
{
zeroHeader = false;
}
Converter.releaseCOMObject(shapes);
Converter.releaseCOMObject(rangeShapes);
Converter.releaseCOMObject(rangeFields);
Converter.releaseCOMObject(range);
}
// Simply querying the range of the header will create it.
// If the header is empty, this can introduce additional space
// between the non-existant header and the top of the page.
// To counter this for empty headers, we manually set the header
// distance to zero here
var shapes = header.Shapes;
var rangeShapes = range.ShapeRange;
if ((shapes.Count > 0) || !String.IsNullOrWhiteSpace(range.Text) || (rangeShapes.Count > 0))
}

var footers = section.Footers;
foreach (Microsoft.Office.Interop.Word.HeaderFooter footer in footers)
{
if (footer.Exists)
{
zeroHeader = false;
var range = footer.Range;
var rangeFields = range.Fields;
foreach (Field rangeField in rangeFields)
{
WordConverter.updateField(rangeField, word, inputFile);
}
// Simply querying the range of the footer will create it.
// If the footer is empty, this can introduce additional space
// between the non-existant footer and the bottom of the page.
// To counter this for empty footers, we manually set the footer
// distance to zero here
var shapes = footer.Shapes;
var rangeShapes = range.ShapeRange;
if (shapes.Count > 0 || !String.IsNullOrWhiteSpace(range.Text) || rangeShapes.Count > 0)
{
zeroFooter = false;
}
Converter.releaseCOMObject(shapes);
Converter.releaseCOMObject(rangeShapes);
Converter.releaseCOMObject(rangeFields);
Converter.releaseCOMObject(range);
}
Converter.releaseCOMObject(shapes);
Converter.releaseCOMObject(rangeShapes);
Converter.releaseCOMObject(rangeFields);
Converter.releaseCOMObject(range);
}
}

var footers = section.Footers;
foreach (Microsoft.Office.Interop.Word.HeaderFooter footer in footers)
{
if (footer.Exists)
if (doc.ProtectionType == WdProtectionType.wdNoProtection)
{
var range = footer.Range;
var rangeFields = range.Fields;
foreach (Field rangeField in rangeFields)
if (zeroHeader)
{
WordConverter.updateField(rangeField, word, inputFile);
sectionPageSetup.HeaderDistance = 0;
}
// Simply querying the range of the footer will create it.
// If the footer is empty, this can introduce additional space
// between the non-existant footer and the bottom of the page.
// To counter this for empty footers, we manually set the footer
// distance to zero here
var shapes = footer.Shapes;
var rangeShapes = range.ShapeRange;
if (shapes.Count > 0 || !String.IsNullOrWhiteSpace(range.Text) || rangeShapes.Count > 0)
if (zeroFooter)
{
zeroFooter = false;
sectionPageSetup.FooterDistance = 0;
}
Converter.releaseCOMObject(shapes);
Converter.releaseCOMObject(rangeShapes);
Converter.releaseCOMObject(rangeFields);
Converter.releaseCOMObject(range);
}
}
if (doc.ProtectionType == WdProtectionType.wdNoProtection)
{
if (zeroHeader)
{
sectionPageSetup.HeaderDistance = 0;
}
if (zeroFooter)
{
sectionPageSetup.FooterDistance = 0;
}
Converter.releaseCOMObject(sectionFields);
Converter.releaseCOMObject(sectionRange);
Converter.releaseCOMObject(headers);
Converter.releaseCOMObject(footers);
Converter.releaseCOMObject(sectionPageSetup);
}
Converter.releaseCOMObject(sectionFields);
Converter.releaseCOMObject(sectionRange);
Converter.releaseCOMObject(headers);
Converter.releaseCOMObject(footers);
Converter.releaseCOMObject(sectionPageSetup);
}

catch (COMException)
{
// There can be odd errors when column widths are out of the page boundaries
// See github issue #14
}
var docFields = doc.Fields;
foreach (Field docField in docFields)
{
Expand Down

0 comments on commit 5f015c0

Please sign in to comment.