Printing from a .doc template

VentureFree

Well-known member
Joined
Jan 9, 2008
Messages
54
Programming Experience
5-10
I'm writing a program which will be printing a bunch of individual pages, all of which have a common content except for 4 lines, which have personalized info (name, address, etc...). Those 4 lines will be very precisely placed upon the page before printing. What I would like to do is have a Word document that contains the common parts, and programmatically add the personalized info at run time to be printed. That way if the common content needed to be changed in the future, simply changing the Word document would suffice, and no code changes would be needed.

I've never actually had to print anything from code, which is quite amazing considering how long I've been coding (it just never came up). That means that I'm having difficulty trying to figure out how to do what I am wanting to do. I think I can handle the 4 lines without much difficulty, but how do I load the Word document, preserving all formatting, font, spacing, etc... and either add it to the internal page with my 4 lines already on it, or add my 4 lines to it before printing?
 
I'd just like to offer you another option as had to do the same as you (but a while ago, in VB6).

Use Word to create an image file (preferably JPG, although it could be BMP). You can then load this and place it onto your page, before adding the changeable content. This avoids the situation (as I discovered :mad:) when you find a PC in the office that doesnt have Word installed.

If you dont have a printer driver that can save to a BMP file, try http://sourceforge.net/projects/imageprinter/
 
Use Word to create an image file (preferably JPG, although it could be BMP). You can then load this and place it onto your page, before adding the changeable content.

I love that idea! :) Now I have to figure out how to print that! (how have I gone so long without ever having to print before?)
 
Start a new project. On Form1, add the following :-

1. A PrintDocument Control - call it "pdPrinter"
2. A PrintPreviewDialog control - call it "ppvwPrinter"
3. Two Buttons - call them "btnPrint" and "btnPrintPreview"

VB.NET:
Imports System.Drawing.Printing

Public Class Form1

    Private intTotalPages As Int32
    Private intPageNumber As Int32
    Private imFixedContent As Image
    Private strFixedContentFilename As String = "c:\your_graphic_filename.jpg"
    Private drawFont As Font
    Private drawBrush As SolidBrush

    Private Sub Begin_Printing(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles pdPrinter.BeginPrint
        intPageNumber = 0
        intTotalPages = 7
        imFixedContent = New Bitmap(strFixedContentFilename)
        drawFont = New Font("Arial", 24, FontStyle.Bold)
        drawBrush = New SolidBrush(Color.Black)
    End Sub

    Private Sub Test_PrintPage(ByVal sender As System.Object, ByVal e As PrintPageEventArgs) Handles pdPrinter.PrintPage
        intPageNumber += 1

        e.Graphics.PageUnit = GraphicsUnit.Millimeter

        'fixed content
        e.Graphics.DrawImage(imFixedContent, 5, 5, 200, 287)

        'variable content
        e.Graphics.DrawString(intPageNumber.ToString, drawFont, drawBrush, 100, 100)

        If intPageNumber < intTotalPages Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If
    End Sub


    Private Sub pdPrinter_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles pdPrinter.EndPrint
        imFixedContent.Dispose()
        drawFont.dispose()
        drawBrush.dispose()
    End Sub

    Private Sub btnPrintPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrintPreview.Click
        pdPrinter.DocumentName = "Test Document"
        ppvwPrinter.Document = pdPrinter
        ppvwPrinter.ShowDialog()
    End Sub
End Class

I'll let you work out the btnPrint code :D
 
Last edited:
Thanks! I think I've got a good image based template going. However, upon further reflection, I've come to the conclusion that I'd rather try for my original idea of having the Word document as a template instead. This is why:

The origin of this program is an older program which was hard coded to print a static page with just a few non-static lines (not written by me, by the way). There have been recent changes to what needs to appear in the static areas of the page, and no one will give me access to the original source code. I personally believe that they lost it and don't want to admit it. Anyway, since I'm rewriting it anyway, I figured I'd make it so that any future changes can be made without needing the source code. Printing the page as an image is conceptually easier on my end, but on the user side it's not nearly as intuitive as having an editable document. I'm not too worried about whether Word is installed because the people using this program are people who more than likely use Word a lot. On the off chance that they don't have it (a vanishingly small chance), having it installed would be no problem at all.

I think that in the end, what I'm really asking is how I might load data from a Word document in a way that translates faithfully to a printable object? Preferably, the printed page would be identical to the original document (i.e. look as if it were printed from Word itself). My assumption is that I if I can get that done, it's simply a matter of pasting my own 4 lines of non-static information onto/into the appropriate place in the printable object.
 
Last edited:
Back
Top