Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add excel_fit_to_page_width flag #63

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .nuget/NuGet.exe
Binary file not shown.
59 changes: 33 additions & 26 deletions OfficeToPDF/ExcelConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class ExcelConverter: Converter
Boolean showFormulas = (Boolean)options["excel_show_formulas"];
Boolean isHidden = (Boolean)options["hidden"];
Boolean screenQuality = (Boolean)options["screen"];
Boolean fitToPageWidth = (Boolean)options["excel_fit_to_page_width"];
Boolean updateLinks = !(Boolean)options["excel_no_link_update"];
int maxRows = (int)options[@"excel_max_rows"];
int worksheetNum = (int)options["excel_worksheet"];
Expand Down Expand Up @@ -174,7 +175,6 @@ class ExcelConverter: Converter
{
workbook.RunAutoMacros(XlRunAutoMacro.xlAutoOpen);
}

// Get any template options
SetPageOptionsFromTemplate(app, workbooks, options, ref templatePageSetup);

Expand Down Expand Up @@ -224,7 +224,6 @@ class ExcelConverter: Converter
return (int)ExitCode.WorksheetNotFound;
}
}

if (showFormulas)
{
// Determine whether to show formulas
Expand All @@ -251,7 +250,6 @@ class ExcelConverter: Converter
activeWindow.Visible = false;
}
}

// Keep track of the active sheet
if (workbook.ActiveSheet != null)
{
Expand Down Expand Up @@ -288,7 +286,8 @@ class ExcelConverter: Converter
if (activeSheet is _Worksheet)
{
itemIndex = ((Worksheet)activeSheet).Index;
} else if (activeSheet is _Chart)
}
else if (activeSheet is _Chart)
{
itemIndex = ((Microsoft.Office.Interop.Excel.Chart)activeSheet).Index;
}
Expand Down Expand Up @@ -316,7 +315,6 @@ class ExcelConverter: Converter
{
pageSetup = ((Worksheet)ws).PageSetup;
pageSetup.PrintHeadings = true;

}
catch (Exception) { }
finally
Expand Down Expand Up @@ -410,6 +408,14 @@ class ExcelConverter: Converter

workbook.SaveAs(tmpFile, fmt, Type.Missing, Type.Missing, Type.Missing, false, XlSaveAsAccessMode.xlNoChange, Type.Missing, false, Type.Missing, Type.Missing, Type.Missing);

// Fit to to one page wide.
if (fitToPageWidth)
{
templatePageSetup["FitToPagesWide"] = 1;
templatePageSetup["FitToPagesTall"] = false;
templatePageSetup["Zoom"] = false;
}

if (onlyActiveSheet)
{
// Set up a delegate function for times we want to print
Expand Down Expand Up @@ -471,21 +477,20 @@ class ExcelConverter: Converter
{
workbook.PrintOutEx(ActivePrinter: printer, PrintToFile: true, PrToFileName: destination);
};
if (HasTemplateOption(options))

// Set up the template page setup options on all the worksheets
// in the workbook
var worksheets = workbook.Worksheets;
for (int wsIdx = 1; wsIdx <= worksheets.Count; wsIdx++)
{
// Set up the template page setup options on all the worksheets
// in the workbook
var worksheets = workbook.Worksheets;
for (int wsIdx = 1; wsIdx <= worksheets.Count; wsIdx++)
{
var ws = worksheets[wsIdx];
var wps = (ws is _Worksheet) ? ((_Worksheet)ws).PageSetup : ((_Chart)ws).PageSetup;
SetPageSetupProperties(templatePageSetup, wps);
ReleaseCOMObject(wps);
ReleaseCOMObject(ws);
}
ReleaseCOMObject(worksheets);
var ws = worksheets[wsIdx];
var wps = (ws is _Worksheet) ? ((_Worksheet)ws).PageSetup : ((_Chart)ws).PageSetup;
SetPageSetupProperties(templatePageSetup, wps);
ReleaseCOMObject(wps);
ReleaseCOMObject(ws);
}
ReleaseCOMObject(worksheets);

if (String.IsNullOrEmpty((string)options["printer"]))
{
try
Expand Down Expand Up @@ -666,18 +671,20 @@ protected static void SetPageSetupProperties(Hashtable tps, PageSetup wps)
{
return;
}

var wpsType = wps.GetType();
for (int i = 0; i < templateProperties.Length; i++)
{
object[] value = { tps[templateProperties[i]] };
try
if (tps.ContainsKey(templateProperties[i]))
{
wpsType.InvokeMember(templateProperties[i], System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.SetProperty, Type.DefaultBinder, wps, value);
}
catch(Exception)
{
Console.WriteLine("Unable to set property {0}", templateProperties[i]);
object[] value = { tps[templateProperties[i]] };
try
{
wpsType.InvokeMember(templateProperties[i], System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.SetProperty, Type.DefaultBinder, wps, value);
}
catch (Exception)
{
Console.WriteLine("Unable to set property {0}", templateProperties[i]);
}
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions OfficeToPDF/OfficeToPDF.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\Costura.Fody.3.3.3\build\Costura.Fody.props" Condition="Exists('..\packages\Costura.Fody.3.3.3\build\Costura.Fody.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand All @@ -11,7 +11,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>OfficeToPDF</RootNamespace>
<AssemblyName>OfficeToPDF</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
Expand Down Expand Up @@ -48,6 +48,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
Expand All @@ -57,6 +58,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -67,6 +69,7 @@
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisIgnoreBuiltInRuleSets>false</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisFailOnMissingRules>false</CodeAnalysisFailOnMissingRules>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
Expand All @@ -78,6 +81,7 @@
<CodeAnalysisIgnoreBuiltInRuleSets>false</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisIgnoreBuiltInRules>false</CodeAnalysisIgnoreBuiltInRules>
<CodeAnalysisFailOnMissingRules>false</CodeAnalysisFailOnMissingRules>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release-signed|x86'">
<OutputPath>bin\x86\Release-signed\</OutputPath>
Expand All @@ -89,6 +93,7 @@
<CodeAnalysisIgnoreBuiltInRuleSets>false</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisIgnoreBuiltInRules>false</CodeAnalysisIgnoreBuiltInRules>
<CodeAnalysisFailOnMissingRules>false</CodeAnalysisFailOnMissingRules>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release-signed|x64'">
<OutputPath>bin\x64\Release-signed\</OutputPath>
Expand All @@ -100,6 +105,7 @@
<CodeAnalysisIgnoreBuiltInRuleSets>false</CodeAnalysisIgnoreBuiltInRuleSets>
<CodeAnalysisIgnoreBuiltInRules>false</CodeAnalysisIgnoreBuiltInRules>
<CodeAnalysisFailOnMissingRules>false</CodeAnalysisFailOnMissingRules>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Costura, Version=3.3.3.0, Culture=neutral, PublicKeyToken=9919ef960d84173d, processorArchitecture=MSIL">
Expand Down
3 changes: 2 additions & 1 deletion OfficeToPDF/PowerpointConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class PowerpointConverter : Converter
// sneaky method of getting the presentation - create an empty presentation
// and insert the slides from the original file.
Fonts fonts = null;
Boolean bitmapMissingFonts = !(Boolean)options["powerpoint_ref_fonts"];
try
{
fonts = activePresentation.Fonts;
Expand Down Expand Up @@ -195,7 +196,7 @@ class PowerpointConverter : Converter
{
try
{
activePresentation.ExportAsFixedFormat(outputFile, PpFixedFormatType.ppFixedFormatTypePDF, quality, MSCore.MsoTriState.msoFalse, PpPrintHandoutOrder.ppPrintHandoutVerticalFirst, printType, MSCore.MsoTriState.msoFalse, null, PpPrintRangeType.ppPrintAll, "", includeProps, true, includeTags, true, pdfa, Type.Missing);
activePresentation.ExportAsFixedFormat(outputFile, PpFixedFormatType.ppFixedFormatTypePDF, quality, MSCore.MsoTriState.msoFalse, PpPrintHandoutOrder.ppPrintHandoutVerticalFirst, printType, MSCore.MsoTriState.msoFalse, null, PpPrintRangeType.ppPrintAll, "", includeProps, true, includeTags, bitmapMissingFonts, pdfa, Type.Missing);
}
catch (Exception) {
if (!String.IsNullOrEmpty((string)options["fallback_printer"])) {
Expand Down
10 changes: 8 additions & 2 deletions OfficeToPDF/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ static void Main(string[] args)
options["excel_active_sheet"] = false;
options["excel_no_link_update"] = false;
options["excel_no_recalculate"] = false;
options["excel_fit_to_page_width"] = false;
options["excel_max_rows"] = (int) 0;
options["excel_worksheet"] = (int) 0;
options["excel_delay"] = (int) 0;
Expand All @@ -129,6 +130,8 @@ static void Main(string[] args)
options["original_filename"] = "";
options["original_basename"] = "";
options["powerpoint_output"] = "";
options["powerpoint_ref_fonts"] = false;
options["pdf_page_mode"] = null;
options["pdf_page_mode"] = null;
options["pdf_layout"] = null;
options["pdf_merge"] = (int) MergeMode.None;
Expand Down Expand Up @@ -162,7 +165,7 @@ static void Main(string[] args)
{ "excel_delay", "Excel delay milliseconds" }
};

Regex switches = new Regex(@"^/(version|hidden|markup|readonly|bookmarks|merge|noquit|print|(fallback_)?printer|screen|pdfa|template|writepassword|password|help|verbose|exclude(props|tags)|excel_(delay|max_rows|show_formulas|show_headings|auto_macros|template_macros|active_sheet|worksheet|no_recalculate|no_link_update)|powerpoint_(output)|word_(show_hidden|header_dist|footer_dist|ref_fonts|no_field_update|field_quick_update(_safe)?|max_pages|keep_history|no_repair|fix_table_columns|show_(comments|revs_comments|format_changes|ink_annot|ins_del|all_markup)|markup_balloon)|pdf_(page_mode|append|prepend|layout|clean_meta|owner_pass|user_pass|restrict_(annotation|extraction|assembly|forms|modify|print|accessibility_extraction|full_quality))|working_dir|\?)$", RegexOptions.IgnoreCase);
Regex switches = new Regex(@"^/(version|hidden|markup|readonly|bookmarks|merge|noquit|print|(fallback_)?printer|screen|pdfa|template|writepassword|password|help|verbose|exclude(props|tags)|excel_(delay|max_rows|show_formulas|show_headings|auto_macros|template_macros|active_sheet|worksheet|no_recalculate|no_link_update|fit_to_page_width)|powerpoint_(output|ref_fonts)|word_(show_hidden|header_dist|footer_dist|ref_fonts|no_field_update|field_quick_update(_safe)?|max_pages|keep_history|no_repair|fix_table_columns|show_(comments|revs_comments|format_changes|ink_annot|ins_del|all_markup)|markup_balloon)|pdf_(page_mode|append|prepend|layout|clean_meta|owner_pass|user_pass|restrict_(annotation|extraction|assembly|forms|modify|print|accessibility_extraction|full_quality))|working_dir|\?)$", RegexOptions.IgnoreCase);
for (int argIdx = 0; argIdx < args.Length; argIdx++)
{
string item = args[argIdx];
Expand Down Expand Up @@ -1074,11 +1077,14 @@ page settings from the first worksheet in the template
the file. Applies when converting with Excel.
/excel_no_link_update - Do not update links when opening Excel files.
/excel_no_recalculate - Skip automatic re-calculation of formulas in the workbook.
/excel_fit_to_page_width - Scale each page to fit on one page wide.
/excel_template_macros - Run Auto_Open macros in the /template document before conversion by Excel
/excel_worksheet <num> - Only convert worksheet <num> in the workbook. First sheet is 1
/powerpoint_output <type> - Controls what is generated by output. Possible values are slides, notes,
outline, build_slides, handouts and multi-page handouts using handout2,
handout3, handout4, handout6 and handout9. The default is slides.
handout3, handout4, handout6 and handout9. The default is slides.
/powerpoint_ref_fonts - When fonts are not available, a reference to the font is used in
the generated PDF rather than a bitmapped version. The default is
/word_header_dist <pts> - The distance (in points) from the header to the top of
the page.
/word_footer_dist <pts> - The distance (in points) from the footer to the bottom
Expand Down
4 changes: 2 additions & 2 deletions OfficeToPDF/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,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.9.0.3")]
[assembly: AssemblyFileVersion("1.9.0.3")]
[assembly: AssemblyVersion("1.9.2.0")]
[assembly: AssemblyFileVersion("1.9.2.0")]
1 change: 1 addition & 0 deletions OfficeToPDF/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ The following optional switches can be used:
the file. Applies when converting with Excel
/excel_no_link_update - do not update links when opening Excel files
/excel_no_recalculate - skip automatic re-calculation of formulas in the workbook
/excel_fit_to_page_width - Scale each page to fit on one page wide.
/excel_template_macros - run Auto_Open macros in the /template document before conversion by Excel
/excel_worksheet <num> - only convert worksheet <num> in the workbook. First sheet is 1
/fallback_printer <name> - prints the image to the postscript printer with name <name> if the export failes - requires GhostScript
Expand Down
8 changes: 4 additions & 4 deletions OfficeToPDF/app.config
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="PdfSharp-WPF" publicKeyToken="f94615aa0424f9eb" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.31.4604.0" newVersion="1.31.4604.0" />
<assemblyIdentity name="PdfSharp-WPF" publicKeyToken="f94615aa0424f9eb" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.31.4604.0" newVersion="1.31.4604.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup>
</configuration>
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ The following optional switches can be used:
| /excel_auto_macros | run Auto_Open macros in Excel files before conversion |
| /excel_no_link_update | do not update links when opening Excel files |
| /excel_no_recalculate | skip automatic re-calculation of formulas in the workbook |
| /excel_fit_to_page_width | Scale each page to fit on one page wide |
| /word_header_dist _pts_ | the distance (in points) from the header to the top of the page |
| /word_footer_dist _pts_ | the distance (in points) from the footer to the bottom of the page |
| /word_field_quick_update | perform a fast update of fields in Word before conversion |
Expand Down
8 changes: 8 additions & 0 deletions docs/Documentation.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ <h3>Command line switches</h3>
<td>skip automatic re-calculation of formulas in the workbook</td>
</tr>
<tr>
<td>/excel_fit_to_page_width</td>
<td>Scale each page to fit on one page wide.</td>
</tr>
<tr>
<td>/word_header_dist <em>&lt;pts&gt;</em></td>
<td>the distance (in points) from the header to the top of the page</td>
</tr>
Expand Down Expand Up @@ -153,6 +157,10 @@ <h3>Command line switches</h3>
<td>when fonts are not available, a reference to the font is used in the generated PDF rather than a bitmapped version. The default is for a bitmap of the text to be used</td>
</tr>
<tr>
<td>/powerpoint_ref_fonts</td>
<td>when fonts are not available, a reference to the font is used in the generated PDF rather than a bitmapped version. The default is for a bitmap of the text to be used</td>
</tr>
<tr>
<td>/pdf_clean_meta <em>&lt;type&gt;</em></td>
<td>allows for some meta-data to be removed from the generated PDF.<br>
<em>&lt;type&gt;</em> can be:
Expand Down