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 option to process docx files in compatibility mode #17

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
4 changes: 2 additions & 2 deletions DocXToPdfConverter/DocXToPdfConverter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<ApplicationIcon />
<StartupObject />
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand All @@ -18,7 +18,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="2.10.0-beta0002" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.15.0" />
<PackageReference Include="System.Drawing.Common" Version="4.5.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,6 @@ public static void Convert(string inputFile, string outputFile, string libreOffi
procStartInfo.WorkingDirectory = Environment.CurrentDirectory;

var process = new Process() { StartInfo = procStartInfo };
Process[] pname = Process.GetProcessesByName("soffice");

//Supposedly, only one instance of Libre Office can be run simultaneously
while (pname.Length > 0)
{
Thread.Sleep(5000);
pname = Process.GetProcessesByName("soffice");
}

process.Start();
process.WaitForExit();
Expand Down
20 changes: 13 additions & 7 deletions DocXToPdfConverter/DocXToPdfHandlers/DocXHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@ public class DocXHandler
private readonly MemoryStream _docxMs;
private readonly Placeholders _rep;
private int _imageCounter;
private readonly OpenSettings _openSettings = new OpenSettings() { AutoSave = true };

public DocXHandler(string docXTemplateFilename, Placeholders rep)

public DocXHandler(string docXTemplateFilename, Placeholders rep, bool office2007Compatible)
{
_docxMs = StreamHandler.GetFileAsMemoryStream(docXTemplateFilename);
_rep = rep;

if (office2007Compatible)
{
_openSettings.MarkupCompatibilityProcessSettings = new MarkupCompatibilityProcessSettings(MarkupCompatibilityProcessMode.ProcessLoadedPartsOnly, FileFormatVersions.Office2007);
}
}


Expand Down Expand Up @@ -58,7 +63,7 @@ public MemoryStream ReplaceTexts()
if (_rep.TextPlaceholders == null || _rep.TextPlaceholders.Count == 0)
return null;

using (var doc = WordprocessingDocument.Open(_docxMs, true))
using (var doc = WordprocessingDocument.Open(_docxMs, true, _openSettings))
{
CleanMarkup(doc);

Expand Down Expand Up @@ -121,7 +126,7 @@ public MemoryStream ReplaceHyperlinks()
if (_rep.HyperlinkPlaceholders == null || _rep.HyperlinkPlaceholders.Count == 0)
return null;

using (var doc = WordprocessingDocument.Open(_docxMs, true))
using (var doc = WordprocessingDocument.Open(_docxMs, true, _openSettings))
{
CleanMarkup(doc);

Expand Down Expand Up @@ -206,7 +211,7 @@ public MemoryStream ReplaceTableRows()
if (_rep.TablePlaceholders == null || _rep.TablePlaceholders.Count == 0)
return null;

using (var doc = WordprocessingDocument.Open(_docxMs, true))
using (var doc = WordprocessingDocument.Open(_docxMs, true, _openSettings))
{
CleanMarkup(doc);

Expand Down Expand Up @@ -302,7 +307,7 @@ public MemoryStream ReplaceImages()
if (_rep.ImagePlaceholders == null || _rep.ImagePlaceholders.Count == 0)
return null;

using (var doc = WordprocessingDocument.Open(_docxMs, true))
using (var doc = WordprocessingDocument.Open(_docxMs, true, _openSettings))
{
CleanMarkup(doc);

Expand Down Expand Up @@ -426,7 +431,8 @@ private Drawing GetImageElement(
new DW.EffectExtent { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
new DW.DocProperties { Id = (UInt32Value)1U, Name = pictureName + _imageCounter },
new DW.NonVisualGraphicFrameDrawingProperties(
new A.GraphicFrameLocks { NoChangeAspect = true }),
new A.GraphicFrameLocks { NoChangeAspect = true }
),
new A.Graphic(
new A.GraphicData(
new PIC.Picture(
Expand Down
21 changes: 14 additions & 7 deletions DocXToPdfConverter/ReportGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ public class ReportGenerator
{
private readonly string _locationOfLibreOfficeSoffice;

// If you dont need conversion to PDF, you can leave the LocationOfLibreOfficeSoffice empty
// For Windows users: this must point to the ".exe" file, so \Path\Path\soffice.exe
public ReportGenerator(string locationOfLibreOfficeSoffice = "")
private readonly bool _processOffice2007Compatible;


/// <summary>
/// ReportGenerator is the main class to convert documents
/// </summary>
/// <param name="locationOfLibreOfficeSoffice">If you dont need conversion to PDF, you can leave the LocationOfLibreOfficeSoffice empty. For Windows users: this must point to the ".exe" file, so \Path\Path\soffice.exe</param>
/// <param name="processOffice2007Compatible">If set to true, then docx documents are generated in a Office2007 compatible way. Default is false, then no processing is done.</param>
public ReportGenerator(string locationOfLibreOfficeSoffice = "", bool processOffice2007Compatible = false)
{
_locationOfLibreOfficeSoffice = locationOfLibreOfficeSoffice;
_processOffice2007Compatible = processOffice2007Compatible;
}

public void Convert(string inputFile, string outputFile, Placeholders rep = null)
Expand Down Expand Up @@ -96,7 +103,7 @@ public void Print(string templateFile, string printerName = null, Placeholders r

private void PrintDocx(string templateFile, string printername, Placeholders rep)
{
var docx = new DocXHandler(templateFile, rep);
var docx = new DocXHandler(templateFile, rep, _processOffice2007Compatible);
var ms = docx.ReplaceAll();
var tempFileToPrint = Path.ChangeExtension(Path.GetTempFileName(), ".docx");
StreamHandler.WriteMemoryStreamToDisk(ms, tempFileToPrint);
Expand All @@ -119,7 +126,7 @@ private void PrintHtml(string templateFile, string printername, Placeholders rep
//string docxSource = filename with path
private void GenerateReportFromDocxToDocX(string docxSource, string docxTarget, Placeholders rep)
{
var docx = new DocXHandler(docxSource, rep);
var docx = new DocXHandler(docxSource, rep, _processOffice2007Compatible);
var ms = docx.ReplaceAll();
StreamHandler.WriteMemoryStreamToDisk(ms, docxTarget);
}
Expand All @@ -128,7 +135,7 @@ private void GenerateReportFromDocxToDocX(string docxSource, string docxTarget,
////string docxSource = filename with path
private void GenerateReportFromDocxToPdf(string docxSource, string pdfTarget, Placeholders rep)
{
var docx = new DocXHandler(docxSource, rep);
var docx = new DocXHandler(docxSource, rep, _processOffice2007Compatible);
var ms = docx.ReplaceAll();
var tmpFile = Path.Combine(Path.GetDirectoryName(pdfTarget), Path.GetFileNameWithoutExtension(pdfTarget) + Guid.NewGuid().ToString().Substring(0, 10) + ".docx");
StreamHandler.WriteMemoryStreamToDisk(ms, tmpFile);
Expand All @@ -139,7 +146,7 @@ private void GenerateReportFromDocxToPdf(string docxSource, string pdfTarget, Pl

private void GenerateReportFromDocxToHtml(string docxSource, string htmlTarget, Placeholders rep)
{
var docx = new DocXHandler(docxSource, rep);
var docx = new DocXHandler(docxSource, rep, _processOffice2007Compatible);
var ms = docx.ReplaceAll();
var tmpFile = Path.Combine(Path.GetDirectoryName(htmlTarget), Path.GetFileNameWithoutExtension(docxSource) + Guid.NewGuid().ToString().Substring(0, 10) + ".docx");
StreamHandler.WriteMemoryStreamToDisk(ms, tmpFile);
Expand Down
2 changes: 1 addition & 1 deletion ExampleApplication/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static void Main(string[] args)

//Most important: give the full path to the soffice.exe file including soffice.exe.
//Don't know how that would be named on Linux...
var test = new ReportGenerator(locationOfLibreOfficeSoffice);
var test = new ReportGenerator(locationOfLibreOfficeSoffice, processOffice2007Compatible: true);

//Convert from HTML to HTML
test.Convert(htmlLocation, Path.Combine(Path.GetDirectoryName(htmlLocation), "Test-HTML-page-out.html"), placeholders);
Expand Down