R
R
Roman Koff2016-02-29 14:03:48
ASP.NET
Roman Koff, 2016-02-29 14:03:48

How to get PDF from DOCX on asp.net* (c#)?

I looked through a bunch of sources and did not find a clear solution for this problem.
There is a DOCX file and it needs to be converted to PDF on the server (IIS, ASP.NET MVC).
To be more precise, there is a template in word that contains template fields that the server script replaces (for example, the NUMBER text in the template is replaced by 123). I carry out the replacement using the OpenXML SDK (there is no problem with this). Next, a new document created on the basis of the template must be converted to PDF format and given to the client.
Found a great PdfSharp library. She copes well with the tasks of generating PDF, but how can she feed an OpenXML document so that the formatting does not move out (or rather, it is preserved in principle).
Maybe there are other solutions to this problem?
Dopinfo:
- commercial products are not suitable;
- encryption in pdf is not needed;
- Microsoft Office Interop copes, but is not suitable (not intended for server solutions)
And one more thing: I'm not very good at postscript, but is it possible to use a PDF document as a template and change keywords to values ​​​​in it (and then generate a new PDF file)? If so, how?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
V Sh., 2016-02-29
@JuniorNoobie

Make a pdf file in the form you need and open it with any editor as xml. See what's there and how. Then just make the same xml file using C# and send it to the client indicating that it is a file with a pdf extension.

R
Roman Koff, 2016-02-29
@Zarinov

If you can correctly transfer the design from OpenXML to PdfSharp.Pdf.PdfDocument, then the problem will be solved. But I don't understand what needs to be moved where.
As an option, modifying the original PDF file using the same PdfSharp would also make life easier, but how?
NUGET: PDFsharp-MigraDoc-GDI

/// <summary>
/// Генерация нового PDF
/// </summary>
private void CreateDPF()
{
  string fileResult = HostingEnvironment.MapPath("/Files/result.pdf");
  var d = new Document();
  var s = d.AddSection();
  s.PageSetup.PageFormat = PageFormat.A4;
  s.PageSetup.Orientation = Orientation.Portrait;
  s.PageSetup.TopMargin = 10;
  s.PageSetup.LeftMargin = 10;
  s.PageSetup.BottomMargin = 10;
  s.PageSetup.RightMargin = 10;
  var p = s.AddParagraph();
  p.AddText("Превед медвед!");
  var r = new PdfDocumentRenderer(true, PdfFontEmbedding.Always);
  r.Document = d;
  r.RenderDocument();
  r.PdfDocument.Save(fileResult);
}

/// <summary>
/// Генерация PDF на основе другого PDF
/// </summary>
private void ConvertDPF()
{
  string fileSource = HostingEnvironment.MapPath("/Files/source.pdf");
  string fileResult = HostingEnvironment.MapPath("/Files/result.pdf");
  using (var src = PdfReader.Open(fileSource, PdfDocumentOpenMode.Import))
  using (var res = new PdfDocument())
  {
    foreach (PdfPage page in src.Pages)
    {
      // что-то нужно сделать с этой page
      res.AddPage(page);
    }
    res.Save(fileResult);
  }			
}

I
iseewhatyoudidthere, 2016-03-01
@iseewhatyoudidthere

You can also look towards iTextSharp
https://www.nuget.org/packages/iTextSharp/
With blackjack and whores.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question